From 7e9d0cca449b73fc94671d2bd2670cfe996a3c00 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 9 Aug 2023 01:28:27 -1000 Subject: [PATCH] Refactor enphase_envoy to have a shared base class (#98088) Co-authored-by: Joost Lekkerkerker --- .coveragerc | 1 + .../components/enphase_envoy/binary_sensor.py | 38 ++-------------- .../components/enphase_envoy/entity.py | 34 ++++++++++++++ .../components/enphase_envoy/sensor.py | 45 ++++--------------- 4 files changed, 47 insertions(+), 71 deletions(-) create mode 100644 homeassistant/components/enphase_envoy/entity.py diff --git a/.coveragerc b/.coveragerc index cb9ca19c5b2..6aa0c8cce06 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/enphase_envoy/binary_sensor.py b/homeassistant/components/enphase_envoy/binary_sensor.py index 4e893050b16..42778aff9d6 100644 --- a/homeassistant/components/enphase_envoy/binary_sensor.py +++ b/homeassistant/components/enphase_envoy/binary_sensor.py @@ -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.""" diff --git a/homeassistant/components/enphase_envoy/entity.py b/homeassistant/components/enphase_envoy/entity.py new file mode 100644 index 00000000000..16669bcd098 --- /dev/null +++ b/homeassistant/components/enphase_envoy/entity.py @@ -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 diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index 019af1d393e..2b2dd591faa 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -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__(