From f37fe3d34312ca2647b552487bb486ef51a89979 Mon Sep 17 00:00:00 2001
From: Pierre-E Mercier
Date: Wed, 3 May 2023 09:22:58 +0200
Subject: [PATCH] feat: green energy
---
.../mylight_systems/coordinator.py | 4 ++++
custom_components/mylight_systems/sensor.py | 19 +++++++++++++++----
2 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/custom_components/mylight_systems/coordinator.py b/custom_components/mylight_systems/coordinator.py
index 11e399b..01e4f19 100644
--- a/custom_components/mylight_systems/coordinator.py
+++ b/custom_components/mylight_systems/coordinator.py
@@ -40,6 +40,7 @@ class MyLightSystemsCoordinatorData(NamedTuple):
self_conso: Measure
msb_charge: Measure
msb_discharge: Measure
+ green_energy: Measure
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities
@@ -94,6 +95,9 @@ async def _async_update_data(self) -> MyLightSystemsCoordinatorData:
msb_discharge=self.find_measure_by_type(
result, "msb_discharge"
),
+ green_energy=self.find_measure_by_type(
+ result, "green_energy"
+ ),
)
except (
UnauthorizedException,
diff --git a/custom_components/mylight_systems/sensor.py b/custom_components/mylight_systems/sensor.py
index 6f51adf..dce4ce1 100644
--- a/custom_components/mylight_systems/sensor.py
+++ b/custom_components/mylight_systems/sensor.py
@@ -40,7 +40,7 @@ class MyLightSensorEntityDescription(
MYLIGHT_SENSORS: tuple[MyLightSensorEntityDescription, ...] = (
MyLightSensorEntityDescription(
key="total_solar_production",
- name="Total solar power production",
+ name="Solar power production",
icon="mdi:solar-panel",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
@@ -62,7 +62,7 @@ class MyLightSensorEntityDescription(
),
MyLightSensorEntityDescription(
key="total_grid_without_battery_consumption",
- name="Total power consumption from the grid without virtual battery",
+ name="Grid power consumption",
icon="mdi:transmission-tower",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
@@ -95,7 +95,7 @@ class MyLightSensorEntityDescription(
),
MyLightSensorEntityDescription(
key="total_msb_charge",
- name="Total My Smart Battery Charge",
+ name="Battery Charge",
icon="mdi:battery-high",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
@@ -106,7 +106,7 @@ class MyLightSensorEntityDescription(
),
MyLightSensorEntityDescription(
key="total_msb_discharge",
- name="Total My Smart Battery Discharge",
+ name="Battery Discharge",
icon="mdi:battery-low",
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
@@ -115,6 +115,17 @@ class MyLightSensorEntityDescription(
if data.msb_discharge is not None
else 0,
),
+ MyLightSensorEntityDescription(
+ key="total_green_energy",
+ name="Green energy",
+ icon="mdi:solar-panel",
+ native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
+ state_class=SensorStateClass.TOTAL_INCREASING,
+ device_class=SensorDeviceClass.ENERGY,
+ value_fn=lambda data: round(data.green_energy.value / 36e2, 2)
+ if data.green_energy is not None
+ else 0,
+ ),
)