From e9d1bb2056f6f61318a0378f146d8dd511ff4c29 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Thu, 25 May 2023 00:52:54 +0200 Subject: [PATCH] Refactor get coordinator --- custom_components/hon/binary_sensor.py | 13 ++++--------- custom_components/hon/button.py | 10 ++-------- custom_components/hon/climate.py | 15 +++++---------- custom_components/hon/config_flow.py | 2 +- custom_components/hon/hon.py | 14 ++++++++++++-- custom_components/hon/number.py | 9 ++------- custom_components/hon/select.py | 17 ++++++----------- custom_components/hon/sensor.py | 13 ++++--------- custom_components/hon/switch.py | 19 +++++++------------ 9 files changed, 43 insertions(+), 69 deletions(-) diff --git a/custom_components/hon/binary_sensor.py b/custom_components/hon/binary_sensor.py index 469ee17..08738d7 100644 --- a/custom_components/hon/binary_sensor.py +++ b/custom_components/hon/binary_sensor.py @@ -1,8 +1,6 @@ import logging from dataclasses import dataclass -from pyhon import Hon - from homeassistant.components.binary_sensor import ( BinarySensorEntityDescription, BinarySensorDeviceClass, @@ -10,8 +8,10 @@ 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 HonCoordinator, HonEntity, unique_entities +from .hon import HonEntity, unique_entities, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -251,14 +251,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := BINARY_SENSORS.get(device.appliance_type): diff --git a/custom_components/hon/button.py b/custom_components/hon/button.py index 160e450..caf7531 100644 --- a/custom_components/hon/button.py +++ b/custom_components/hon/button.py @@ -1,7 +1,6 @@ import logging import pkg_resources - from homeassistant.components import persistent_notification from homeassistant.components.button import ButtonEntityDescription, ButtonEntity from homeassistant.config_entries import ConfigEntry @@ -10,7 +9,7 @@ from pyhon import Hon from pyhon.appliance import HonAppliance from .const import DOMAIN -from .hon import HonCoordinator, HonEntity +from .hon import HonEntity, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -42,14 +41,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := BUTTONS.get(device.appliance_type): diff --git a/custom_components/hon/climate.py b/custom_components/hon/climate.py index e8b55b1..3b72c04 100644 --- a/custom_components/hon/climate.py +++ b/custom_components/hon/climate.py @@ -1,8 +1,5 @@ import logging -from pyhon import Hon -from pyhon.appliance import HonAppliance - from homeassistant.components.climate import ( ClimateEntity, ClimateEntityDescription, @@ -23,8 +20,11 @@ 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, HonCoordinator +from .hon import HonEntity, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -42,14 +42,9 @@ CLIMATES = { async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: hon: Hon = hass.data[DOMAIN][entry.unique_id] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := CLIMATES.get(device.appliance_type): diff --git a/custom_components/hon/config_flow.py b/custom_components/hon/config_flow.py index 468ba42..e084ce9 100644 --- a/custom_components/hon/config_flow.py +++ b/custom_components/hon/config_flow.py @@ -1,9 +1,9 @@ import logging import voluptuous as vol - from homeassistant import config_entries from homeassistant.const import CONF_EMAIL, CONF_PASSWORD + from .const import DOMAIN _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/hon/hon.py b/custom_components/hon/hon.py index c88283b..2b4e624 100644 --- a/custom_components/hon/hon.py +++ b/custom_components/hon/hon.py @@ -1,11 +1,11 @@ import logging from datetime import timedelta -from pyhon.appliance import HonAppliance - from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from pyhon.appliance import HonAppliance + from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -59,3 +59,13 @@ def unique_entities(base_entities, new_entities): if entity.key not in existing_entities: result.append(entity) return tuple(result) + + +def get_coordinator(hass, appliance): + coordinators = hass.data[DOMAIN]["coordinators"] + if appliance.unique_id in coordinators: + coordinator = hass.data[DOMAIN]["coordinators"][appliance.unique_id] + else: + coordinator = HonCoordinator(hass, appliance) + hass.data[DOMAIN]["coordinators"][appliance.unique_id] = coordinator + return coordinator diff --git a/custom_components/hon/number.py b/custom_components/hon/number.py index dbca6dc..04b15ad 100644 --- a/custom_components/hon/number.py +++ b/custom_components/hon/number.py @@ -14,7 +14,7 @@ from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.range import HonParameterRange from .const import DOMAIN -from .hon import HonEntity, HonCoordinator, unique_entities +from .hon import HonEntity, unique_entities, get_coordinator NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = { "WM": ( @@ -175,14 +175,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := NUMBERS.get(device.appliance_type): diff --git a/custom_components/hon/select.py b/custom_components/hon/select.py index 40764f8..6a7b2d0 100644 --- a/custom_components/hon/select.py +++ b/custom_components/hon/select.py @@ -2,17 +2,17 @@ from __future__ import annotations import logging -from pyhon import Hon -from pyhon.appliance import HonAppliance -from pyhon.parameter.fixed import HonParameterFixed - from homeassistant.components.select import SelectEntity, SelectEntityDescription 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, HonCoordinator, unique_entities +from .hon import HonEntity, unique_entities, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -139,14 +139,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := SELECTS.get(device.appliance_type): diff --git a/custom_components/hon/sensor.py b/custom_components/hon/sensor.py index 64d782b..cfe03b7 100644 --- a/custom_components/hon/sensor.py +++ b/custom_components/hon/sensor.py @@ -1,7 +1,5 @@ import logging -from pyhon import Hon - from homeassistant.components.sensor import ( SensorEntity, SensorDeviceClass, @@ -22,9 +20,11 @@ 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 HonCoordinator, HonEntity, unique_entities +from .hon import HonEntity, unique_entities, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -512,14 +512,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := SENSORS.get(device.appliance_type): diff --git a/custom_components/hon/switch.py b/custom_components/hon/switch.py index fb4e78c..41d6edb 100644 --- a/custom_components/hon/switch.py +++ b/custom_components/hon/switch.py @@ -2,17 +2,17 @@ import logging from dataclasses import dataclass from typing import Any -from pyhon import Hon -from pyhon.appliance import HonAppliance -from pyhon.parameter.base import HonParameter -from pyhon.parameter.range import HonParameterRange - from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity 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 HonCoordinator, HonEntity, unique_entities +from .hon import HonEntity, unique_entities, get_coordinator _LOGGER = logging.getLogger(__name__) @@ -355,14 +355,9 @@ 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] - coordinators = hass.data[DOMAIN]["coordinators"] appliances = [] for device in hon.appliances: - if device.unique_id in coordinators: - coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id] - else: - coordinator = HonCoordinator(hass, device) - hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator + coordinator = get_coordinator(hass, device) await coordinator.async_config_entry_first_refresh() if descriptions := SWITCHES.get(device.appliance_type):