From 696dc136ebc72d74a006976ab4984ca1d55ab363 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Thu, 25 May 2023 01:30:33 +0200 Subject: [PATCH] Refactor entry setup --- custom_components/hon/binary_sensor.py | 31 +++++++--------- custom_components/hon/button.py | 40 +++++++++------------ custom_components/hon/climate.py | 32 +++++++---------- custom_components/hon/hon.py | 3 +- custom_components/hon/number.py | 31 +++++++--------- custom_components/hon/select.py | 32 +++++++---------- custom_components/hon/sensor.py | 34 ++++++++---------- custom_components/hon/switch.py | 49 +++++++++++--------------- 8 files changed, 101 insertions(+), 151 deletions(-) diff --git a/custom_components/hon/binary_sensor.py b/custom_components/hon/binary_sensor.py index 08738d7..856d17d 100644 --- a/custom_components/hon/binary_sensor.py +++ b/custom_components/hon/binary_sensor.py @@ -8,10 +8,9 @@ from homeassistant.components.binary_sensor import ( ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import callback -from pyhon import Hon from .const import DOMAIN -from .hon import HonEntity, unique_entities, get_coordinator +from .hon import HonEntity, unique_entities _LOGGER = logging.getLogger(__name__) @@ -250,28 +249,22 @@ BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD" async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() - - if descriptions := BINARY_SENSORS.get(device.appliance_type): - for description in descriptions: - if not device.get(description.key): - continue - appliances.append( - HonBinarySensorEntity(hass, coordinator, entry, device, description) - ) - - async_add_entities(appliances) + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in BINARY_SENSORS.get(device.appliance_type, []): + if not device.get(description.key): + continue + entity = HonBinarySensorEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) + async_add_entities(entities) class HonBinarySensorEntity(HonEntity, BinarySensorEntity): entity_description: HonBinarySensorEntityDescription - def __init__(self, hass, coordinator, entry, device, description) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device, description) -> None: + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}" diff --git a/custom_components/hon/button.py b/custom_components/hon/button.py index caf7531..10387d8 100644 --- a/custom_components/hon/button.py +++ b/custom_components/hon/button.py @@ -5,11 +5,10 @@ from homeassistant.components import persistent_notification from homeassistant.components.button import ButtonEntityDescription, ButtonEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory -from pyhon import Hon from pyhon.appliance import HonAppliance from .const import DOMAIN -from .hon import HonEntity, get_coordinator +from .hon import HonEntity _LOGGER = logging.getLogger(__name__) @@ -40,29 +39,22 @@ BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = { async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() - - if descriptions := BUTTONS.get(device.appliance_type): - for description in descriptions: - if not device.commands.get(description.key): - continue - appliances.extend( - [HonButtonEntity(hass, coordinator, entry, device, description)] - ) - appliances.extend([HonFeatureRequestButton(hass, coordinator, entry, device)]) - - async_add_entities(appliances) + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in BUTTONS.get(device.appliance_type, []): + if not device.commands.get(description.key): + continue + entity = HonButtonEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) + entities.append(HonFeatureRequestButton(hass, entry, device)) + await entities[-1].coordinator.async_config_entry_first_refresh() + async_add_entities(entities) class HonButtonEntity(HonEntity, ButtonEntity): - def __init__( - self, hass, coordinator, entry, device: HonAppliance, description - ) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device: HonAppliance, description) -> None: + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}" @@ -81,8 +73,8 @@ class HonButtonEntity(HonEntity, ButtonEntity): class HonFeatureRequestButton(HonEntity, ButtonEntity): - def __init__(self, hass, coordinator, entry, device: HonAppliance) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device: HonAppliance) -> None: + super().__init__(hass, entry, device) self._attr_unique_id = f"{super().unique_id}_log_device_info" self._attr_icon = "mdi:information" diff --git a/custom_components/hon/climate.py b/custom_components/hon/climate.py index 3b72c04..decbe65 100644 --- a/custom_components/hon/climate.py +++ b/custom_components/hon/climate.py @@ -20,11 +20,10 @@ from homeassistant.const import ( TEMP_CELSIUS, ) from homeassistant.core import callback -from pyhon import Hon from pyhon.appliance import HonAppliance from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN -from .hon import HonEntity, get_coordinator +from .hon import HonEntity _LOGGER = logging.getLogger(__name__) @@ -41,27 +40,20 @@ CLIMATES = { async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() - - if descriptions := CLIMATES.get(device.appliance_type): - for description in descriptions: - if description.key not in list(device.commands): - continue - appliances.append( - HonClimateEntity(hass, coordinator, entry, device, description) - ) - async_add_entities(appliances) + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in CLIMATES.get(device.appliance_type, []): + if description.key not in list(device.commands): + continue + entity = HonClimateEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) + async_add_entities(entities) class HonClimateEntity(HonEntity, ClimateEntity): - def __init__( - self, hass, coordinator, entry, device: HonAppliance, description - ) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device: HonAppliance, description) -> None: + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}climate" diff --git a/custom_components/hon/hon.py b/custom_components/hon/hon.py index 2b4e624..da7c45a 100644 --- a/custom_components/hon/hon.py +++ b/custom_components/hon/hon.py @@ -14,7 +14,8 @@ _LOGGER = logging.getLogger(__name__) class HonEntity(CoordinatorEntity): _attr_has_entity_name = True - def __init__(self, hass, entry, coordinator, device: HonAppliance) -> None: + def __init__(self, hass, entry, device: HonAppliance) -> None: + coordinator = get_coordinator(hass, device) super().__init__(coordinator) self._hon = hass.data[DOMAIN][entry.unique_id] diff --git a/custom_components/hon/number.py b/custom_components/hon/number.py index 04b15ad..2932cc1 100644 --- a/custom_components/hon/number.py +++ b/custom_components/hon/number.py @@ -8,13 +8,12 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfTime, UnitOfTemperature from homeassistant.core import callback from homeassistant.helpers.entity import EntityCategory -from pyhon import Hon from pyhon.parameter.base import HonParameter from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.range import HonParameterRange from .const import DOMAIN -from .hon import HonEntity, unique_entities, get_coordinator +from .hon import HonEntity, unique_entities NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = { "WM": ( @@ -174,26 +173,20 @@ NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"]) async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() - - if descriptions := NUMBERS.get(device.appliance_type): - for description in descriptions: - if description.key not in device.available_settings: - continue - appliances.extend( - [HonNumberEntity(hass, coordinator, entry, device, description)] - ) - - async_add_entities(appliances) + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in NUMBERS.get(device.appliance_type, []): + if description.key not in device.available_settings: + continue + entity = HonNumberEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) + async_add_entities(entities) class HonNumberEntity(HonEntity, NumberEntity): - def __init__(self, hass, coordinator, entry, device, description) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device, description) -> None: + super().__init__(hass, entry, device) self._data = device.settings[description.key] self.entity_description = description diff --git a/custom_components/hon/select.py b/custom_components/hon/select.py index 6a7b2d0..9e59d58 100644 --- a/custom_components/hon/select.py +++ b/custom_components/hon/select.py @@ -7,12 +7,11 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE from homeassistant.core import callback from homeassistant.helpers.entity import EntityCategory -from pyhon import Hon from pyhon.appliance import HonAppliance from pyhon.parameter.fixed import HonParameterFixed from .const import DOMAIN -from .hon import HonEntity, unique_entities, get_coordinator +from .hon import HonEntity, unique_entities _LOGGER = logging.getLogger(__name__) @@ -138,27 +137,20 @@ SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"]) async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() - - if descriptions := SELECTS.get(device.appliance_type): - for description in descriptions: - if description.key not in device.available_settings: - continue - appliances.extend( - [HonSelectEntity(hass, coordinator, entry, device, description)] - ) - async_add_entities(appliances) + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in SELECTS.get(device.appliance_type, []): + if description.key not in device.available_settings: + continue + entity = HonSelectEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) + async_add_entities(entities) class HonSelectEntity(HonEntity, SelectEntity): - def __init__( - self, hass, coordinator, entry, device: HonAppliance, description - ) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device: HonAppliance, description) -> None: + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}" diff --git a/custom_components/hon/sensor.py b/custom_components/hon/sensor.py index cfe03b7..6ddfc62 100644 --- a/custom_components/hon/sensor.py +++ b/custom_components/hon/sensor.py @@ -20,11 +20,10 @@ from homeassistant.const import ( from homeassistant.core import callback from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.typing import StateType -from pyhon import Hon from . import const from .const import DOMAIN -from .hon import HonEntity, unique_entities, get_coordinator +from .hon import HonEntity, unique_entities _LOGGER = logging.getLogger(__name__) @@ -511,28 +510,23 @@ SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"]) async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in SENSORS.get(device.appliance_type, []): + if not device.get(description.key) and not device.settings.get( + description.key + ): + continue + entity = HonSensorEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) - if descriptions := SENSORS.get(device.appliance_type): - for description in descriptions: - if not device.get(description.key) and not device.settings.get( - description.key - ): - continue - appliances.extend( - [HonSensorEntity(hass, coordinator, entry, device, description)] - ) - - async_add_entities(appliances) + async_add_entities(entities) class HonSensorEntity(HonEntity, SensorEntity): - def __init__(self, hass, coordinator, entry, device, description) -> None: - super().__init__(hass, entry, coordinator, device) + def __init__(self, hass, entry, device, description) -> None: + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}" diff --git a/custom_components/hon/switch.py b/custom_components/hon/switch.py index 41d6edb..56ea512 100644 --- a/custom_components/hon/switch.py +++ b/custom_components/hon/switch.py @@ -6,13 +6,12 @@ from homeassistant.components.switch import SwitchEntityDescription, SwitchEntit from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import callback -from pyhon import Hon from pyhon.appliance import HonAppliance from pyhon.parameter.base import HonParameter from pyhon.parameter.range import HonParameterRange from .const import DOMAIN -from .hon import HonEntity, unique_entities, get_coordinator +from .hon import HonEntity, unique_entities _LOGGER = logging.getLogger(__name__) @@ -354,31 +353,26 @@ SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"]) async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: - hon: Hon = hass.data[DOMAIN][entry.unique_id] - appliances = [] - for device in hon.appliances: - coordinator = get_coordinator(hass, device) - await coordinator.async_config_entry_first_refresh() + entities = [] + for device in hass.data[DOMAIN][entry.unique_id].appliances: + for description in SWITCHES.get(device.appliance_type, []): + if description.entity_category == EntityCategory.CONFIG: + if description.key not in device.available_settings: + continue + else: + if not any( + [ + device.get(description.key) is not None, + description.turn_on_key in list(device.commands), + description.turn_off_key in list(device.commands), + ] + ): + continue + entity = HonSwitchEntity(hass, entry, device, description) + await entity.coordinator.async_config_entry_first_refresh() + entities.append(entity) - if descriptions := SWITCHES.get(device.appliance_type): - for description in descriptions: - if description.entity_category == EntityCategory.CONFIG: - if description.key not in device.available_settings: - continue - else: - if not any( - [ - device.get(description.key) is not None, - description.turn_on_key in list(device.commands), - description.turn_off_key in list(device.commands), - ] - ): - continue - appliances.extend( - [HonSwitchEntity(hass, coordinator, entry, device, description)] - ) - - async_add_entities(appliances) + async_add_entities(entities) class HonSwitchEntity(HonEntity, SwitchEntity): @@ -387,12 +381,11 @@ class HonSwitchEntity(HonEntity, SwitchEntity): def __init__( self, hass, - coordinator, entry, device: HonAppliance, description: HonSwitchEntityDescription, ) -> None: - super().__init__(hass, entry, coordinator, device) + super().__init__(hass, entry, device) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}"