Skip to content

Commit 9ce1801

Browse files
authored
Merge pull request #197 from kumarunster/master
upgrade pymodbus to 3.5.1 and homeassistant to 2023.09.0
2 parents 4c08aa5 + d5ed8be commit 9ce1801

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

custom_components/solaredge_modbus/__init__.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,14 @@ async def async_refresh_modbus_data(self, _now: Optional[int] = None) -> None:
212212
if not self._sensors:
213213
return
214214

215+
if not self._check_and_reconnect():
216+
#if not connected, skip
217+
return
218+
215219
try:
216220
update_result = self.read_modbus_data()
217221
except Exception as e:
218-
_LOGGER.exception("Error reading modbus data")
222+
_LOGGER.exception("Error reading modbus data", exc_info=True)
219223
update_result = False
220224

221225
if update_result:
@@ -232,10 +236,27 @@ def close(self):
232236
with self._lock:
233237
self._client.close()
234238

239+
def _check_and_reconnect(self):
240+
if not self._client.connected:
241+
_LOGGER.info("modbus client is not connected, trying to reconnect")
242+
return self.connect()
243+
244+
return self._client.connected
245+
235246
def connect(self):
236247
"""Connect client."""
248+
result = False
237249
with self._lock:
238-
self._client.connect()
250+
result = self._client.connect()
251+
252+
if result:
253+
_LOGGER.info("successfully connected to %s:%s",
254+
self._client.comm_params.host, self._client.comm_params.port)
255+
else:
256+
_LOGGER.warning("not able to connect to %s:%s",
257+
self._client.comm_params.host, self._client.comm_params.port)
258+
return result
259+
239260

240261
@property
241262
def power_control_enabled(self):
@@ -305,7 +326,7 @@ def read_modbus_data_meter(self, meter_prefix, start_address):
305326
return False
306327

307328
decoder = BinaryPayloadDecoder.fromRegisters(
308-
meter_data.registers, byteorder=Endian.Big
329+
meter_data.registers, byteorder=Endian.BIG
309330
)
310331
accurrent = decoder.decode_16bit_int()
311332
accurrenta = decoder.decode_16bit_int()
@@ -564,7 +585,7 @@ def read_modbus_data_inverter(self):
564585
return False
565586

566587
decoder = BinaryPayloadDecoder.fromRegisters(
567-
inverter_data.registers, byteorder=Endian.Big
588+
inverter_data.registers, byteorder=Endian.BIG
568589
)
569590
accurrent = decoder.decode_16bit_uint()
570591
accurrenta = decoder.decode_16bit_uint()
@@ -695,7 +716,7 @@ def read_modbus_power_limit(self):
695716
return True
696717

697718
decoder = BinaryPayloadDecoder.fromRegisters(
698-
inverter_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
719+
inverter_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
699720
)
700721
# 0xF001 - 1 - Active Power Limit
701722
self.data["nominal_active_power_limit"] = decoder.decode_16bit_uint()
@@ -715,7 +736,7 @@ def read_modbus_data_storage(self):
715736
)
716737
if not storage_data.isError():
717738
decoder = BinaryPayloadDecoder.fromRegisters(
718-
storage_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
739+
storage_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
719740
)
720741

721742
# 0xE000 - 1 - Export control mode
@@ -824,8 +845,8 @@ def read_modbus_data_battery(self, battery_prefix, start_address):
824845
if not battery_data.isError():
825846
decoder = BinaryPayloadDecoder.fromRegisters(
826847
battery_data.registers,
827-
byteorder=Endian.Big,
828-
wordorder=Endian.Little,
848+
byteorder=Endian.BIG,
849+
wordorder=Endian.LITTLE,
829850
)
830851

831852
def decode_string(decoder):
@@ -881,7 +902,7 @@ def decode_string(decoder):
881902
return False
882903

883904
decoder = BinaryPayloadDecoder.fromRegisters(
884-
storage_data.registers, byteorder=Endian.Big, wordorder=Endian.Little
905+
storage_data.registers, byteorder=Endian.BIG, wordorder=Endian.LITTLE
885906
)
886907

887908
# 0x6C - 2 - avg temp C

custom_components/solaredge_modbus/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"domain": "solaredge_modbus",
33
"name": "SolarEdge Modbus",
44
"documentation": "https://github.com/binsentsu/home-assistant-solaredge-modbus",
5-
"requirements": ["pymodbus==3.3.1"],
5+
"requirements": ["pymodbus==3.5.1"],
66
"dependencies": [],
77
"codeowners": ["@binsentsu"],
88
"config_flow": true,
9-
"version": "1.9.1"
10-
}
9+
"version": "1.9.2"
10+
}

custom_components/solaredge_modbus/number.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def native_value(self) -> float:
140140

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

145145
if self._fmt == "u32":
146146
builder.add_32bit_uint(int(value))

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "Solaredge Modbus",
33
"content_in_root": false,
44
"domains": ["sensor"],
5-
"homeassistant": "2023.7.0",
5+
"homeassistant": "2023.9.0",
66
"iot_class": "local_poll"
77
}

0 commit comments

Comments
 (0)