Skip to content

Commit

Permalink
Grid Status On Off binary sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
WillCodeForCats committed Jul 7, 2024
1 parent 8f28a63 commit b63f5ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions custom_components/solaredge_modbus_multi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ async def async_setup_entry(
if hub.option_detect_extras and inverter.advanced_power_control:
entities.append(AdvPowerControlEnabled(inverter, config_entry, coordinator))

if hub.option_detect_extras:
entities.append(GridStatusOnOff(inverter, config_entry, coordinator))

if entities:
async_add_entities(entities)

Expand Down Expand Up @@ -92,3 +95,31 @@ def name(self) -> str:
@property
def is_on(self) -> bool:
return self._platform.decoded_model["AdvPwrCtrlEn"] == 0x1


class GridStatusOnOff(SolarEdgeBinarySensorBase):
"""Grid Status On Off. This is undocumented from discussions."""

icon = "mdi:transmission-tower"

@property
def available(self) -> bool:
return (
super().available and "grid_on_off" in self._platform.decoded_model.keys()
)

@property
def unique_id(self) -> str:
return f"{self._platform.uid_base}_grid_status_on_off"

@property
def name(self) -> str:
return "Grid Power Available"

@property
def entity_registry_enabled_default(self) -> bool:
return "grid_on_off" in self._platform.decoded_model.keys()

@property
def is_on(self) -> bool:
return self._platform.decoded_model["grid_on_off"]
41 changes: 41 additions & 0 deletions custom_components/solaredge_modbus_multi/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ def __init__(self, device_id: int, hub: SolarEdgeModbusMultiHub) -> None:
self.global_power_control = None
self.advanced_power_control = None
self.site_limit_control = None
self._grid_on_off = None

async def init_device(self) -> None:
"""Set up data about the device from modbus."""
Expand Down Expand Up @@ -1300,6 +1301,46 @@ async def read_modbus_data(self) -> None:
f"No response from inverter ID {self.inverter_unit_id}"
)

""" Grid On/Off Status """
if self.hub.option_detect_extras is True and self._grid_on_off is not False:
# 42162 as a Uint32 is the correct register to indicate ON/OFF grid.
try:
inverter_data = await self.hub.modbus_read_holding_registers(
unit=self.inverter_unit_id, address=42162, rcount=2
)

decoder = BinaryPayloadDecoder.fromRegisters(
inverter_data.registers,
byteorder=Endian.BIG,
wordorder=Endian.LITTLE,
)

self.decoded_model.update(
OrderedDict(
[
("grid_on_off", decoder.decode_32bit_uint()),
]
)
)
self._grid_status = True

except ModbusIllegalAddress:
try:
del self.decoded_model["grid_on_off"]
except KeyError:
pass

self._grid_status = False

_LOGGER.debug(
(f"I{self.inverter_unit_id}: " "Grid On/Off NOT available")
)

except ModbusIOError:
raise ModbusReadError(
f"No response from inverter ID {self.inverter_unit_id}"
)

for name, value in iter(self.decoded_model.items()):
if isinstance(value, float):
display_value = float_to_hex(value)
Expand Down

0 comments on commit b63f5ce

Please sign in to comment.