2019-04-03 17:40:03 +02:00
|
|
|
"""Support for Enphase Envoy solar energy monitor."""
|
2021-08-06 14:59:00 +02:00
|
|
|
from __future__ import annotations
|
2020-12-28 14:58:09 -08:00
|
|
|
|
2022-03-29 17:25:14 -10:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import datetime
|
|
|
|
import logging
|
2023-08-05 13:33:16 -10:00
|
|
|
|
|
|
|
from pyenphase import EnvoyInverter, EnvoySystemConsumption, EnvoySystemProduction
|
2022-03-29 17:25:14 -10:00
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2022-01-03 19:19:11 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-07-22 16:59:15 +02:00
|
|
|
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
2022-01-03 19:19:11 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-05 13:33:16 -10:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
2022-01-03 19:19:11 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-03-29 17:25:14 -10:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.util import dt as dt_util
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import EnphaseUpdateCoordinator
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
ICON = "mdi:flash"
|
2022-03-29 17:25:14 -10:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
INVERTERS_KEY = "inverters"
|
|
|
|
LAST_REPORTED_KEY = "last_reported"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2023-08-05 13:33:16 -10:00
|
|
|
class EnvoyInverterRequiredKeysMixin:
|
2022-03-29 17:25:14 -10:00
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
value_fn: Callable[[EnvoyInverter], datetime.datetime | float]
|
2022-03-29 17:25:14 -10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2023-08-05 13:33:16 -10:00
|
|
|
class EnvoyInverterSensorEntityDescription(
|
|
|
|
SensorEntityDescription, EnvoyInverterRequiredKeysMixin
|
|
|
|
):
|
2022-03-29 17:25:14 -10:00
|
|
|
"""Describes an Envoy inverter sensor entity."""
|
|
|
|
|
|
|
|
|
|
|
|
INVERTER_SENSORS = (
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyInverterSensorEntityDescription(
|
2022-03-29 17:25:14 -10:00
|
|
|
key=INVERTERS_KEY,
|
2023-08-06 02:32:35 +02:00
|
|
|
name=None,
|
2022-12-12 11:49:02 +01:00
|
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
2022-03-29 17:25:14 -10:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2022-12-12 11:49:02 +01:00
|
|
|
device_class=SensorDeviceClass.POWER,
|
2023-08-05 13:33:16 -10:00
|
|
|
value_fn=lambda inverter: inverter.last_report_watts,
|
2022-03-29 17:25:14 -10:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyInverterSensorEntityDescription(
|
2022-03-29 17:25:14 -10:00
|
|
|
key=LAST_REPORTED_KEY,
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key=LAST_REPORTED_KEY,
|
2022-03-29 17:25:14 -10:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
entity_registry_enabled_default=False,
|
2023-08-05 13:33:16 -10:00
|
|
|
value_fn=lambda inverter: dt_util.utc_from_timestamp(inverter.last_report_date),
|
2022-03-29 17:25:14 -10:00
|
|
|
),
|
|
|
|
)
|
2020-12-28 14:58:09 -08:00
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EnvoyProductionRequiredKeysMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[EnvoySystemProduction], int]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EnvoyProductionSensorEntityDescription(
|
|
|
|
SensorEntityDescription, EnvoyProductionRequiredKeysMixin
|
|
|
|
):
|
|
|
|
"""Describes an Envoy production sensor entity."""
|
|
|
|
|
|
|
|
|
|
|
|
PRODUCTION_SENSORS = (
|
|
|
|
EnvoyProductionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="production",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="current_power_production",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfPower.KILO_WATT,
|
|
|
|
suggested_display_precision=3,
|
|
|
|
value_fn=lambda production: production.watts_now,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyProductionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="daily_production",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="daily_production",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
suggested_display_precision=2,
|
|
|
|
value_fn=lambda production: production.watt_hours_today,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyProductionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="seven_days_production",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="seven_days_production",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
suggested_display_precision=1,
|
|
|
|
value_fn=lambda production: production.watt_hours_last_7_days,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyProductionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="lifetime_production",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="lifetime_production",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR,
|
|
|
|
suggested_display_precision=3,
|
|
|
|
value_fn=lambda production: production.watt_hours_lifetime,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EnvoyConsumptionRequiredKeysMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[EnvoySystemConsumption], int]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class EnvoyConsumptionSensorEntityDescription(
|
|
|
|
SensorEntityDescription, EnvoyConsumptionRequiredKeysMixin
|
|
|
|
):
|
|
|
|
"""Describes an Envoy consumption sensor entity."""
|
|
|
|
|
|
|
|
|
|
|
|
CONSUMPTION_SENSORS = (
|
|
|
|
EnvoyConsumptionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="consumption",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="current_power_consumption",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfPower.KILO_WATT,
|
|
|
|
suggested_display_precision=3,
|
|
|
|
value_fn=lambda consumption: consumption.watts_now,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyConsumptionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="daily_consumption",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="daily_consumption",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
suggested_display_precision=2,
|
|
|
|
value_fn=lambda consumption: consumption.watt_hours_today,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyConsumptionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="seven_days_consumption",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="seven_days_consumption",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
|
|
|
|
suggested_display_precision=1,
|
|
|
|
value_fn=lambda consumption: consumption.watt_hours_last_7_days,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyConsumptionSensorEntityDescription(
|
2023-07-22 16:59:15 +02:00
|
|
|
key="lifetime_consumption",
|
2023-08-06 02:32:35 +02:00
|
|
|
translation_key="lifetime_consumption",
|
2023-07-22 16:59:15 +02:00
|
|
|
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2023-08-05 13:33:16 -10:00
|
|
|
suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR,
|
|
|
|
suggested_display_precision=3,
|
|
|
|
value_fn=lambda consumption: consumption.watt_hours_lifetime,
|
2023-07-22 16:59:15 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-12-28 14:58:09 -08:00
|
|
|
|
2022-01-03 19:19:11 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-04-04 22:11:06 -10:00
|
|
|
"""Set up envoy sensor platform."""
|
2023-08-05 13:33:16 -10:00
|
|
|
coordinator: EnphaseUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
envoy_data = coordinator.envoy.data
|
|
|
|
assert envoy_data is not None
|
2022-03-29 17:25:14 -10:00
|
|
|
envoy_serial_num = config_entry.unique_id
|
|
|
|
assert envoy_serial_num is not None
|
|
|
|
_LOGGER.debug("Envoy data: %s", envoy_data)
|
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
entities: list[Entity] = [
|
|
|
|
EnvoyProductionEntity(coordinator, description)
|
|
|
|
for description in PRODUCTION_SENSORS
|
|
|
|
]
|
|
|
|
if envoy_data.system_consumption:
|
|
|
|
entities.extend(
|
|
|
|
EnvoyConsumptionEntity(coordinator, description)
|
|
|
|
for description in CONSUMPTION_SENSORS
|
2022-03-29 17:25:14 -10:00
|
|
|
)
|
2023-08-05 13:33:16 -10:00
|
|
|
if envoy_data.inverters:
|
2022-03-29 17:25:14 -10:00
|
|
|
entities.extend(
|
2023-08-05 13:33:16 -10:00
|
|
|
EnvoyInverterEntity(coordinator, description, inverter)
|
2022-03-29 17:25:14 -10:00
|
|
|
for description in INVERTER_SENSORS
|
2023-08-05 13:33:16 -10:00
|
|
|
for inverter in envoy_data.inverters
|
2022-03-29 17:25:14 -10:00
|
|
|
)
|
2020-12-28 14:58:09 -08:00
|
|
|
|
2019-07-08 09:21:08 -05:00
|
|
|
async_add_entities(entities)
|
2018-08-02 16:14:43 -05:00
|
|
|
|
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
class EnvoyEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
2022-03-29 17:25:14 -10:00
|
|
|
"""Envoy inverter entity."""
|
|
|
|
|
|
|
|
_attr_icon = ICON
|
2023-08-06 02:32:35 +02:00
|
|
|
_attr_has_entity_name = True
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2021-04-04 22:11:06 -10:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-08-05 13:33:16 -10:00
|
|
|
coordinator: EnphaseUpdateCoordinator,
|
2022-03-29 17:25:14 -10:00
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2020-12-28 14:58:09 -08:00
|
|
|
"""Initialize Envoy entity."""
|
2021-07-28 17:31:10 -07:00
|
|
|
self.entity_description = description
|
2023-08-05 13:33:16 -10:00
|
|
|
envoy_name = coordinator.name
|
|
|
|
envoy_serial_num = coordinator.envoy.serial_number
|
|
|
|
assert envoy_serial_num is not None
|
2022-03-29 17:25:14 -10:00
|
|
|
self._attr_unique_id = f"{envoy_serial_num}_{description.key}"
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, envoy_serial_num)},
|
|
|
|
manufacturer="Enphase",
|
2023-08-06 16:55:34 -10:00
|
|
|
model=coordinator.envoy.part_number or "Envoy",
|
2022-03-29 17:25:14 -10:00
|
|
|
name=envoy_name,
|
2023-08-05 13:33:16 -10:00
|
|
|
sw_version=str(coordinator.envoy.firmware),
|
2022-03-29 17:25:14 -10:00
|
|
|
)
|
2020-12-28 14:58:09 -08:00
|
|
|
super().__init__(coordinator)
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
|
|
|
|
class EnvoyProductionEntity(EnvoyEntity):
|
|
|
|
"""Envoy production entity."""
|
|
|
|
|
|
|
|
entity_description: EnvoyProductionSensorEntityDescription
|
|
|
|
|
2018-08-02 16:14:43 -05:00
|
|
|
@property
|
2023-08-05 13:33:16 -10:00
|
|
|
def native_value(self) -> int | None:
|
2018-08-02 16:14:43 -05:00
|
|
|
"""Return the state of the sensor."""
|
2023-08-05 13:33:16 -10:00
|
|
|
envoy = self.coordinator.envoy
|
|
|
|
assert envoy.data is not None
|
|
|
|
assert envoy.data.system_production is not None
|
|
|
|
return self.entity_description.value_fn(envoy.data.system_production)
|
2020-12-28 14:58:09 -08:00
|
|
|
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2023-08-05 13:33:16 -10:00
|
|
|
class EnvoyConsumptionEntity(EnvoyEntity):
|
|
|
|
"""Envoy consumption entity."""
|
|
|
|
|
|
|
|
entity_description: EnvoyConsumptionSensorEntityDescription
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> int | None:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
envoy = self.coordinator.envoy
|
|
|
|
assert envoy.data is not None
|
|
|
|
assert envoy.data.system_consumption is not None
|
|
|
|
return self.entity_description.value_fn(envoy.data.system_consumption)
|
|
|
|
|
|
|
|
|
|
|
|
class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity):
|
2022-03-29 17:25:14 -10:00
|
|
|
"""Envoy inverter entity."""
|
2018-08-02 16:14:43 -05:00
|
|
|
|
2022-03-29 17:25:14 -10:00
|
|
|
_attr_icon = ICON
|
2023-08-06 02:32:35 +02:00
|
|
|
_attr_has_entity_name = True
|
2023-08-05 13:33:16 -10:00
|
|
|
entity_description: EnvoyInverterSensorEntityDescription
|
2019-12-17 16:51:19 -08:00
|
|
|
|
2022-03-29 17:25:14 -10:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-08-05 13:33:16 -10:00
|
|
|
coordinator: EnphaseUpdateCoordinator,
|
|
|
|
description: EnvoyInverterSensorEntityDescription,
|
2022-03-29 17:25:14 -10:00
|
|
|
serial_number: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize Envoy inverter entity."""
|
|
|
|
self.entity_description = description
|
|
|
|
self._serial_number = serial_number
|
2023-08-05 13:33:16 -10:00
|
|
|
key = description.key
|
|
|
|
|
|
|
|
if key == INVERTERS_KEY:
|
|
|
|
# Originally there was only one inverter sensor, so we don't want to
|
2023-08-06 02:32:35 +02:00
|
|
|
# break existing installations by changing the unique_id.
|
2022-03-29 17:25:14 -10:00
|
|
|
self._attr_unique_id = serial_number
|
|
|
|
else:
|
2023-08-06 02:32:35 +02:00
|
|
|
# Additional sensors have a unique_id that includes the
|
2023-08-05 13:33:16 -10:00
|
|
|
# sensor key.
|
|
|
|
self._attr_unique_id = f"{serial_number}_{key}"
|
|
|
|
|
|
|
|
envoy_serial_num = coordinator.envoy.serial_number
|
|
|
|
assert envoy_serial_num is not None
|
2022-03-29 17:25:14 -10:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, serial_number)},
|
|
|
|
name=f"Inverter {serial_number}",
|
2021-10-23 05:44:51 -04:00
|
|
|
manufacturer="Enphase",
|
2022-03-29 17:25:14 -10:00
|
|
|
model="Inverter",
|
|
|
|
via_device=(DOMAIN, envoy_serial_num),
|
2021-10-23 05:44:51 -04:00
|
|
|
)
|
2022-03-29 17:25:14 -10:00
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
@property
|
2023-08-05 13:33:16 -10:00
|
|
|
def native_value(self) -> datetime.datetime | float:
|
2022-03-29 17:25:14 -10:00
|
|
|
"""Return the state of the sensor."""
|
2023-08-05 13:33:16 -10:00
|
|
|
envoy = self.coordinator.envoy
|
|
|
|
assert envoy.data is not None
|
|
|
|
assert envoy.data.inverters is not None
|
|
|
|
inverter = envoy.data.inverters[self._serial_number]
|
|
|
|
return self.entity_description.value_fn(inverter)
|