Refactor enphase_envoy to have a shared base class (#98088)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
2cae79415d
commit
7e9d0cca44
4 changed files with 47 additions and 71 deletions
|
@ -304,6 +304,7 @@ omit =
|
||||||
homeassistant/components/enphase_envoy/__init__.py
|
homeassistant/components/enphase_envoy/__init__.py
|
||||||
homeassistant/components/enphase_envoy/binary_sensor.py
|
homeassistant/components/enphase_envoy/binary_sensor.py
|
||||||
homeassistant/components/enphase_envoy/coordinator.py
|
homeassistant/components/enphase_envoy/coordinator.py
|
||||||
|
homeassistant/components/enphase_envoy/entity.py
|
||||||
homeassistant/components/enphase_envoy/sensor.py
|
homeassistant/components/enphase_envoy/sensor.py
|
||||||
homeassistant/components/entur_public_transport/*
|
homeassistant/components/entur_public_transport/*
|
||||||
homeassistant/components/environment_canada/__init__.py
|
homeassistant/components/environment_canada/__init__.py
|
||||||
|
|
|
@ -3,13 +3,8 @@ from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import logging
|
|
||||||
|
|
||||||
from pyenphase import (
|
from pyenphase import EnvoyEncharge, EnvoyEnpower
|
||||||
EnvoyData,
|
|
||||||
EnvoyEncharge,
|
|
||||||
EnvoyEnpower,
|
|
||||||
)
|
|
||||||
from pyenphase.models.dry_contacts import DryContactStatus
|
from pyenphase.models.dry_contacts import DryContactStatus
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
|
@ -22,14 +17,10 @@ from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
|
||||||
CoordinatorEntity,
|
|
||||||
)
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import EnphaseUpdateCoordinator
|
from .coordinator import EnphaseUpdateCoordinator
|
||||||
|
from .entity import EnvoyBaseEntity
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -143,32 +134,9 @@ async def async_setup_entry(
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyBaseBinarySensorEntity(
|
class EnvoyBaseBinarySensorEntity(EnvoyBaseEntity, BinarySensorEntity):
|
||||||
CoordinatorEntity[EnphaseUpdateCoordinator], BinarySensorEntity
|
|
||||||
):
|
|
||||||
"""Defines a base envoy binary_sensor entity."""
|
"""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):
|
class EnvoyEnchargeBinarySensorEntity(EnvoyBaseBinarySensorEntity):
|
||||||
"""Defines an Encharge binary_sensor entity."""
|
"""Defines an Encharge binary_sensor entity."""
|
||||||
|
|
34
homeassistant/components/enphase_envoy/entity.py
Normal file
34
homeassistant/components/enphase_envoy/entity.py
Normal 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
|
|
@ -7,7 +7,6 @@ import datetime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from pyenphase import (
|
from pyenphase import (
|
||||||
EnvoyData,
|
|
||||||
EnvoyEncharge,
|
EnvoyEncharge,
|
||||||
EnvoyEnchargePower,
|
EnvoyEnchargePower,
|
||||||
EnvoyEnpower,
|
EnvoyEnpower,
|
||||||
|
@ -33,13 +32,11 @@ from homeassistant.const import (
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
|
||||||
CoordinatorEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import EnphaseUpdateCoordinator
|
from .coordinator import EnphaseUpdateCoordinator
|
||||||
|
from .entity import EnvoyBaseEntity
|
||||||
|
|
||||||
ICON = "mdi:flash"
|
ICON = "mdi:flash"
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -340,34 +337,14 @@ async def async_setup_entry(
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyBaseEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
class EnvoySensorBaseEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
"""Defines a base envoy entity."""
|
"""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
|
class EnvoySystemSensorEntity(EnvoySensorBaseEntity):
|
||||||
def data(self) -> EnvoyData:
|
"""Envoy system base entity."""
|
||||||
"""Return envoy data."""
|
|
||||||
data = self.coordinator.envoy.data
|
|
||||||
assert data is not None
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEntity(EnvoyBaseEntity, SensorEntity):
|
|
||||||
"""Envoy inverter entity."""
|
|
||||||
|
|
||||||
_attr_icon = ICON
|
_attr_icon = ICON
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
@ -386,7 +363,7 @@ class EnvoyEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyProductionEntity(EnvoyEntity):
|
class EnvoyProductionEntity(EnvoySystemSensorEntity):
|
||||||
"""Envoy production entity."""
|
"""Envoy production entity."""
|
||||||
|
|
||||||
entity_description: EnvoyProductionSensorEntityDescription
|
entity_description: EnvoyProductionSensorEntityDescription
|
||||||
|
@ -399,7 +376,7 @@ class EnvoyProductionEntity(EnvoyEntity):
|
||||||
return self.entity_description.value_fn(system_production)
|
return self.entity_description.value_fn(system_production)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyConsumptionEntity(EnvoyEntity):
|
class EnvoyConsumptionEntity(EnvoySystemSensorEntity):
|
||||||
"""Envoy consumption entity."""
|
"""Envoy consumption entity."""
|
||||||
|
|
||||||
entity_description: EnvoyConsumptionSensorEntityDescription
|
entity_description: EnvoyConsumptionSensorEntityDescription
|
||||||
|
@ -412,11 +389,10 @@ class EnvoyConsumptionEntity(EnvoyEntity):
|
||||||
return self.entity_description.value_fn(system_consumption)
|
return self.entity_description.value_fn(system_consumption)
|
||||||
|
|
||||||
|
|
||||||
class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity):
|
class EnvoyInverterEntity(EnvoySensorBaseEntity):
|
||||||
"""Envoy inverter entity."""
|
"""Envoy inverter entity."""
|
||||||
|
|
||||||
_attr_icon = ICON
|
_attr_icon = ICON
|
||||||
_attr_has_entity_name = True
|
|
||||||
entity_description: EnvoyInverterSensorEntityDescription
|
entity_description: EnvoyInverterSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -453,11 +429,9 @@ class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity):
|
||||||
return self.entity_description.value_fn(inverters[self._serial_number])
|
return self.entity_description.value_fn(inverters[self._serial_number])
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEnchargeEntity(EnvoyBaseEntity, SensorEntity):
|
class EnvoyEnchargeEntity(EnvoySensorBaseEntity):
|
||||||
"""Envoy Encharge sensor entity."""
|
"""Envoy Encharge sensor entity."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: EnphaseUpdateCoordinator,
|
coordinator: EnphaseUpdateCoordinator,
|
||||||
|
@ -507,10 +481,9 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity):
|
||||||
return self.entity_description.value_fn(encharge_power[self._serial_number])
|
return self.entity_description.value_fn(encharge_power[self._serial_number])
|
||||||
|
|
||||||
|
|
||||||
class EnvoyEnpowerEntity(EnvoyBaseEntity, SensorEntity):
|
class EnvoyEnpowerEntity(EnvoySensorBaseEntity):
|
||||||
"""Envoy Enpower sensor entity."""
|
"""Envoy Enpower sensor entity."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
entity_description: EnvoyEnpowerSensorEntityDescription
|
entity_description: EnvoyEnpowerSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue