From 8912b19cf4c36278419e63b7263c858b640bbb0c Mon Sep 17 00:00:00 2001 From: Charles Garwood Date: Fri, 11 Aug 2023 20:15:42 -0400 Subject: [PATCH] Add Enphase Encharge aggregate sensors (#98276) * Add Encharge aggregate sensors * Update dependency --- .../components/enphase_envoy/manifest.json | 2 +- .../components/enphase_envoy/sensor.py | 71 +++++++++++++++++++ .../components/enphase_envoy/strings.json | 15 ++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/enphase_envoy/manifest.json b/homeassistant/components/enphase_envoy/manifest.json index f500ac538e7..6969dc3d6ab 100644 --- a/homeassistant/components/enphase_envoy/manifest.json +++ b/homeassistant/components/enphase_envoy/manifest.json @@ -6,7 +6,7 @@ "documentation": "https://www.home-assistant.io/integrations/enphase_envoy", "iot_class": "local_polling", "loggers": ["pyenphase"], - "requirements": ["pyenphase==1.4.0"], + "requirements": ["pyenphase==1.5.2"], "zeroconf": [ { "type": "_enphase-envoy._tcp.local." diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index 9ecc205522a..0e4a9b71232 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -8,6 +8,7 @@ import logging from pyenphase import ( EnvoyEncharge, + EnvoyEnchargeAggregate, EnvoyEnchargePower, EnvoyEnpower, EnvoyInverter, @@ -288,6 +289,58 @@ ENPOWER_SENSORS = ( ) +@dataclass +class EnvoyEnchargeAggregateRequiredKeysMixin: + """Mixin for required keys.""" + + value_fn: Callable[[EnvoyEnchargeAggregate], int] + + +@dataclass +class EnvoyEnchargeAggregateSensorEntityDescription( + SensorEntityDescription, EnvoyEnchargeAggregateRequiredKeysMixin +): + """Describes an Envoy Encharge sensor entity.""" + + +ENCHARGE_AGGREGATE_SENSORS = ( + EnvoyEnchargeAggregateSensorEntityDescription( + key="battery_level", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.BATTERY, + value_fn=lambda encharge: encharge.state_of_charge, + ), + EnvoyEnchargeAggregateSensorEntityDescription( + key="reserve_soc", + translation_key="reserve_soc", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.BATTERY, + value_fn=lambda encharge: encharge.reserve_state_of_charge, + ), + EnvoyEnchargeAggregateSensorEntityDescription( + key="available_energy", + translation_key="available_energy", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value_fn=lambda encharge: encharge.available_energy, + ), + EnvoyEnchargeAggregateSensorEntityDescription( + key="reserve_energy", + translation_key="reserve_energy", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value_fn=lambda encharge: encharge.backup_reserve, + ), + EnvoyEnchargeAggregateSensorEntityDescription( + key="max_capacity", + translation_key="max_capacity", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + value_fn=lambda encharge: encharge.max_available_capacity, + ), +) + + async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -329,6 +382,11 @@ async def async_setup_entry( for description in ENCHARGE_POWER_SENSORS for encharge in envoy_data.encharge_power ) + if envoy_data.encharge_aggregate: + entities.extend( + EnvoyEnchargeAggregateEntity(coordinator, description) + for description in ENCHARGE_AGGREGATE_SENSORS + ) if envoy_data.enpower: entities.extend( EnvoyEnpowerEntity(coordinator, description) @@ -482,6 +540,19 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity): return self.entity_description.value_fn(encharge_power[self._serial_number]) +class EnvoyEnchargeAggregateEntity(EnvoySystemSensorEntity): + """Envoy Encharge Aggregate sensor entity.""" + + entity_description: EnvoyEnchargeAggregateSensorEntityDescription + + @property + def native_value(self) -> int: + """Return the state of the aggregate sensors.""" + encharge_aggregate = self.data.encharge_aggregate + assert encharge_aggregate is not None + return self.entity_description.value_fn(encharge_aggregate) + + class EnvoyEnpowerEntity(EnvoySensorBaseEntity): """Envoy Enpower sensor entity.""" diff --git a/homeassistant/components/enphase_envoy/strings.json b/homeassistant/components/enphase_envoy/strings.json index f42e44d7afa..2afd19d87d1 100644 --- a/homeassistant/components/enphase_envoy/strings.json +++ b/homeassistant/components/enphase_envoy/strings.json @@ -63,6 +63,21 @@ }, "lifetime_consumption": { "name": "Lifetime energy consumption" + }, + "reserve_soc": { + "name": "Reserve battery level" + }, + "available_energy": { + "name": "Available battery energy" + }, + "reserve_energy": { + "name": "Reserve battery energy" + }, + "max_capacity": { + "name": "Battery capacity" + }, + "configured_reserve_soc": { + "name": "Configured reserve battery level" } }, "switch": { diff --git a/requirements_all.txt b/requirements_all.txt index 4f99f44c9f1..8eae7bc175f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1665,7 +1665,7 @@ pyedimax==0.2.1 pyefergy==22.1.1 # homeassistant.components.enphase_envoy -pyenphase==1.4.0 +pyenphase==1.5.2 # homeassistant.components.envisalink pyenvisalink==4.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index cc1bc38b233..7048126d3a8 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1229,7 +1229,7 @@ pyeconet==0.1.20 pyefergy==22.1.1 # homeassistant.components.enphase_envoy -pyenphase==1.4.0 +pyenphase==1.5.2 # homeassistant.components.everlights pyeverlights==0.1.0