Skip to content

Commit

Permalink
Update project by removing deprecated API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kvj committed Dec 25, 2024
1 parent bcc4eb1 commit 6f83c73
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 56 deletions.
76 changes: 31 additions & 45 deletions custom_components/nuki_ng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from .constants import DOMAIN, PLATFORMS

from homeassistant.core import HomeAssistant
from homeassistant.helpers import service, entity_registry, device_registry
from homeassistant.helpers import service
from homeassistant.helpers.entity import EntityCategory
from homeassistant.config_entries import ConfigEntry

# from homeassistant.helpers import device_registry
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
)
Expand All @@ -24,62 +24,48 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
_LOGGER.debug(f"async_setup_entry: {data}")

coordinator = NukiCoordinator(hass, entry, data)
entry.runtime_data = coordinator
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = coordinator

for p in PLATFORMS:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, p))
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True


async def async_unload_entry(hass: HomeAssistant, entry):
await hass.data[DOMAIN][entry.entry_id].unload()
for p in PLATFORMS:
await hass.config_entries.async_forward_entry_unload(entry, p)
hass.data[DOMAIN].pop(entry.entry_id)
coordinator = entry.runtime_data
await coordinator.unload()
await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
entry.runtime_data = None
return True


async def _extract_dev_ids(hass, service_call) -> set[str]:
entity_ids = await service.async_extract_entity_ids(hass, service_call)
result = set()
entity_reg = entity_registry.async_get(hass)
device_reg = device_registry.async_get(hass)
for id in entity_ids:
if entry := entity_reg.async_get(id):
if device := device_reg.async_get(entry.device_id):
ids = {x[0]: x[1] for x in device.identifiers}
result.add((entry.config_entry_id, ids.get("id")))
return result


async def async_setup(hass: HomeAssistant, config) -> bool:
hass.data[DOMAIN] = dict()

async def async_reboot(call):
def _register_coordinator_service(hass: HomeAssistant, name: str, handler):
async def handler_(call):
for entry_id in await service.async_extract_config_entry_ids(hass, call):
await hass.data[DOMAIN][entry_id].do_reboot()
if entry := hass.config_entries.async_get_entry(entry_id):
_LOGGER.debug(f"_register_coordinator_service: {name}: {entry.domain}")
if entry.domain == DOMAIN:
handler(entry.runtime_data, call.data)
hass.services.async_register(DOMAIN, name, handler_)

async def async_fwupdate(call):
for entry_id in await service.async_extract_config_entry_ids(hass, call):
await hass.data[DOMAIN][entry_id].do_fwupdate()

async def async_delete_callback(call):
for entry_id in await service.async_extract_config_entry_ids(hass, call):
await hass.data[DOMAIN][entry_id].do_delete_callback(
call.data.get("callback")
)

async def async_exec_action(call):
for entry_id, dev_id in await _extract_dev_ids(hass, call):
await hass.data[DOMAIN][entry_id].action(dev_id, call.data.get("action"))

hass.services.async_register(DOMAIN, "bridge_reboot", async_reboot)
hass.services.async_register(DOMAIN, "bridge_fwupdate", async_fwupdate)
hass.services.async_register(
DOMAIN, "bridge_delete_callback", async_delete_callback
async def async_setup(hass: HomeAssistant, config) -> bool:
_register_coordinator_service(
hass, "bridge_reboot",
lambda coord, _: hass.async_create_task(coord.do_reboot())
)
_register_coordinator_service(
hass, "bridge_fwupdate",
lambda coord, _: hass.async_create_task(coord.do_fwupdate())
)
_register_coordinator_service(
hass, "bridge_delete_callback",
lambda coord, data: hass.async_create_task(coord.do_delete_callback(data.get("callback")))
)
_register_coordinator_service(
hass, "execute_action",
lambda coord, data: hass.async_create_task(coord.action_for_devices(data.get("action")))
)
hass.services.async_register(DOMAIN, "execute_action", async_exec_action)

return True

Expand Down
3 changes: 1 addition & 2 deletions custom_components/nuki_ng/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

async def async_setup_entry(hass, entry, async_add_entities):
entities = []
data = entry.as_dict()
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

for dev_id in coordinator.data.get("devices", {}):
entities.append(BatteryLow(coordinator, dev_id))
Expand Down
3 changes: 1 addition & 2 deletions custom_components/nuki_ng/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

async def async_setup_entry(hass, entry, async_add_entities):
entities = []
data = entry.as_dict()
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

if coordinator.api.can_bridge():
entities.append(NukiBridgeRestartButton(coordinator))
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nuki_ng/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

async def async_setup_entry(hass, entry, async_add_entities):
entities = []
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

for dev_id in coordinator.data.get("devices", {}):
entities.append(Lock(coordinator, dev_id))
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nuki_ng/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"requirements": [],
"iot_class": "local_polling",
"config_flow": true,
"version": "0.2.1"
"version": "0.5.3"
}
4 changes: 4 additions & 0 deletions custom_components/nuki_ng/nuki.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ async def unload(self):
except Exception:
_LOGGER.exception(f"Failed to remove callback")

async def action_for_devices(self, action: str):
for device_id in self.data.get("devices", {}):
await self.action(device_id, action)

async def action(self, dev_id: str, action: str):
if self.api.can_bridge():
device_type = self.device_data(dev_id).get("deviceType")
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nuki_ng/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def async_setup_entry(
async_add_entities
):
entities = []
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

for dev_id in coordinator.data.get("devices", {}):
if coordinator.info_field(dev_id, -1, "openerAdvancedConfig", "doorbellSuppression") >= 0:
Expand Down
3 changes: 1 addition & 2 deletions custom_components/nuki_ng/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

async def async_setup_entry(hass, entry, async_add_entities):
entities = []
data = entry.as_dict()
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

if coordinator.api.can_bridge():
entities.append(BridgeWifiVersion(coordinator))
Expand Down
3 changes: 1 addition & 2 deletions custom_components/nuki_ng/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ async def async_setup_entry(
async_add_entities
):
entities = []
data = entry.as_dict()
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = entry.runtime_data

for dev_id in coordinator.data.get("devices", {}):
for auth_id in coordinator.device_data(dev_id).get("web_auth", {}):
Expand Down

0 comments on commit 6f83c73

Please sign in to comment.