From 6f83c73eb003cf8a1305b40ddb7c9464c39398f5 Mon Sep 17 00:00:00 2001 From: Konstantin Vorobyev Date: Wed, 25 Dec 2024 19:12:54 +0100 Subject: [PATCH] Update project by removing deprecated API usage --- custom_components/nuki_ng/__init__.py | 76 +++++++++------------- custom_components/nuki_ng/binary_sensor.py | 3 +- custom_components/nuki_ng/button.py | 3 +- custom_components/nuki_ng/lock.py | 2 +- custom_components/nuki_ng/manifest.json | 2 +- custom_components/nuki_ng/nuki.py | 4 ++ custom_components/nuki_ng/select.py | 2 +- custom_components/nuki_ng/sensor.py | 3 +- custom_components/nuki_ng/switch.py | 3 +- 9 files changed, 42 insertions(+), 56 deletions(-) diff --git a/custom_components/nuki_ng/__init__.py b/custom_components/nuki_ng/__init__.py index 12a4ef6..aebc389 100644 --- a/custom_components/nuki_ng/__init__.py +++ b/custom_components/nuki_ng/__init__.py @@ -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, ) @@ -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 diff --git a/custom_components/nuki_ng/binary_sensor.py b/custom_components/nuki_ng/binary_sensor.py index a4311e1..4937019 100644 --- a/custom_components/nuki_ng/binary_sensor.py +++ b/custom_components/nuki_ng/binary_sensor.py @@ -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)) diff --git a/custom_components/nuki_ng/button.py b/custom_components/nuki_ng/button.py index 0a3bdaa..a622397 100644 --- a/custom_components/nuki_ng/button.py +++ b/custom_components/nuki_ng/button.py @@ -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)) diff --git a/custom_components/nuki_ng/lock.py b/custom_components/nuki_ng/lock.py index 0e339ef..b27405f 100644 --- a/custom_components/nuki_ng/lock.py +++ b/custom_components/nuki_ng/lock.py @@ -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)) diff --git a/custom_components/nuki_ng/manifest.json b/custom_components/nuki_ng/manifest.json index 2412126..4ef6479 100644 --- a/custom_components/nuki_ng/manifest.json +++ b/custom_components/nuki_ng/manifest.json @@ -8,5 +8,5 @@ "requirements": [], "iot_class": "local_polling", "config_flow": true, - "version": "0.2.1" + "version": "0.5.3" } diff --git a/custom_components/nuki_ng/nuki.py b/custom_components/nuki_ng/nuki.py index d1dfe78..ea12728 100644 --- a/custom_components/nuki_ng/nuki.py +++ b/custom_components/nuki_ng/nuki.py @@ -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") diff --git a/custom_components/nuki_ng/select.py b/custom_components/nuki_ng/select.py index 0eac8dd..b8bcdfd 100644 --- a/custom_components/nuki_ng/select.py +++ b/custom_components/nuki_ng/select.py @@ -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: diff --git a/custom_components/nuki_ng/sensor.py b/custom_components/nuki_ng/sensor.py index 68ab0c7..44a5268 100644 --- a/custom_components/nuki_ng/sensor.py +++ b/custom_components/nuki_ng/sensor.py @@ -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)) diff --git a/custom_components/nuki_ng/switch.py b/custom_components/nuki_ng/switch.py index d701e11..80bcc62 100644 --- a/custom_components/nuki_ng/switch.py +++ b/custom_components/nuki_ng/switch.py @@ -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", {}):