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

@ -304,6 +304,7 @@ omit =
homeassistant/components/enphase_envoy/__init__.py
homeassistant/components/enphase_envoy/binary_sensor.py
homeassistant/components/enphase_envoy/coordinator.py
homeassistant/components/enphase_envoy/entity.py
homeassistant/components/enphase_envoy/sensor.py
homeassistant/components/entur_public_transport/*
homeassistant/components/environment_canada/__init__.py

View file

@ -3,13 +3,8 @@ from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import logging
from pyenphase import (
EnvoyData,
EnvoyEncharge,
EnvoyEnpower,
)
from pyenphase import EnvoyEncharge, EnvoyEnpower
from pyenphase.models.dry_contacts import DryContactStatus
from homeassistant.components.binary_sensor import (
@ -22,14 +17,10 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
)
from .const import DOMAIN
from .coordinator import EnphaseUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
from .entity import EnvoyBaseEntity
@dataclass
@ -143,32 +134,9 @@ async def async_setup_entry(
async_add_entities(entities)
class EnvoyBaseBinarySensorEntity(
CoordinatorEntity[EnphaseUpdateCoordinator], BinarySensorEntity
):
class EnvoyBaseBinarySensorEntity(EnvoyBaseEntity, BinarySensorEntity):
"""Defines a base envoy binary_sensor entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: EnphaseUpdateCoordinator,
description: BinarySensorEntityDescription,
) -> None:
"""Init the Enphase base binary_sensor 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 EnvoyEnchargeBinarySensorEntity(EnvoyBaseBinarySensorEntity):
"""Defines an Encharge binary_sensor entity."""

View file

@ -0,0 +1,34 @@
"""Support for Enphase Envoy solar energy monitor."""
from __future__ import annotations
from pyenphase import EnvoyData
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .coordinator import EnphaseUpdateCoordinator
class EnvoyBaseEntity(CoordinatorEntity[EnphaseUpdateCoordinator]):
"""Defines a base envoy entity."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: EnphaseUpdateCoordinator,
description: EntityDescription,
) -> None:
"""Init the Enphase 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

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__(