Refactor bsblan coordinator (#124308)

* chore: Refactor BSBLanUpdateCoordinator to improve code readability and maintainability

* feat: Add BSBLan integration models

This commit adds the models for the BSB-Lan integration. It includes a dataclass for the BSBLanCoordinatorData, which stores the state and sensor information.

* refactor: Update BSBLANClimate class to use DataUpdateCoordinator without specifying the State type

* chore: Remove unused Sensor import in BSBLan models

* feat: Refactor BSBLanEntity to use CoordinatorEntity

The BSBLanEntity class has been refactored to inherit from the CoordinatorEntity class, which provides better integration with the update coordinator. This change improves code readability and maintainability.

* refactor: Remove unused config_entry variable in BSBLanUpdateCoordinator

* refactor: Update BSBLANClimate class to use DataUpdateCoordinator

Refactor the BSBLANClimate class to use the Coordinator of the entity

* refactor: Update tests to use the new structure

* fix coverage

 it should be the same as before

* refactor: moved dataclass BSBLanCoordinatorData

* use the data class inside init

* refactor: Remove unused config_entry variable in BSBLanUpdateCoordinator

* refactor: use BSBLanData from init

* remove entry data from diagnostics

* fix: add random interval back

* refactor: Simplify coordinator_data assignment in async_get_config_entry_diagnostics

* revert back to original except dataclass import

* revert: Add MAC address back to device info in BSBLanEntity
This commit is contained in:
Willem-Jan van Rootselaar 2024-08-23 08:42:36 +02:00 committed by GitHub
parent 7c6e3fe9c4
commit 4e94ce0cc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 101 additions and 95 deletions

View File

@ -22,7 +22,7 @@ PLATFORMS = [Platform.CLIMATE]
@dataclasses.dataclass @dataclasses.dataclass
class HomeAssistantBSBLANData: class BSBLanData:
"""BSBLan data stored in the Home Assistant data object.""" """BSBLan data stored in the Home Assistant data object."""
coordinator: BSBLanUpdateCoordinator coordinator: BSBLanUpdateCoordinator
@ -57,7 +57,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
info = await bsblan.info() info = await bsblan.info()
static = await bsblan.static_values() static = await bsblan.static_values()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantBSBLANData( hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BSBLanData(
client=bsblan, client=bsblan,
coordinator=coordinator, coordinator=coordinator,
device=device, device=device,

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from typing import Any from typing import Any
from bsblan import BSBLAN, BSBLANError, Device, Info, State, StaticState from bsblan import BSBLANError
from homeassistant.components.climate import ( from homeassistant.components.climate import (
ATTR_HVAC_MODE, ATTR_HVAC_MODE,
@ -21,15 +21,11 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from homeassistant.util.enum import try_parse_enum from homeassistant.util.enum import try_parse_enum
from . import HomeAssistantBSBLANData from . import BSBLanData
from .const import ATTR_TARGET_TEMPERATURE, DOMAIN from .const import ATTR_TARGET_TEMPERATURE, DOMAIN
from .entity import BSBLANEntity from .entity import BSBLanEntity
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
@ -51,24 +47,17 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up BSBLAN device based on a config entry.""" """Set up BSBLAN device based on a config entry."""
data: HomeAssistantBSBLANData = hass.data[DOMAIN][entry.entry_id] data: BSBLanData = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
[ [
BSBLANClimate( BSBLANClimate(
data.coordinator, data,
data.client,
data.device,
data.info,
data.static,
entry,
) )
] ]
) )
class BSBLANClimate( class BSBLANClimate(BSBLanEntity, ClimateEntity):
BSBLANEntity, CoordinatorEntity[DataUpdateCoordinator[State]], ClimateEntity
):
"""Defines a BSBLAN climate device.""" """Defines a BSBLAN climate device."""
_attr_has_entity_name = True _attr_has_entity_name = True
@ -80,30 +69,22 @@ class BSBLANClimate(
| ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_ON
) )
_attr_preset_modes = PRESET_MODES
# Determine hvac modes _attr_preset_modes = PRESET_MODES
_attr_hvac_modes = HVAC_MODES _attr_hvac_modes = HVAC_MODES
_enable_turn_on_off_backwards_compatibility = False _enable_turn_on_off_backwards_compatibility = False
def __init__( def __init__(
self, self,
coordinator: DataUpdateCoordinator[State], data: BSBLanData,
client: BSBLAN,
device: Device,
info: Info,
static: StaticState,
entry: ConfigEntry,
) -> None: ) -> None:
"""Initialize BSBLAN climate device.""" """Initialize BSBLAN climate device."""
super().__init__(client, device, info, static, entry) super().__init__(data.coordinator, data)
CoordinatorEntity.__init__(self, coordinator) self._attr_unique_id = f"{format_mac(data.device.MAC)}-climate"
self._attr_unique_id = f"{format_mac(device.MAC)}-climate"
self._attr_min_temp = float(static.min_temp.value) self._attr_min_temp = float(data.static.min_temp.value)
self._attr_max_temp = float(static.max_temp.value) self._attr_max_temp = float(data.static.max_temp.value)
# check if self.coordinator.data.current_temperature.unit is "°C" or "°C" if data.static.min_temp.unit in ("°C", "°C"):
if static.min_temp.unit in ("°C", "°C"):
self._attr_temperature_unit = UnitOfTemperature.CELSIUS self._attr_temperature_unit = UnitOfTemperature.CELSIUS
else: else:
self._attr_temperature_unit = UnitOfTemperature.FAHRENHEIT self._attr_temperature_unit = UnitOfTemperature.FAHRENHEIT
@ -111,30 +92,30 @@ class BSBLANClimate(
@property @property
def current_temperature(self) -> float | None: def current_temperature(self) -> float | None:
"""Return the current temperature.""" """Return the current temperature."""
if self.coordinator.data.current_temperature.value == "---": if self.coordinator.data.state.current_temperature.value == "---":
# device returns no current temperature # device returns no current temperature
return None return None
return float(self.coordinator.data.current_temperature.value) return float(self.coordinator.data.state.current_temperature.value)
@property @property
def target_temperature(self) -> float | None: def target_temperature(self) -> float | None:
"""Return the temperature we try to reach.""" """Return the temperature we try to reach."""
return float(self.coordinator.data.target_temperature.value) return float(self.coordinator.data.state.target_temperature.value)
@property @property
def hvac_mode(self) -> HVACMode | None: def hvac_mode(self) -> HVACMode | None:
"""Return hvac operation ie. heat, cool mode.""" """Return hvac operation ie. heat, cool mode."""
if self.coordinator.data.hvac_mode.value == PRESET_ECO: if self.coordinator.data.state.hvac_mode.value == PRESET_ECO:
return HVACMode.AUTO return HVACMode.AUTO
return try_parse_enum(HVACMode, self.coordinator.data.hvac_mode.value) return try_parse_enum(HVACMode, self.coordinator.data.state.hvac_mode.value)
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
"""Return the current preset mode.""" """Return the current preset mode."""
if ( if (
self.hvac_mode == HVACMode.AUTO self.hvac_mode == HVACMode.AUTO
and self.coordinator.data.hvac_mode.value == PRESET_ECO and self.coordinator.data.state.hvac_mode.value == PRESET_ECO
): ):
return PRESET_ECO return PRESET_ECO
return PRESET_NONE return PRESET_NONE
@ -173,7 +154,7 @@ class BSBLANClimate(
else: else:
data[ATTR_HVAC_MODE] = kwargs[ATTR_PRESET_MODE] data[ATTR_HVAC_MODE] = kwargs[ATTR_PRESET_MODE]
try: try:
await self.client.thermostat(**data) await self.coordinator.client.thermostat(**data)
except BSBLANError as err: except BSBLANError as err:
raise HomeAssistantError( raise HomeAssistantError(
"An error occurred while updating the BSBLAN device", "An error occurred while updating the BSBLAN device",

View File

@ -1,12 +1,10 @@
"""DataUpdateCoordinator for the BSB-Lan integration.""" """DataUpdateCoordinator for the BSB-Lan integration."""
from __future__ import annotations from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from random import randint from random import randint
from bsblan import BSBLAN, BSBLANConnectionError from bsblan import BSBLAN, BSBLANConnectionError, State
from bsblan.models import State
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST
@ -16,7 +14,14 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DOMAIN, LOGGER, SCAN_INTERVAL from .const import DOMAIN, LOGGER, SCAN_INTERVAL
class BSBLanUpdateCoordinator(DataUpdateCoordinator[State]): @dataclass
class BSBLanCoordinatorData:
"""BSBLan data stored in the Home Assistant data object."""
state: State
class BSBLanUpdateCoordinator(DataUpdateCoordinator[BSBLanCoordinatorData]):
"""The BSB-Lan update coordinator.""" """The BSB-Lan update coordinator."""
config_entry: ConfigEntry config_entry: ConfigEntry
@ -28,30 +33,32 @@ class BSBLanUpdateCoordinator(DataUpdateCoordinator[State]):
client: BSBLAN, client: BSBLAN,
) -> None: ) -> None:
"""Initialize the BSB-Lan coordinator.""" """Initialize the BSB-Lan coordinator."""
self.client = client
super().__init__( super().__init__(
hass, hass,
LOGGER, logger=LOGGER,
name=f"{DOMAIN}_{config_entry.data[CONF_HOST]}", name=f"{DOMAIN}_{config_entry.data[CONF_HOST]}",
# use the default scan interval and add a random number of seconds to avoid timeouts when update_interval=self._get_update_interval(),
# the BSB-Lan device is already/still busy retrieving data,
# e.g. for MQTT or internal logging.
update_interval=SCAN_INTERVAL + timedelta(seconds=randint(1, 8)),
) )
self.client = client
async def _async_update_data(self) -> State: def _get_update_interval(self) -> timedelta:
"""Get state from BSB-Lan device.""" """Get the update interval with a random offset.
# use the default scan interval and add a random number of seconds to avoid timeouts when Use the default scan interval and add a random number of seconds to avoid timeouts when
# the BSB-Lan device is already/still busy retrieving data, e.g. for MQTT or internal logging. the BSB-Lan device is already/still busy retrieving data,
self.update_interval = SCAN_INTERVAL + timedelta(seconds=randint(1, 8)) e.g. for MQTT or internal logging.
"""
return SCAN_INTERVAL + timedelta(seconds=randint(1, 8))
async def _async_update_data(self) -> BSBLanCoordinatorData:
"""Get state and sensor data from BSB-Lan device."""
try: try:
return await self.client.state() state = await self.client.state()
except BSBLANConnectionError as err: except BSBLANConnectionError as err:
host = self.config_entry.data[CONF_HOST] if self.config_entry else "unknown"
raise UpdateFailed( raise UpdateFailed(
f"Error while establishing connection with " f"Error while establishing connection with BSB-Lan device at {host}"
f"BSB-Lan device at {self.config_entry.data[CONF_HOST]}"
) from err ) from err
self.update_interval = self._get_update_interval()
return BSBLanCoordinatorData(state=state)

View File

@ -7,7 +7,7 @@ from typing import Any
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import HomeAssistantBSBLANData from . import BSBLanData
from .const import DOMAIN from .const import DOMAIN
@ -15,9 +15,10 @@ async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
data: HomeAssistantBSBLANData = hass.data[DOMAIN][entry.entry_id] data: BSBLanData = hass.data[DOMAIN][entry.entry_id]
return { return {
"info": data.info.to_dict(), "info": data.info.to_dict(),
"device": data.device.to_dict(), "device": data.device.to_dict(),
"state": data.coordinator.data.to_dict(), "state": data.coordinator.data.state.to_dict(),
} }

View File

@ -1,41 +1,35 @@
"""Base entity for the BSBLAN integration.""" """BSBLan base entity."""
from __future__ import annotations from __future__ import annotations
from bsblan import BSBLAN, Device, Info, StaticState
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.helpers.device_registry import ( from homeassistant.helpers.device_registry import (
CONNECTION_NETWORK_MAC, CONNECTION_NETWORK_MAC,
DeviceInfo, DeviceInfo,
format_mac, format_mac,
) )
from homeassistant.helpers.entity import Entity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import BSBLanData
from .const import DOMAIN from .const import DOMAIN
from .coordinator import BSBLanUpdateCoordinator
class BSBLANEntity(Entity): class BSBLanEntity(CoordinatorEntity[BSBLanUpdateCoordinator]):
"""Defines a BSBLAN entity.""" """Defines a base BSBLan entity."""
def __init__( _attr_has_entity_name = True
self,
client: BSBLAN,
device: Device,
info: Info,
static: StaticState,
entry: ConfigEntry,
) -> None:
"""Initialize an BSBLAN entity."""
self.client = client
def __init__(self, coordinator: BSBLanUpdateCoordinator, data: BSBLanData) -> None:
"""Initialize BSBLan entity."""
super().__init__(coordinator, data)
host = self.coordinator.config_entry.data["host"]
mac = self.coordinator.config_entry.data["mac"]
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
connections={(CONNECTION_NETWORK_MAC, format_mac(device.MAC))}, identifiers={(DOMAIN, data.device.MAC)},
identifiers={(DOMAIN, format_mac(device.MAC))}, connections={(CONNECTION_NETWORK_MAC, format_mac(mac))},
name=data.device.name,
manufacturer="BSBLAN Inc.", manufacturer="BSBLAN Inc.",
model=info.device_identification.value, model=data.info.device_identification.value,
name=device.name, sw_version=data.device.version,
sw_version=f"{device.version})", configuration_url=f"http://{host}",
configuration_url=f"http://{entry.data[CONF_HOST]}",
) )

View File

@ -3,7 +3,7 @@
from collections.abc import Generator from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from bsblan import Device, Info, State from bsblan import Device, Info, State, StaticState
import pytest import pytest
from homeassistant.components.bsblan.const import CONF_PASSKEY, DOMAIN from homeassistant.components.bsblan.const import CONF_PASSKEY, DOMAIN
@ -42,7 +42,6 @@ def mock_setup_entry() -> Generator[AsyncMock]:
@pytest.fixture @pytest.fixture
def mock_bsblan() -> Generator[MagicMock]: def mock_bsblan() -> Generator[MagicMock]:
"""Return a mocked BSBLAN client.""" """Return a mocked BSBLAN client."""
with ( with (
patch("homeassistant.components.bsblan.BSBLAN", autospec=True) as bsblan_mock, patch("homeassistant.components.bsblan.BSBLAN", autospec=True) as bsblan_mock,
patch("homeassistant.components.bsblan.config_flow.BSBLAN", new=bsblan_mock), patch("homeassistant.components.bsblan.config_flow.BSBLAN", new=bsblan_mock),
@ -53,6 +52,11 @@ def mock_bsblan() -> Generator[MagicMock]:
load_fixture("device.json", DOMAIN) load_fixture("device.json", DOMAIN)
) )
bsblan.state.return_value = State.from_json(load_fixture("state.json", DOMAIN)) bsblan.state.return_value = State.from_json(load_fixture("state.json", DOMAIN))
bsblan.static_values.return_value = StaticState.from_json(
load_fixture("static.json", DOMAIN)
)
yield bsblan yield bsblan

View File

@ -0,0 +1,20 @@
{
"min_temp": {
"name": "Room temp frost protection setpoint",
"error": 0,
"value": "8.0",
"desc": "",
"dataType": 0,
"readonly": 0,
"unit": "°C"
},
"max_temp": {
"name": "Summer/winter changeover temp heat circuit 1",
"error": 0,
"value": "20.0",
"desc": "",
"dataType": 0,
"readonly": 0,
"unit": "°C"
}
}

View File

@ -16,8 +16,7 @@ async def test_diagnostics(
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics.""" """Test diagnostics."""
diagnostics_data = await get_diagnostics_for_config_entry(
assert ( hass, hass_client, init_integration
await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
== snapshot
) )
assert diagnostics_data == snapshot