Marlon d4d30f1c46
Add integration for APsystems EZ1 microinverter (#114531)
* Add APsystems local API integration

* Fix session usage in config_flow in apsystems local api

* Remove skip check option for apsystems_loca api

* Update APsystems API dependency and increased test coverage to 100%

* Utilize EntityDescriptions for APsystems Local integration

* Ensure coverage entries are sorted (#114424)

* Ensure coverage entries are sorted

* Use autofix

* Adjust

* Add comment to coverage file

* test CI

* revert CI test

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Use patch instead of Http Mocks for APsystems API tests

* Fix linter waring for apsystemsapi

* Fix apsystemsapi test

* Fix CODEOWNERS for apsystemsapi

* Address small PR review changes for apsystems_local

* Remove wrong lines in coveragerc

* Add serial number for apsystems_local

* Remove option of custom refresh interval fro apsystems_local

* Remove function override and fix stale comments

* Use native device id and name storage instead of custom one for apsystems_local

* Use runtime_data for apsystems_local

* Don't store entry data in runtime data

* Move from apsystems_local to apsystems domain

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
2024-05-14 22:50:25 -04:00

166 lines
5.7 KiB
Python

"""The read-only sensors for APsystems local API integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from APsystemsEZ1 import ReturnOutputData
from homeassistant import config_entries
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant, callback
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 .coordinator import ApSystemsDataCoordinator
@dataclass(frozen=True, kw_only=True)
class ApsystemsLocalApiSensorDescription(SensorEntityDescription):
"""Describes Apsystens Inverter sensor entity."""
value_fn: Callable[[ReturnOutputData], float | None]
SENSORS: tuple[ApsystemsLocalApiSensorDescription, ...] = (
ApsystemsLocalApiSensorDescription(
key="total_power",
translation_key="total_power",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda c: c.p1 + c.p2,
),
ApsystemsLocalApiSensorDescription(
key="total_power_p1",
translation_key="total_power_p1",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda c: c.p1,
),
ApsystemsLocalApiSensorDescription(
key="total_power_p2",
translation_key="total_power_p2",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda c: c.p2,
),
ApsystemsLocalApiSensorDescription(
key="lifetime_production",
translation_key="lifetime_production",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.te1 + c.te2,
),
ApsystemsLocalApiSensorDescription(
key="lifetime_production_p1",
translation_key="lifetime_production_p1",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.te1,
),
ApsystemsLocalApiSensorDescription(
key="lifetime_production_p2",
translation_key="lifetime_production_p2",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.te2,
),
ApsystemsLocalApiSensorDescription(
key="today_production",
translation_key="today_production",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.e1 + c.e2,
),
ApsystemsLocalApiSensorDescription(
key="today_production_p1",
translation_key="today_production_p1",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.e1,
),
ApsystemsLocalApiSensorDescription(
key="today_production_p2",
translation_key="today_production_p2",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda c: c.e2,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: config_entries.ConfigEntry,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
config = config_entry.runtime_data
coordinator = config["COORDINATOR"]
device_name = config_entry.title
device_id: str = config_entry.unique_id # type: ignore[assignment]
add_entities(
ApSystemsSensorWithDescription(coordinator, desc, device_name, device_id)
for desc in SENSORS
)
class ApSystemsSensorWithDescription(CoordinatorEntity, SensorEntity):
"""Base sensor to be used with description."""
entity_description: ApsystemsLocalApiSensorDescription
def __init__(
self,
coordinator: ApSystemsDataCoordinator,
entity_description: ApsystemsLocalApiSensorDescription,
device_name: str,
device_id: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.entity_description = entity_description
self._device_name = device_name
self._device_id = device_id
self._attr_unique_id = f"{device_id}_{entity_description.key}"
@property
def device_info(self) -> DeviceInfo:
"""Get the DeviceInfo."""
return DeviceInfo(
identifiers={("apsystems", self._device_id)},
name=self._device_name,
serial_number=self._device_id,
manufacturer="APsystems",
model="EZ1-M",
)
@callback
def _handle_coordinator_update(self) -> None:
if self.coordinator.data is None:
return # type: ignore[unreachable]
self._attr_native_value = self.entity_description.value_fn(
self.coordinator.data
)
self.async_write_ha_state()