diff --git a/custom_components/solarman/api.py b/custom_components/solarman/api.py index 45df5ed..8b62003 100644 --- a/custom_components/solarman/api.py +++ b/custom_components/solarman/api.py @@ -1,7 +1,3 @@ -import time -import errno -import struct -import socket import logging import asyncio import threading @@ -9,145 +5,13 @@ from datetime import datetime -from umodbus.client.tcp import read_coils, read_discrete_inputs, read_holding_registers, read_input_registers, write_single_coil, write_multiple_coils, write_single_register, write_multiple_registers, parse_response_adu - from .const import * from .common import * from .provider import * -from .include.pysolarmanv5 import PySolarmanV5Async, NoSocketAvailableError +from .include.pysolarmanv5 import PySolarmanAsync _LOGGER = logging.getLogger(__name__) -class PySolarmanV5AsyncWrapper(PySolarmanV5Async): - def __init__(self, address, serial, port, mb_slave_id): - super().__init__(address, serial, port = port, mb_slave_id = mb_slave_id, logger = _LOGGER, auto_reconnect = AUTO_RECONNECT, socket_timeout = TIMINGS_SOCKET_TIMEOUT) - - @property - def auto_reconnect(self): - return self._needs_reconnect - - async def connect(self) -> bool: - if not self.reader_task: - _LOGGER.info(f"[{self.serial}] Connecting to {self.address}:{self.port}") - await super().connect() - return True - return False - - async def disconnect(self) -> None: - _LOGGER.info(f"[{self.serial}] Disconnecting from {self.address}:{self.port}") - try: - await super().disconnect() - finally: - self.reader_task = None - self.reader = None - self.writer = None - -class PySolarmanAsync(PySolarmanV5AsyncWrapper): - def __init__(self, address, serial, port, mb_slave_id): - super().__init__(address, serial, port, mb_slave_id) - self._passthrough = False - - async def _tcp_send_receive_frame(self, mb_request_frame): - 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) - - async def _heartbeat_response(self, request_frame): - v5_frame = bytearray( - self.v5_start - + struct.pack(" 9: - return int.from_bytes(frame[5:6], byteorder = "big") == len(frame[6:]) and int.from_bytes(frame[8:9], byteorder = "big") == len(frame[9:]) - if frame_len > 6: # [0xa5, 0x17, 0x00, 0x10, 0x45, 0x03, 0x00, 0x98, 0x02] - return int.from_bytes(frame[5:6], byteorder = "big") == len(frame[6:]) - return False - -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 ''}") diff --git a/custom_components/solarman/const.py b/custom_components/solarman/const.py index c1a15b8..c4e3a2e 100644 --- a/custom_components/solarman/const.py +++ b/custom_components/solarman/const.py @@ -73,12 +73,6 @@ ATTR_ = { CONF_MOD: CONF_MOD, CONF_MPPT: CONF_MPPT, CONF_PHASE: "l" } -CONTROL_CODE = types.SimpleNamespace() -CONTROL_CODE.REQUEST = struct.pack("