Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sensor for SmartGrid Status #147

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions custom_components/e3dc_rscp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion custom_components/e3dc_rscp/e3dc_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)
7 changes: 7 additions & 0 deletions custom_components/e3dc_rscp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@
device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="smartgrid",
translation_key="smartgrid",
icon="mdi:solar-power",
device_class=SensorDeviceClass.ENUM,
options=["blocked", "normal", "recommendation_on", "startup"],
torbennehmer marked this conversation as resolved.
Show resolved Hide resolved
),
)


Expand Down
3 changes: 3 additions & 0 deletions custom_components/e3dc_rscp/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
},
"farm-additional-total": {
"name": "Farm additional powermeter - total"
},
"smartgrid": {
"name": "SmartGrid Status"
}
},
"switch": {
Expand Down
3 changes: 3 additions & 0 deletions custom_components/e3dc_rscp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
},
"farm-additional-total": {
"name": "Farm additional powermeter - total"
},
"smartgrid": {
"name": "SmartGrid Status"
}
},
"switch": {
Expand Down