Add Envoy enpower sensors (#98086)
This commit is contained in:
parent
d975e93abc
commit
ce6b759b70
6 changed files with 225 additions and 21 deletions
|
@ -10,6 +10,7 @@ from pyenphase import (
|
|||
EnvoyData,
|
||||
EnvoyEncharge,
|
||||
EnvoyEnchargePower,
|
||||
EnvoyEnpower,
|
||||
EnvoyInverter,
|
||||
EnvoySystemConsumption,
|
||||
EnvoySystemProduction,
|
||||
|
@ -259,6 +260,36 @@ ENCHARGE_POWER_SENSORS = (
|
|||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class EnvoyEnpowerRequiredKeysMixin:
|
||||
"""Mixin for required keys."""
|
||||
|
||||
value_fn: Callable[[EnvoyEnpower], datetime.datetime | int | float]
|
||||
|
||||
|
||||
@dataclass
|
||||
class EnvoyEnpowerSensorEntityDescription(
|
||||
SensorEntityDescription, EnvoyEnpowerRequiredKeysMixin
|
||||
):
|
||||
"""Describes an Envoy Encharge sensor entity."""
|
||||
|
||||
|
||||
ENPOWER_SENSORS = (
|
||||
EnvoyEnpowerSensorEntityDescription(
|
||||
key="temperature",
|
||||
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
value_fn=lambda enpower: enpower.temperature,
|
||||
),
|
||||
EnvoyEnpowerSensorEntityDescription(
|
||||
key=LAST_REPORTED_KEY,
|
||||
translation_key=LAST_REPORTED_KEY,
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
value_fn=lambda enpower: dt_util.utc_from_timestamp(enpower.last_report_date),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
|
@ -300,6 +331,11 @@ async def async_setup_entry(
|
|||
for description in ENCHARGE_POWER_SENSORS
|
||||
for encharge in envoy_data.encharge_power
|
||||
)
|
||||
if envoy_data.enpower:
|
||||
entities.extend(
|
||||
EnvoyEnpowerEntity(coordinator, description)
|
||||
for description in ENPOWER_SENSORS
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
@ -469,3 +505,38 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
|
|||
encharge_power = self.data.encharge_power
|
||||
assert encharge_power is not None
|
||||
return self.entity_description.value_fn(encharge_power[self._serial_number])
|
||||
|
||||
|
||||
class EnvoyEnpowerEntity(EnvoyBaseEntity, SensorEntity):
|
||||
"""Envoy Enpower sensor entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
entity_description: EnvoyEnpowerSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: EnphaseUpdateCoordinator,
|
||||
description: EnvoyEnpowerSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize Enpower entity."""
|
||||
super().__init__(coordinator, description)
|
||||
assert coordinator.envoy.data is not None
|
||||
enpower_data = coordinator.envoy.data.enpower
|
||||
assert enpower_data is not None
|
||||
self._serial_number = enpower_data.serial_number
|
||||
self._attr_unique_id = f"{self._serial_number}_{description.key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, self._serial_number)},
|
||||
manufacturer="Enphase",
|
||||
model="Enpower",
|
||||
name=f"Enpower {self._serial_number}",
|
||||
sw_version=str(enpower_data.firmware_version),
|
||||
via_device=(DOMAIN, self.envoy_serial_num),
|
||||
)
|
||||
|
||||
@property
|
||||
def native_value(self) -> datetime.datetime | int | float | None:
|
||||
"""Return the state of the power sensors."""
|
||||
enpower = self.data.enpower
|
||||
assert enpower is not None
|
||||
return self.entity_description.value_fn(enpower)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue