Add BaseEntity for apsystems integration (#117514)

* Add BaseEntity for apsystems integration

* Exclude entity.py from apsystems from coverage

* Remove api from BaseEntity from apsystems as it is not yet used

* Split BaseEntity and BaseCoordinatorEntity in apsystems integration

* Clean up of asserting unique_id everywhere in apsystems integration

* Remove BaseCoordinatorEntity from apsystems

* Remove double type declaration originating from merge in apsystems
This commit is contained in:
Marlon 2024-06-03 07:28:13 +02:00 committed by GitHub
parent 78e5f9578c
commit c23ec96174
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 21 deletions

View File

@ -90,6 +90,7 @@ omit =
homeassistant/components/aprilaire/entity.py homeassistant/components/aprilaire/entity.py
homeassistant/components/apsystems/__init__.py homeassistant/components/apsystems/__init__.py
homeassistant/components/apsystems/coordinator.py homeassistant/components/apsystems/coordinator.py
homeassistant/components/apsystems/entity.py
homeassistant/components/apsystems/sensor.py homeassistant/components/apsystems/sensor.py
homeassistant/components/aqualogic/* homeassistant/components/aqualogic/*
homeassistant/components/aquostv/media_player.py homeassistant/components/aquostv/media_player.py

View File

@ -2,6 +2,8 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from APsystemsEZ1 import APsystemsEZ1M from APsystemsEZ1 import APsystemsEZ1M
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -12,15 +14,27 @@ from .coordinator import ApSystemsDataCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
type ApsystemsConfigEntry = ConfigEntry[ApSystemsDataCoordinator]
@dataclass
class ApSystemsData:
"""Store runtime data."""
coordinator: ApSystemsDataCoordinator
device_id: str
async def async_setup_entry(hass: HomeAssistant, entry: ApsystemsConfigEntry) -> bool: type ApSystemsConfigEntry = ConfigEntry[ApSystemsData]
async def async_setup_entry(hass: HomeAssistant, entry: ApSystemsConfigEntry) -> bool:
"""Set up this integration using UI.""" """Set up this integration using UI."""
api = APsystemsEZ1M(ip_address=entry.data[CONF_IP_ADDRESS], timeout=8) api = APsystemsEZ1M(ip_address=entry.data[CONF_IP_ADDRESS], timeout=8)
coordinator = ApSystemsDataCoordinator(hass, api) coordinator = ApSystemsDataCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator assert entry.unique_id
entry.runtime_data = ApSystemsData(
coordinator=coordinator, device_id=entry.unique_id
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True return True

View File

@ -0,0 +1,27 @@
"""APsystems base entity."""
from __future__ import annotations
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
from . import ApSystemsData
from .const import DOMAIN
class ApSystemsEntity(Entity):
"""Defines a base APsystems entity."""
_attr_has_entity_name = True
def __init__(
self,
data: ApSystemsData,
) -> None:
"""Initialize the APsystems entity."""
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, data.device_id)},
serial_number=data.device_id,
manufacturer="APsystems",
model="EZ1-M",
)

View File

@ -14,16 +14,15 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
StateType, StateType,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfEnergy, UnitOfPower from homeassistant.const import UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from . import ApSystemsConfigEntry, ApSystemsData
from .coordinator import ApSystemsDataCoordinator from .coordinator import ApSystemsDataCoordinator
from .entity import ApSystemsEntity
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
@ -111,22 +110,24 @@ SENSORS: tuple[ApsystemsLocalApiSensorDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ApSystemsConfigEntry,
add_entities: AddEntitiesCallback, add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the sensor platform.""" """Set up the sensor platform."""
config = config_entry.runtime_data config = config_entry.runtime_data
device_id = config_entry.unique_id
assert device_id
add_entities( add_entities(
ApSystemsSensorWithDescription(config, desc, device_id) for desc in SENSORS ApSystemsSensorWithDescription(
data=config,
entity_description=desc,
)
for desc in SENSORS
) )
class ApSystemsSensorWithDescription( class ApSystemsSensorWithDescription(
CoordinatorEntity[ApSystemsDataCoordinator], SensorEntity CoordinatorEntity[ApSystemsDataCoordinator], ApSystemsEntity, SensorEntity
): ):
"""Base sensor to be used with description.""" """Base sensor to be used with description."""
@ -134,20 +135,14 @@ class ApSystemsSensorWithDescription(
def __init__( def __init__(
self, self,
coordinator: ApSystemsDataCoordinator, data: ApSystemsData,
entity_description: ApsystemsLocalApiSensorDescription, entity_description: ApsystemsLocalApiSensorDescription,
device_id: str,
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator) super().__init__(data.coordinator)
ApSystemsEntity.__init__(self, data)
self.entity_description = entity_description self.entity_description = entity_description
self._attr_unique_id = f"{device_id}_{entity_description.key}" self._attr_unique_id = f"{data.device_id}_{entity_description.key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_id)},
serial_number=device_id,
manufacturer="APsystems",
model="EZ1-M",
)
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType: