Cleanup enphase_envoy sensor inheritance (#98012)
This commit is contained in:
parent
128dadafae
commit
2a80a63ac2
1 changed files with 53 additions and 51 deletions
|
@ -7,6 +7,7 @@ import datetime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pyenphase import (
|
from pyenphase import (
|
||||||
|
EnvoyData,
|
||||||
EnvoyEncharge,
|
EnvoyEncharge,
|
||||||
EnvoyEnchargePower,
|
EnvoyEnchargePower,
|
||||||
EnvoyInverter,
|
EnvoyInverter,
|
||||||
|
@ -303,7 +304,30 @@ async def async_setup_entry(
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
class EnvoyBaseEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
||||||
|
"""Defines a base envoy entity."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: EnphaseUpdateCoordinator,
|
||||||
|
description: SensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Init the envoy base entity."""
|
||||||
|
self.entity_description = description
|
||||||
|
serial_number = coordinator.envoy.serial_number
|
||||||
|
assert serial_number is not None
|
||||||
|
self.envoy_serial_num = serial_number
|
||||||
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def data(self) -> EnvoyData:
|
||||||
|
"""Return envoy data."""
|
||||||
|
data = self.coordinator.envoy.data
|
||||||
|
assert data is not None
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
class EnvoyEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
"""Envoy inverter entity."""
|
"""Envoy inverter entity."""
|
||||||
|
|
||||||
_attr_icon = ICON
|
_attr_icon = ICON
|
||||||
|
@ -315,19 +339,15 @@ class EnvoyEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
||||||
description: SensorEntityDescription,
|
description: SensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Envoy entity."""
|
"""Initialize Envoy entity."""
|
||||||
self.entity_description = description
|
super().__init__(coordinator, description)
|
||||||
envoy_name = coordinator.name
|
self._attr_unique_id = f"{self.envoy_serial_num}_{description.key}"
|
||||||
envoy_serial_num = coordinator.envoy.serial_number
|
|
||||||
assert envoy_serial_num is not None
|
|
||||||
self._attr_unique_id = f"{envoy_serial_num}_{description.key}"
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, envoy_serial_num)},
|
identifiers={(DOMAIN, self.envoy_serial_num)},
|
||||||
manufacturer="Enphase",
|
manufacturer="Enphase",
|
||||||
model=coordinator.envoy.part_number or "Envoy",
|
model=coordinator.envoy.part_number or "Envoy",
|
||||||
name=envoy_name,
|
name=coordinator.name,
|
||||||
sw_version=str(coordinator.envoy.firmware),
|
sw_version=str(coordinator.envoy.firmware),
|
||||||
)
|
)
|
||||||
super().__init__(coordinator)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyProductionEntity(EnvoyEntity):
|
class EnvoyProductionEntity(EnvoyEntity):
|
||||||
|
@ -338,10 +358,9 @@ class EnvoyProductionEntity(EnvoyEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> int | None:
|
def native_value(self) -> int | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
envoy = self.coordinator.envoy
|
system_production = self.data.system_production
|
||||||
assert envoy.data is not None
|
assert system_production is not None
|
||||||
assert envoy.data.system_production is not None
|
return self.entity_description.value_fn(system_production)
|
||||||
return self.entity_description.value_fn(envoy.data.system_production)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyConsumptionEntity(EnvoyEntity):
|
class EnvoyConsumptionEntity(EnvoyEntity):
|
||||||
|
@ -352,13 +371,12 @@ class EnvoyConsumptionEntity(EnvoyEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> int | None:
|
def native_value(self) -> int | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
envoy = self.coordinator.envoy
|
system_consumption = self.data.system_consumption
|
||||||
assert envoy.data is not None
|
assert system_consumption is not None
|
||||||
assert envoy.data.system_consumption is not None
|
return self.entity_description.value_fn(system_consumption)
|
||||||
return self.entity_description.value_fn(envoy.data.system_consumption)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
"""Envoy inverter entity."""
|
"""Envoy inverter entity."""
|
||||||
|
|
||||||
_attr_icon = ICON
|
_attr_icon = ICON
|
||||||
|
@ -372,10 +390,9 @@ class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt
|
||||||
serial_number: str,
|
serial_number: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Envoy inverter entity."""
|
"""Initialize Envoy inverter entity."""
|
||||||
self.entity_description = description
|
super().__init__(coordinator, description)
|
||||||
self._serial_number = serial_number
|
self._serial_number = serial_number
|
||||||
key = description.key
|
key = description.key
|
||||||
|
|
||||||
if key == INVERTERS_KEY:
|
if key == INVERTERS_KEY:
|
||||||
# Originally there was only one inverter sensor, so we don't want to
|
# Originally there was only one inverter sensor, so we don't want to
|
||||||
# break existing installations by changing the unique_id.
|
# break existing installations by changing the unique_id.
|
||||||
|
@ -384,32 +401,25 @@ class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt
|
||||||
# Additional sensors have a unique_id that includes the
|
# Additional sensors have a unique_id that includes the
|
||||||
# sensor key.
|
# sensor key.
|
||||||
self._attr_unique_id = f"{serial_number}_{key}"
|
self._attr_unique_id = f"{serial_number}_{key}"
|
||||||
|
|
||||||
envoy_serial_num = coordinator.envoy.serial_number
|
|
||||||
assert envoy_serial_num is not None
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, serial_number)},
|
identifiers={(DOMAIN, serial_number)},
|
||||||
name=f"Inverter {serial_number}",
|
name=f"Inverter {serial_number}",
|
||||||
manufacturer="Enphase",
|
manufacturer="Enphase",
|
||||||
model="Inverter",
|
model="Inverter",
|
||||||
via_device=(DOMAIN, envoy_serial_num),
|
via_device=(DOMAIN, self.envoy_serial_num),
|
||||||
)
|
)
|
||||||
super().__init__(coordinator)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> datetime.datetime | float:
|
def native_value(self) -> datetime.datetime | float:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
envoy = self.coordinator.envoy
|
inverters = self.data.inverters
|
||||||
assert envoy.data is not None
|
assert inverters is not None
|
||||||
assert envoy.data.inverters is not None
|
return self.entity_description.value_fn(inverters[self._serial_number])
|
||||||
inverter = envoy.data.inverters[self._serial_number]
|
|
||||||
return self.entity_description.value_fn(inverter)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEnchargeEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
class EnvoyEnchargeEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
"""Envoy Encharge sensor entity."""
|
"""Envoy Encharge sensor entity."""
|
||||||
|
|
||||||
entity_description: EnvoyEnchargeSensorEntityDescription | EnvoyEnchargePowerSensorEntityDescription
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -420,23 +430,19 @@ class EnvoyEnchargeEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt
|
||||||
serial_number: str,
|
serial_number: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Encharge entity."""
|
"""Initialize Encharge entity."""
|
||||||
self.entity_description = description
|
super().__init__(coordinator, description)
|
||||||
self._serial_number = serial_number
|
self._serial_number = serial_number
|
||||||
envoy_serial_num = coordinator.envoy.serial_number
|
|
||||||
assert envoy_serial_num is not None
|
|
||||||
self._attr_unique_id = f"{serial_number}_{description.key}"
|
self._attr_unique_id = f"{serial_number}_{description.key}"
|
||||||
assert coordinator.envoy.data is not None
|
encharge_inventory = self.data.encharge_inventory
|
||||||
assert coordinator.envoy.data.encharge_inventory is not None
|
assert encharge_inventory is not None
|
||||||
encharge = coordinator.envoy.data.encharge_inventory[self._serial_number]
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, serial_number)},
|
identifiers={(DOMAIN, serial_number)},
|
||||||
manufacturer="Enphase",
|
manufacturer="Enphase",
|
||||||
model="Encharge",
|
model="Encharge",
|
||||||
name=f"Encharge {serial_number}",
|
name=f"Encharge {serial_number}",
|
||||||
sw_version=str(encharge.firmware_version),
|
sw_version=str(encharge_inventory[self._serial_number].firmware_version),
|
||||||
via_device=(DOMAIN, envoy_serial_num),
|
via_device=(DOMAIN, self.envoy_serial_num),
|
||||||
)
|
)
|
||||||
super().__init__(coordinator)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEnchargeInventoryEntity(EnvoyEnchargeEntity):
|
class EnvoyEnchargeInventoryEntity(EnvoyEnchargeEntity):
|
||||||
|
@ -447,11 +453,9 @@ class EnvoyEnchargeInventoryEntity(EnvoyEnchargeEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> int | float | datetime.datetime | None:
|
def native_value(self) -> int | float | datetime.datetime | None:
|
||||||
"""Return the state of the inventory sensors."""
|
"""Return the state of the inventory sensors."""
|
||||||
envoy = self.coordinator.envoy
|
encharge_inventory = self.data.encharge_inventory
|
||||||
assert envoy.data is not None
|
assert encharge_inventory is not None
|
||||||
assert envoy.data.encharge_inventory is not None
|
return self.entity_description.value_fn(encharge_inventory[self._serial_number])
|
||||||
encharge = envoy.data.encharge_inventory[self._serial_number]
|
|
||||||
return self.entity_description.value_fn(encharge)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
|
class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
|
||||||
|
@ -462,8 +466,6 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> int | float | None:
|
def native_value(self) -> int | float | None:
|
||||||
"""Return the state of the power sensors."""
|
"""Return the state of the power sensors."""
|
||||||
envoy = self.coordinator.envoy
|
encharge_power = self.data.encharge_power
|
||||||
assert envoy.data is not None
|
assert encharge_power is not None
|
||||||
assert envoy.data.encharge_power is not None
|
return self.entity_description.value_fn(encharge_power[self._serial_number])
|
||||||
encharge = envoy.data.encharge_power[self._serial_number]
|
|
||||||
return self.entity_description.value_fn(encharge)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue