From 800cd9023fe9934600cadf937240073cd478e888 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Thu, 12 Dec 2024 01:20:20 +0100 Subject: [PATCH] refactor: Not standard following sticks compatibility --- custom_components/solarman/api.py | 2 +- custom_components/solarman/common.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/solarman/api.py b/custom_components/solarman/api.py index 865ec78..1193880 100644 --- a/custom_components/solarman/api.py +++ b/custom_components/solarman/api.py @@ -47,7 +47,7 @@ def __init__(self, address, serial, port, mb_slave_id): self._passthrough = False async def _tcp_send_receive_frame(self, mb_request_frame): - return mb_response_adu if (l := len(mb_response_adu := await self._send_receive_v5_frame(mb_request_frame))) and not 8 <= l <= 10 else mb_response_adu + b_(b'\x00\x01', 10 - l) + return mb_compatibility(await self._send_receive_v5_frame(mb_request_frame), mb_request_frame) async def _tcp_parse_response_adu(self, mb_request_frame): return parse_response_adu(await self._tcp_send_receive_frame(mb_request_frame), mb_request_frame) diff --git a/custom_components/solarman/common.py b/custom_components/solarman/common.py index 2790403..41828a0 100644 --- a/custom_components/solarman/common.py +++ b/custom_components/solarman/common.py @@ -135,8 +135,8 @@ def is_ethernet_frame(frame): return int.from_bytes(frame[5:6], byteorder = "big") == len(frame[6:]) return False -def b_(suffix, count): - return b'\x00' * count + suffix +def mb_compatibility(response, request): + return response if not 8 <= (l := len(response)) <= 10 else response[:5] + b'\x06' + response[6:] + (request[l:10] if len(request) > 12 else (b'\x00' * (10 - l))) + b'\x00\x01' def format_exception(e): return re.sub(r"\s+", " ", f"{type(e).__name__}{f': {e}' if f'{e}' else ''}")