diff --git a/.coveragerc b/.coveragerc index 4f839ffccdd..625057e9900 100644 --- a/.coveragerc +++ b/.coveragerc @@ -90,6 +90,7 @@ omit = homeassistant/components/aprilaire/entity.py homeassistant/components/apsystems/__init__.py homeassistant/components/apsystems/coordinator.py + homeassistant/components/apsystems/entity.py homeassistant/components/apsystems/sensor.py homeassistant/components/aqualogic/* homeassistant/components/aquostv/media_player.py diff --git a/homeassistant/components/apsystems/__init__.py b/homeassistant/components/apsystems/__init__.py index 1a103244d5b..0231d2975d8 100644 --- a/homeassistant/components/apsystems/__init__.py +++ b/homeassistant/components/apsystems/__init__.py @@ -2,6 +2,8 @@ from __future__ import annotations +from dataclasses import dataclass + from APsystemsEZ1 import APsystemsEZ1M from homeassistant.config_entries import ConfigEntry @@ -12,15 +14,27 @@ from .coordinator import ApSystemsDataCoordinator 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.""" api = APsystemsEZ1M(ip_address=entry.data[CONF_IP_ADDRESS], timeout=8) coordinator = ApSystemsDataCoordinator(hass, api) 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) return True diff --git a/homeassistant/components/apsystems/entity.py b/homeassistant/components/apsystems/entity.py new file mode 100644 index 00000000000..519f4fffb61 --- /dev/null +++ b/homeassistant/components/apsystems/entity.py @@ -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", + ) diff --git a/homeassistant/components/apsystems/sensor.py b/homeassistant/components/apsystems/sensor.py index 5321498d1b6..fdfe7d0f0b7 100644 --- a/homeassistant/components/apsystems/sensor.py +++ b/homeassistant/components/apsystems/sensor.py @@ -14,16 +14,15 @@ from homeassistant.components.sensor import ( SensorStateClass, StateType, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfEnergy, UnitOfPower from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import DiscoveryInfoType from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import DOMAIN +from . import ApSystemsConfigEntry, ApSystemsData from .coordinator import ApSystemsDataCoordinator +from .entity import ApSystemsEntity @dataclass(frozen=True, kw_only=True) @@ -111,22 +110,24 @@ SENSORS: tuple[ApsystemsLocalApiSensorDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: ApSystemsConfigEntry, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the sensor platform.""" config = config_entry.runtime_data - device_id = config_entry.unique_id - assert device_id add_entities( - ApSystemsSensorWithDescription(config, desc, device_id) for desc in SENSORS + ApSystemsSensorWithDescription( + data=config, + entity_description=desc, + ) + for desc in SENSORS ) class ApSystemsSensorWithDescription( - CoordinatorEntity[ApSystemsDataCoordinator], SensorEntity + CoordinatorEntity[ApSystemsDataCoordinator], ApSystemsEntity, SensorEntity ): """Base sensor to be used with description.""" @@ -134,20 +135,14 @@ class ApSystemsSensorWithDescription( def __init__( self, - coordinator: ApSystemsDataCoordinator, + data: ApSystemsData, entity_description: ApsystemsLocalApiSensorDescription, - device_id: str, ) -> None: """Initialize the sensor.""" - super().__init__(coordinator) + super().__init__(data.coordinator) + ApSystemsEntity.__init__(self, data) self.entity_description = entity_description - self._attr_unique_id = f"{device_id}_{entity_description.key}" - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, device_id)}, - serial_number=device_id, - manufacturer="APsystems", - model="EZ1-M", - ) + self._attr_unique_id = f"{data.device_id}_{entity_description.key}" @property def native_value(self) -> StateType: