Skip to content

Commit

Permalink
2024.03.7b3
Browse files Browse the repository at this point in the history
  • Loading branch information
wills106 committed Mar 22, 2024
1 parent a74d928 commit 4c5a3f5
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions custom_components/solax_modbus/plugin_solax.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ async def async_read_serialnr(hub, address):
if not inverter_data.isError():
decoder = BinaryPayloadDecoder.fromRegisters(inverter_data.registers, byteorder=Endian.BIG)
res = decoder.decode_string(14).decode("ascii")
if res and not res.startswith(("M", "X")):
ba = bytearray(res,"ascii") # convert to bytearray for swapping
ba[0::2], ba[1::2] = ba[1::2], ba[0::2] # swap bytes ourselves - due to bug in Endian.LITTLE ?
res = str(ba, "ascii") # convert back to string
hub.seriesnumber = res
hub.seriesnumber = res
except Exception as ex: _LOGGER.warning(f"{hub.name}: attempt to read serialnumber failed at 0x{address:x} data: {inverter_data}", exc_info=True)
if not res: _LOGGER.warning(f"{hub.name}: reading serial number from address 0x{address:x} failed; other address may succeed")
Expand Down Expand Up @@ -162,11 +167,13 @@ def value_function_remotecontrol_recompute(initval, descr, datadict):
return res

def value_function_byteswapserial(initval, descr, datadict):
if seriesnumber and not seriesnumber.startswith(("M", "X")):
ba = bytearray(seriesnumber,"ascii") # convert to bytearray for swapping
ba[0::2], ba[1::2] = ba[1::2], ba[0::2] # swap bytes ourselves - due to bug in Endian.LITTLE ?
res = str(ba, "ascii") # convert back to string
seriesnumber = res
if initval and not initval.startswith(("M", "X")):
preswap = initval
swapped = ""
for pos in range(0, len(preswap), 2):
swapped += preswap[pos+1] + preswap[pos]
return swapped
return initval

def valuefunction_firmware_g3(initval, descr, datadict):
return f"3.{initval}"
Expand Down Expand Up @@ -6149,7 +6156,7 @@ def value_function_remotecontrol_autorepeat_remaining(initval, descr, datadict):
register = 0x300,
unit = REGISTER_STR,
wordcount=7,
scale = value_function_byeswapserial,
scale = value_function_byteswapserial,
entity_registry_enabled_default = False,
allowedtypes = MIC,
entity_category = EntityCategory.DIAGNOSTIC,
Expand Down Expand Up @@ -7082,11 +7089,6 @@ async def async_determineInverterType(self, hub, configdict):
seriesnumber = await async_read_serialnr(hub, 0x0)
if not seriesnumber:
seriesnumber = await async_read_serialnr(hub, 0x300) # bug in Endian.LITTLE decoding?
if seriesnumber and not seriesnumber.startswith(("M", "X")):
ba = bytearray(seriesnumber,"ascii") # convert to bytearray for swapping
ba[0::2], ba[1::2] = ba[1::2], ba[0::2] # swap bytes ourselves - due to bug in Endian.LITTLE ?
res = str(ba, "ascii") # convert back to string
seriesnumber = res
if not seriesnumber:
_LOGGER.error(f"{hub.name}: cannot find serial number, even not for MIC")
seriesnumber = "unknown"
Expand Down

0 comments on commit 4c5a3f5

Please sign in to comment.