Skip to content

Commit

Permalink
Merge pull request #197 from kumarunster/master
Browse files Browse the repository at this point in the history
upgrade pymodbus to 3.5.1 and homeassistant to 2023.09.0
  • Loading branch information
binsentsu authored Sep 12, 2023
2 parents 4c08aa5 + d5ed8be commit 9ce1801
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
39 changes: 30 additions & 9 deletions custom_components/solaredge_modbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ async def async_refresh_modbus_data(self, _now: Optional[int] = None) -> None:
if not self._sensors:
return

if not self._check_and_reconnect():
#if not connected, skip
return

try:
update_result = self.read_modbus_data()
except Exception as e:
_LOGGER.exception("Error reading modbus data")
_LOGGER.exception("Error reading modbus data", exc_info=True)
update_result = False

if update_result:
Expand All @@ -232,10 +236,27 @@ def close(self):
with self._lock:
self._client.close()

def _check_and_reconnect(self):
if not self._client.connected:
_LOGGER.info("modbus client is not connected, trying to reconnect")
return self.connect()

return self._client.connected

def connect(self):
"""Connect client."""
result = False
with self._lock:
self._client.connect()
result = self._client.connect()

if result:
_LOGGER.info("successfully connected to %s:%s",
self._client.comm_params.host, self._client.comm_params.port)
else:
_LOGGER.warning("not able to connect to %s:%s",
self._client.comm_params.host, self._client.comm_params.port)
return result


@property
def power_control_enabled(self):
Expand Down Expand Up @@ -305,7 +326,7 @@ def read_modbus_data_meter(self, meter_prefix, start_address):
return False

decoder = BinaryPayloadDecoder.fromRegisters(
meter_data.registers, byteorder=Endian.Big
meter_data.registers, byteorder=Endian.BIG
)
accurrent = decoder.decode_16bit_int()
accurrenta = decoder.decode_16bit_int()
Expand Down Expand Up @@ -564,7 +585,7 @@ def read_modbus_data_inverter(self):
return False

decoder = BinaryPayloadDecoder.fromRegisters(
inverter_data.registers, byteorder=Endian.Big
inverter_data.registers, byteorder=Endian.BIG
)
accurrent = decoder.decode_16bit_uint()
accurrenta = decoder.decode_16bit_uint()
Expand Down Expand Up @@ -695,7 +716,7 @@ def read_modbus_power_limit(self):
return True

decoder = BinaryPayloadDecoder.fromRegisters(
inverter_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
inverter_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
)
# 0xF001 - 1 - Active Power Limit
self.data["nominal_active_power_limit"] = decoder.decode_16bit_uint()
Expand All @@ -715,7 +736,7 @@ def read_modbus_data_storage(self):
)
if not storage_data.isError():
decoder = BinaryPayloadDecoder.fromRegisters(
storage_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
storage_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
)

# 0xE000 - 1 - Export control mode
Expand Down Expand Up @@ -824,8 +845,8 @@ def read_modbus_data_battery(self, battery_prefix, start_address):
if not battery_data.isError():
decoder = BinaryPayloadDecoder.fromRegisters(
battery_data.registers,
byteorder=Endian.Big,
wordorder=Endian.Little,
byteorder=Endian.BIG,
wordorder=Endian.LITTLE,
)

def decode_string(decoder):
Expand Down Expand Up @@ -881,7 +902,7 @@ def decode_string(decoder):
return False

decoder = BinaryPayloadDecoder.fromRegisters(
storage_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
storage_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
)

# 0x6C - 2 - avg temp C
Expand Down
6 changes: 3 additions & 3 deletions custom_components/solaredge_modbus/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"domain": "solaredge_modbus",
"name": "SolarEdge Modbus",
"documentation": "https://github.com/binsentsu/home-assistant-solaredge-modbus",
"requirements": ["pymodbus==3.3.1"],
"requirements": ["pymodbus==3.5.1"],
"dependencies": [],
"codeowners": ["@binsentsu"],
"config_flow": true,
"version": "1.9.1"
}
"version": "1.9.2"
}
2 changes: 1 addition & 1 deletion custom_components/solaredge_modbus/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def native_value(self) -> float:

async def async_set_native_value(self, value: float) -> None:
"""Change the selected value."""
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Little)
builder = BinaryPayloadBuilder(byteorder=Endian.BIG, wordorder=Endian.LITTLE)

if self._fmt == "u32":
builder.add_32bit_uint(int(value))
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "Solaredge Modbus",
"content_in_root": false,
"domains": ["sensor"],
"homeassistant": "2023.7.0",
"homeassistant": "2023.9.0",
"iot_class": "local_poll"
}

0 comments on commit 9ce1801

Please sign in to comment.