From 8efc68b306500959b27f4b53e496431dfeac27ff Mon Sep 17 00:00:00 2001 From: Krisjanis Lejejs Date: Sat, 30 Nov 2024 00:37:28 +0200 Subject: [PATCH] Code and workflow improvements --- .github/workflows/validate_hacs.yaml | 2 +- .github/workflows/validate_hass.yaml | 2 +- custom_components/thermia/__init__.py | 35 ++++----------------- custom_components/thermia/coordinator.py | 37 +++++++++++++++++++++++ custom_components/thermia/sensor.py | 1 - custom_components/thermia/water_heater.py | 9 +++--- 6 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 custom_components/thermia/coordinator.py diff --git a/.github/workflows/validate_hacs.yaml b/.github/workflows/validate_hacs.yaml index 152c838..51cec8b 100644 --- a/.github/workflows/validate_hacs.yaml +++ b/.github/workflows/validate_hacs.yaml @@ -9,7 +9,7 @@ on: - cron: "0 0 * * *" jobs: - validate: + validate_hacs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/validate_hass.yaml b/.github/workflows/validate_hass.yaml index f880532..1503b81 100644 --- a/.github/workflows/validate_hass.yaml +++ b/.github/workflows/validate_hass.yaml @@ -9,7 +9,7 @@ on: - cron: "0 0 * * *" jobs: - validate: + validate_hass: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/custom_components/thermia/__init__.py b/custom_components/thermia/__init__.py index edf0da8..cbbc545 100644 --- a/custom_components/thermia/__init__.py +++ b/custom_components/thermia/__init__.py @@ -4,16 +4,15 @@ import asyncio import logging -from datetime import timedelta from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.typing import ConfigType -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from ThermiaOnlineAPI import Thermia from .const import CONF_PASSWORD, CONF_USERNAME, DOMAIN +from .coordinator import ThermiaDataUpdateCoordinator PLATFORMS: list[str] = ["binary_sensor", "sensor", "switch", "water_heater"] @@ -47,7 +46,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) - config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry)) + config_entry.async_on_unload( + config_entry.add_update_listener(async_reload_entry)) + return True @@ -56,7 +57,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): unload_ok = all( await asyncio.gather( *[ - hass.config_entries.async_forward_entry_unload(entry, component) + hass.config_entries.async_forward_entry_unload( + entry, component) for component in PLATFORMS ] ) @@ -67,31 +69,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): return unload_ok -class ThermiaDataUpdateCoordinator(DataUpdateCoordinator): - """Thermia Data Update Coordinator.""" - - def __init__(self, hass: HomeAssistant, thermia: Thermia): - """Initialize the data update object.""" - - self.thermia = thermia - - super().__init__( - hass, - _LOGGER, - name=DOMAIN, - update_interval=timedelta(seconds=10), - ) - - async def _async_update_data(self): - """Update the data.""" - try: - await self.hass.async_add_executor_job(lambda: self.thermia.update_data()) - except Exception as exception: - raise UpdateFailed(exception) - - return self.thermia - - async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None: """Reload config entry.""" await async_unload_entry(hass, config_entry) diff --git a/custom_components/thermia/coordinator.py b/custom_components/thermia/coordinator.py new file mode 100644 index 0000000..82de066 --- /dev/null +++ b/custom_components/thermia/coordinator.py @@ -0,0 +1,37 @@ +from __future__ import annotations + +from datetime import timedelta +import logging + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from ThermiaOnlineAPI import Thermia + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +class ThermiaDataUpdateCoordinator(DataUpdateCoordinator[Thermia]): + """Thermia Data Update Coordinator.""" + + def __init__(self, hass: HomeAssistant, thermia: Thermia): + """Initialize the data update object.""" + + self.thermia = thermia + + super().__init__( + hass, + _LOGGER, + name=DOMAIN, + update_interval=timedelta(seconds=10), + ) + + async def _async_update_data(self): + """Update the data.""" + try: + await self.hass.async_add_executor_job(lambda: self.thermia.update_data()) + except Exception as exception: + raise UpdateFailed(exception) + + return self.thermia diff --git a/custom_components/thermia/sensor.py b/custom_components/thermia/sensor.py index d80ddc8..2f3b994 100644 --- a/custom_components/thermia/sensor.py +++ b/custom_components/thermia/sensor.py @@ -13,7 +13,6 @@ from .const import ( DOMAIN, - MDI_INFORMATION_OUTLINE_ICON, MDI_TEMPERATURE_ICON, MDI_TIMER_COG_OUTLINE_ICON, ) diff --git a/custom_components/thermia/water_heater.py b/custom_components/thermia/water_heater.py index 6503128..ad50012 100644 --- a/custom_components/thermia/water_heater.py +++ b/custom_components/thermia/water_heater.py @@ -16,6 +16,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN +from .coordinator import ThermiaDataUpdateCoordinator _LOGGER = logging.getLogger(__name__) @@ -28,20 +29,20 @@ async def async_setup_entry( ) -> None: """Set up the Thermia water heater.""" - coordinator = hass.data[DOMAIN][config_entry.entry_id] + coordinator: ThermiaDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] hass_water_heaters = [ ThermiaWaterHeater(coordinator, idx) - for idx, _ in enumerate(coordinator.data.heat_pumps) + for idx in range(len(coordinator.data.heat_pumps)) ] async_add_entities(hass_water_heaters) -class ThermiaWaterHeater(CoordinatorEntity, WaterHeaterEntity): +class ThermiaWaterHeater(CoordinatorEntity[ThermiaDataUpdateCoordinator], WaterHeaterEntity): """Representation of an Thermia water heater.""" - def __init__(self, coordinator, idx): + def __init__(self, coordinator: ThermiaDataUpdateCoordinator, idx: int): super().__init__(coordinator) self.idx = idx