From 5389db5660f086e7a0826e4edecd5ebcbda5a3f0 Mon Sep 17 00:00:00 2001 From: tobiby Date: Wed, 15 May 2024 15:19:45 +0200 Subject: [PATCH 1/6] Implement Sensor for SmartGrid Status --- custom_components/e3dc_rscp/coordinator.py | 36 +++++++++++++++++++ custom_components/e3dc_rscp/e3dc_proxy.py | 7 +++- custom_components/e3dc_rscp/sensor.py | 8 +++++ custom_components/e3dc_rscp/strings.json | 3 ++ .../e3dc_rscp/translations/en.json | 3 ++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/custom_components/e3dc_rscp/coordinator.py b/custom_components/e3dc_rscp/coordinator.py index af66648..22bc6dd 100644 --- a/custom_components/e3dc_rscp/coordinator.py +++ b/custom_components/e3dc_rscp/coordinator.py @@ -142,6 +142,7 @@ async def _async_update_data(self) -> dict[str, Any]: _LOGGER.debug("Polling general status information") await self._load_and_process_poll() + await self._load_and_process_poll_switches() # TODO: Check if we need to replace this with a safe IPC sync if self._update_guard_powersettings is False: @@ -213,6 +214,41 @@ async def _load_and_process_poll(self): self._mydata["solar-production"] = poll_data["production"]["solar"] self._mydata["wallbox-consumption"] = poll_data["consumption"]["wallbox"] + async def _load_and_process_poll_switches(self): + """Load and process switches poll data.""" + try: + poll_data: list[dict] = await self.hass.async_add_executor_job(self.proxy.poll_switches) + except HomeAssistantError as ex: + _LOGGER.warning("Failed to poll switches, not updating data: %s", ex) + return + + sg0_status=False + sg1_status=False + for switch in poll_data: + if switch["name"] == "SG0": + #48 seems to be the value for switch off + if switch["status"] == 48: + sg0_status = False + else: + sg0_status = True + if switch["name"] == "SG1": + #48 seems to be the value for switch off + if switch["status"] == 48: + sg1_status = False + else: + sg1_status = True + + if sg0_status is True and sg1_status is False: + self._mydata["smartgrid"] = "blocked" + elif sg0_status is False and sg1_status is False: + self._mydata["smartgrid"] = "normal" + elif sg0_status is False and sg1_status is True: + self._mydata["smartgrid"] = "recommendation_on" + elif sg0_status is True and sg1_status is True: + self._mydata["smartgrid"] = "startup" + else: + self._mydata["smartgrid"] = "Normal" + async def _load_and_process_db_data_today(self) -> None: """Load and process retrieved db data settings.""" try: diff --git a/custom_components/e3dc_rscp/e3dc_proxy.py b/custom_components/e3dc_rscp/e3dc_proxy.py index 7b90ee0..f012686 100644 --- a/custom_components/e3dc_rscp/e3dc_proxy.py +++ b/custom_components/e3dc_rscp/e3dc_proxy.py @@ -214,6 +214,11 @@ def poll(self) -> dict[str, Any]: """Poll E3DC current state.""" return self.e3dc.poll(keepAlive=True) + @e3dc_call + def poll_switches(self) -> dict[str, Any]: + """Poll E3DC switches.""" + return self.e3dc.poll_switches(keepAlive=True) + @e3dc_call def start_manual_charge(self, charge_amount_wh: int) -> None: """Initiate the manual charging process, zero will stop charging.""" @@ -264,4 +269,4 @@ def set_weather_regulated_charge(self, enabled: bool): # unsuccessful, the next polling cycle will reset this to the actual # value. # TODO: Find a way to deal with the weather regulation api - self.e3dc.set_weather_regulated_charge(enabled, True) + self.e3dc.set_weather_regulated_charge(enabled, True) \ No newline at end of file diff --git a/custom_components/e3dc_rscp/sensor.py b/custom_components/e3dc_rscp/sensor.py index da563ab..022d192 100644 --- a/custom_components/e3dc_rscp/sensor.py +++ b/custom_components/e3dc_rscp/sensor.py @@ -372,6 +372,14 @@ device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, ), + SensorEntityDescription( + key="smartgrid", + translation_key="smartgrid", + icon="mdi:solar-power", + device_class=SensorDeviceClass.ENUM, + state_class=SensorStateClass.MEASUREMENT, + options=["blocked", "normal", "recommendation_on", "startup"], + ), ) diff --git a/custom_components/e3dc_rscp/strings.json b/custom_components/e3dc_rscp/strings.json index 3fe65db..4e40e50 100644 --- a/custom_components/e3dc_rscp/strings.json +++ b/custom_components/e3dc_rscp/strings.json @@ -193,6 +193,9 @@ }, "farm-additional-total": { "name": "Farm additional powermeter - total" + }, + "smartgrid": { + "name": "SmartGrid Status" } }, "switch": { diff --git a/custom_components/e3dc_rscp/translations/en.json b/custom_components/e3dc_rscp/translations/en.json index 80350bf..106e99c 100644 --- a/custom_components/e3dc_rscp/translations/en.json +++ b/custom_components/e3dc_rscp/translations/en.json @@ -193,6 +193,9 @@ }, "farm-additional-total": { "name": "Farm additional powermeter - total" + }, + "smartgrid": { + "name": "SmartGrid Status" } }, "switch": { From 6b5aafdb0c67b78c00bc4a146b14e198278652a5 Mon Sep 17 00:00:00 2001 From: tobiby Date: Thu, 16 May 2024 21:36:36 +0200 Subject: [PATCH 2/6] Remove state class to have the statistic available --- custom_components/e3dc_rscp/sensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/e3dc_rscp/sensor.py b/custom_components/e3dc_rscp/sensor.py index 022d192..e8a9a21 100644 --- a/custom_components/e3dc_rscp/sensor.py +++ b/custom_components/e3dc_rscp/sensor.py @@ -377,7 +377,6 @@ translation_key="smartgrid", icon="mdi:solar-power", device_class=SensorDeviceClass.ENUM, - state_class=SensorStateClass.MEASUREMENT, options=["blocked", "normal", "recommendation_on", "startup"], ), ) From d75c2a4960a6968a5f1ef222d60418323ca9a65e Mon Sep 17 00:00:00 2001 From: tobiby Date: Mon, 27 May 2024 11:33:01 +0200 Subject: [PATCH 3/6] Fixed fallback state --- custom_components/e3dc_rscp/coordinator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/e3dc_rscp/coordinator.py b/custom_components/e3dc_rscp/coordinator.py index 22bc6dd..8f6724e 100644 --- a/custom_components/e3dc_rscp/coordinator.py +++ b/custom_components/e3dc_rscp/coordinator.py @@ -247,7 +247,7 @@ async def _load_and_process_poll_switches(self): elif sg0_status is True and sg1_status is True: self._mydata["smartgrid"] = "startup" else: - self._mydata["smartgrid"] = "Normal" + self._mydata["smartgrid"] = "normal" async def _load_and_process_db_data_today(self) -> None: """Load and process retrieved db data settings.""" From 0f5558e4ce018b2747a1c3eaf6e2db2db4356f92 Mon Sep 17 00:00:00 2001 From: tobiby Date: Sun, 4 Aug 2024 23:32:24 +0200 Subject: [PATCH 4/6] Update translations --- custom_components/e3dc_rscp/translations/en.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/e3dc_rscp/translations/en.json b/custom_components/e3dc_rscp/translations/en.json index 106e99c..f84bf53 100644 --- a/custom_components/e3dc_rscp/translations/en.json +++ b/custom_components/e3dc_rscp/translations/en.json @@ -195,7 +195,11 @@ "name": "Farm additional powermeter - total" }, "smartgrid": { - "name": "SmartGrid Status" + "name": "SmartGrid Status", + "blocked": "blocked mode", + "normal": "normal operation", + "recommendation_on": "PV surplus operation", + "startup": "Operation for derating" } }, "switch": { @@ -251,4 +255,4 @@ } } } -} \ No newline at end of file +} From 5af577c0dd58c9f1e0b000aa22d3edfbfbbbc938 Mon Sep 17 00:00:00 2001 From: tobiby Date: Sun, 4 Aug 2024 23:33:37 +0200 Subject: [PATCH 5/6] Update translations --- custom_components/e3dc_rscp/strings.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/custom_components/e3dc_rscp/strings.json b/custom_components/e3dc_rscp/strings.json index 4e40e50..2752a65 100644 --- a/custom_components/e3dc_rscp/strings.json +++ b/custom_components/e3dc_rscp/strings.json @@ -195,7 +195,11 @@ "name": "Farm additional powermeter - total" }, "smartgrid": { - "name": "SmartGrid Status" + "name": "SmartGrid Status", + "blocked": "blocked mode", + "normal": "normal operation", + "recommendation_on": "PV surplus operation", + "startup": "Operation for derating" } }, "switch": { @@ -251,4 +255,4 @@ } } } -} \ No newline at end of file +} From 1629454431ebe1095713dfb4ae43ecf238db70ce Mon Sep 17 00:00:00 2001 From: Tobias Bystricky Date: Sun, 8 Sep 2024 21:48:25 +0200 Subject: [PATCH 6/6] Fixed wrong indentations --- custom_components/e3dc_rscp/coordinator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/e3dc_rscp/coordinator.py b/custom_components/e3dc_rscp/coordinator.py index c029131..62e7cdb 100644 --- a/custom_components/e3dc_rscp/coordinator.py +++ b/custom_components/e3dc_rscp/coordinator.py @@ -314,22 +314,22 @@ async def _load_and_process_poll_switches(self): except HomeAssistantError as ex: _LOGGER.warning("Failed to poll switches, not updating data: %s", ex) return - + sg0_status=False sg1_status=False for switch in poll_data: if switch["name"] == "SG0": #48 seems to be the value for switch off if switch["status"] == 48: - sg0_status = False + sg0_status = False else: - sg0_status = True + sg0_status = True if switch["name"] == "SG1": #48 seems to be the value for switch off if switch["status"] == 48: - sg1_status = False + sg1_status = False else: - sg1_status = True + sg1_status = True if sg0_status is True and sg1_status is False: self._mydata["smartgrid"] = "blocked"