From 213cb6f0fd04ec75eb702868a3fe17114b2d1e6f Mon Sep 17 00:00:00 2001 From: Bouwe Westerdijk <11290930+bouwew@users.noreply.github.com> Date: Mon, 24 Jun 2024 09:57:04 +0200 Subject: [PATCH] Improve Plugwise runtime-updating (#120230) --- .../components/plugwise/binary_sensor.py | 28 ++++++++----------- homeassistant/components/plugwise/climate.py | 7 ++--- .../components/plugwise/coordinator.py | 16 ++++------- homeassistant/components/plugwise/number.py | 7 ++--- homeassistant/components/plugwise/select.py | 7 ++--- homeassistant/components/plugwise/sensor.py | 27 ++++++------------ homeassistant/components/plugwise/switch.py | 22 ++++++--------- 7 files changed, 41 insertions(+), 73 deletions(-) diff --git a/homeassistant/components/plugwise/binary_sensor.py b/homeassistant/components/plugwise/binary_sensor.py index ef1051fa7b2..4b251d20a02 100644 --- a/homeassistant/components/plugwise/binary_sensor.py +++ b/homeassistant/components/plugwise/binary_sensor.py @@ -89,26 +89,20 @@ async def async_setup_entry( if not coordinator.new_devices: return - entities: list[PlugwiseBinarySensorEntity] = [] - for device_id, device in coordinator.data.devices.items(): - if not (binary_sensors := device.get("binary_sensors")): - continue - for description in BINARY_SENSORS: - if description.key not in binary_sensors: - continue - - entities.append( - PlugwiseBinarySensorEntity( - coordinator, - device_id, - description, - ) + async_add_entities( + PlugwiseBinarySensorEntity(coordinator, device_id, description) + for device_id in coordinator.new_devices + if ( + binary_sensors := coordinator.data.devices[device_id].get( + "binary_sensors" ) - async_add_entities(entities) - - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) + ) + for description in BINARY_SENSORS + if description.key in binary_sensors + ) _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity): diff --git a/homeassistant/components/plugwise/climate.py b/homeassistant/components/plugwise/climate.py index 29d44fe8159..7b0fe35835d 100644 --- a/homeassistant/components/plugwise/climate.py +++ b/homeassistant/components/plugwise/climate.py @@ -41,13 +41,12 @@ async def async_setup_entry( async_add_entities( PlugwiseClimateEntity(coordinator, device_id) - for device_id, device in coordinator.data.devices.items() - if device["dev_class"] in MASTER_THERMOSTATS + for device_id in coordinator.new_devices + if coordinator.data.devices[device_id]["dev_class"] in MASTER_THERMOSTATS ) - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) - _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity): diff --git a/homeassistant/components/plugwise/coordinator.py b/homeassistant/components/plugwise/coordinator.py index 34d983510ed..1dff11d26d8 100644 --- a/homeassistant/components/plugwise/coordinator.py +++ b/homeassistant/components/plugwise/coordinator.py @@ -15,7 +15,6 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryError -from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.debounce import Debouncer from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -55,8 +54,8 @@ class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]): timeout=30, websession=async_get_clientsession(hass, verify_ssl=False), ) - self.device_list: list[dr.DeviceEntry] = [] - self.new_devices: bool = False + self._current_devices: set[str] = set() + self.new_devices: set[str] = set() async def _connect(self) -> None: """Connect to the Plugwise Smile.""" @@ -81,13 +80,8 @@ class PlugwiseDataUpdateCoordinator(DataUpdateCoordinator[PlugwiseData]): raise ConfigEntryError("Device with unsupported firmware") from err except ConnectionFailedError as err: raise UpdateFailed("Failed to connect to the Plugwise Smile") from err - - device_reg = dr.async_get(self.hass) - device_list = dr.async_entries_for_config_entry( - device_reg, self.config_entry.entry_id - ) - - self.new_devices = len(data.devices.keys()) - len(self.device_list) > 0 - self.device_list = device_list + else: + self.new_devices = set(data.devices) - self._current_devices + self._current_devices = set(data.devices) return data diff --git a/homeassistant/components/plugwise/number.py b/homeassistant/components/plugwise/number.py index c84ca2cf5c7..1f12b2374b3 100644 --- a/homeassistant/components/plugwise/number.py +++ b/homeassistant/components/plugwise/number.py @@ -81,14 +81,13 @@ async def async_setup_entry( async_add_entities( PlugwiseNumberEntity(coordinator, device_id, description) - for device_id, device in coordinator.data.devices.items() + for device_id in coordinator.new_devices for description in NUMBER_TYPES - if description.key in device + if description.key in coordinator.data.devices[device_id] ) - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) - _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity): diff --git a/homeassistant/components/plugwise/select.py b/homeassistant/components/plugwise/select.py index 88c97b9b9f3..c8c9791c0da 100644 --- a/homeassistant/components/plugwise/select.py +++ b/homeassistant/components/plugwise/select.py @@ -74,14 +74,13 @@ async def async_setup_entry( async_add_entities( PlugwiseSelectEntity(coordinator, device_id, description) - for device_id, device in coordinator.data.devices.items() + for device_id in coordinator.new_devices for description in SELECT_TYPES - if description.options_key in device + if description.options_key in coordinator.data.devices[device_id] ) - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) - _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity): diff --git a/homeassistant/components/plugwise/sensor.py b/homeassistant/components/plugwise/sensor.py index 147bab828a8..ae5b4e6ed91 100644 --- a/homeassistant/components/plugwise/sensor.py +++ b/homeassistant/components/plugwise/sensor.py @@ -414,27 +414,16 @@ async def async_setup_entry( if not coordinator.new_devices: return - entities: list[PlugwiseSensorEntity] = [] - for device_id, device in coordinator.data.devices.items(): - if not (sensors := device.get("sensors")): - continue - for description in SENSORS: - if description.key not in sensors: - continue - - entities.append( - PlugwiseSensorEntity( - coordinator, - device_id, - description, - ) - ) - - async_add_entities(entities) - - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) + async_add_entities( + PlugwiseSensorEntity(coordinator, device_id, description) + for device_id in coordinator.new_devices + if (sensors := coordinator.data.devices[device_id].get("sensors")) + for description in SENSORS + if description.key in sensors + ) _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseSensorEntity(PlugwiseEntity, SensorEntity): diff --git a/homeassistant/components/plugwise/switch.py b/homeassistant/components/plugwise/switch.py index 3ed2d14b8dd..a134ab5b044 100644 --- a/homeassistant/components/plugwise/switch.py +++ b/homeassistant/components/plugwise/switch.py @@ -68,22 +68,16 @@ async def async_setup_entry( if not coordinator.new_devices: return - entities: list[PlugwiseSwitchEntity] = [] - for device_id, device in coordinator.data.devices.items(): - if not (switches := device.get("switches")): - continue - for description in SWITCHES: - if description.key not in switches: - continue - entities.append( - PlugwiseSwitchEntity(coordinator, device_id, description) - ) - - async_add_entities(entities) - - entry.async_on_unload(coordinator.async_add_listener(_add_entities)) + async_add_entities( + PlugwiseSwitchEntity(coordinator, device_id, description) + for device_id in coordinator.new_devices + if (switches := coordinator.data.devices[device_id].get("switches")) + for description in SWITCHES + if description.key in switches + ) _add_entities() + entry.async_on_unload(coordinator.async_add_listener(_add_entities)) class PlugwiseSwitchEntity(PlugwiseEntity, SwitchEntity):