Refactor enphase_envoy to have a shared base class (#98088)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
J. Nick Koston 2023-08-09 01:28:27 -10:00 committed by GitHub
parent 2cae79415d
commit 7e9d0cca44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 71 deletions

View file

@ -7,7 +7,6 @@ import datetime
import logging
from pyenphase import (
EnvoyData,
EnvoyEncharge,
EnvoyEnchargePower,
EnvoyEnpower,
@ -33,13 +32,11 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
)
from homeassistant.util import dt as dt_util
from .const import DOMAIN
from .coordinator import EnphaseUpdateCoordinator
from .entity import EnvoyBaseEntity
ICON = "mdi:flash"
_LOGGER = logging.getLogger(__name__)
@ -340,34 +337,14 @@ async def async_setup_entry(
async_add_entities(entities)
class EnvoyBaseEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
class EnvoySensorBaseEntity(EnvoyBaseEntity, 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."""
class EnvoySystemSensorEntity(EnvoySensorBaseEntity):
"""Envoy system base entity."""
_attr_icon = ICON
_attr_has_entity_name = True
def __init__(
self,
@ -386,7 +363,7 @@ class EnvoyEntity(EnvoyBaseEntity, SensorEntity):
)
class EnvoyProductionEntity(EnvoyEntity):
class EnvoyProductionEntity(EnvoySystemSensorEntity):
"""Envoy production entity."""
entity_description: EnvoyProductionSensorEntityDescription
@ -399,7 +376,7 @@ class EnvoyProductionEntity(EnvoyEntity):
return self.entity_description.value_fn(system_production)
class EnvoyConsumptionEntity(EnvoyEntity):
class EnvoyConsumptionEntity(EnvoySystemSensorEntity):
"""Envoy consumption entity."""
entity_description: EnvoyConsumptionSensorEntityDescription
@ -412,11 +389,10 @@ class EnvoyConsumptionEntity(EnvoyEntity):
return self.entity_description.value_fn(system_consumption)
class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity):
class EnvoyInverterEntity(EnvoySensorBaseEntity):
"""Envoy inverter entity."""
_attr_icon = ICON
_attr_has_entity_name = True
entity_description: EnvoyInverterSensorEntityDescription
def __init__(
@ -453,11 +429,9 @@ class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity):
return self.entity_description.value_fn(inverters[self._serial_number])
class EnvoyEnchargeEntity(EnvoyBaseEntity, SensorEntity):
class EnvoyEnchargeEntity(EnvoySensorBaseEntity):
"""Envoy Encharge sensor entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: EnphaseUpdateCoordinator,
@ -507,10 +481,9 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
return self.entity_description.value_fn(encharge_power[self._serial_number])
class EnvoyEnpowerEntity(EnvoyBaseEntity, SensorEntity):
class EnvoyEnpowerEntity(EnvoySensorBaseEntity):
"""Envoy Enpower sensor entity."""
_attr_has_entity_name = True
entity_description: EnvoyEnpowerSensorEntityDescription
def __init__(