Skip to content

Commit

Permalink
Merge pull request #105 from klejejs/chore/code-improvements
Browse files Browse the repository at this point in the history
Code and workflow improvements
  • Loading branch information
klejejs authored Nov 29, 2024
2 parents 3b9c8b2 + 8efc68b commit be5fb0a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate_hacs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- cron: "0 0 * * *"

jobs:
validate:
validate_hacs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate_hass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- cron: "0 0 * * *"

jobs:
validate:
validate_hass:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
35 changes: 6 additions & 29 deletions custom_components/thermia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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


Expand All @@ -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
]
)
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions custom_components/thermia/coordinator.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion custom_components/thermia/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from .const import (
DOMAIN,
MDI_INFORMATION_OUTLINE_ICON,
MDI_TEMPERATURE_ICON,
MDI_TIMER_COG_OUTLINE_ICON,
)
Expand Down
9 changes: 5 additions & 4 deletions custom_components/thermia/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import ThermiaDataUpdateCoordinator


_LOGGER = logging.getLogger(__name__)
Expand All @@ -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

Expand Down

0 comments on commit be5fb0a

Please sign in to comment.