From b548528085f3b44e63ae4e5057531c64daafee0e Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Fri, 31 May 2024 12:55:46 +0200 Subject: [PATCH] updated pack polling --- .../base_devices/BluettiDevice.py | 5 ++ .../base_devices/ProtocolV1Device.py | 6 ++ .../bluetti_bt_lib/bluetooth/device_reader.py | 55 +++++++++++-------- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/BluettiDevice.py b/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/BluettiDevice.py index dcc5cd7..44a16c3 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/BluettiDevice.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/BluettiDevice.py @@ -42,6 +42,11 @@ class BluettiDevice: def writable_ranges(self) -> List[range]: """The address ranges that are writable""" return [] + + @property + def pack_num_field(self) -> List[ReadHoldingRegisters]: + """The address 'range' of the pack num""" + return [] def has_field(self, field: str): return any(f.name == field for f in self.struct.fields) diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/ProtocolV1Device.py b/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/ProtocolV1Device.py index 7f1f585..91bd5a4 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/ProtocolV1Device.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/base_devices/ProtocolV1Device.py @@ -56,3 +56,9 @@ class ProtocolV1Device(BluettiDevice): @property def writable_ranges(self) -> List[range]: return [range(3006, 3009)] + + @property + def pack_num_field(self) -> List[ReadHoldingRegisters]: + return [ + ReadHoldingRegisters(3006, 1), + ] diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/bluetooth/device_reader.py b/custom_components/bluetti_bt/bluetti_bt_lib/bluetooth/device_reader.py index 0e04564..a88beb0 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/bluetooth/device_reader.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/bluetooth/device_reader.py @@ -97,20 +97,45 @@ class DeviceReader: _LOGGER.warning("Got a parse exception") # Execute pack polling commands - if len(pack_commands) > 0: + if len(pack_commands) > 0 and len(self.bluetti_device.pack_num_field) == 1: _LOGGER.debug("Polling battery packs") for pack in range(1, self.bluetti_device.pack_num_max + 1): + _LOGGER.debug("Setting pack_num to %i", pack) + # Set current pack number - await self._async_send_command( - self.bluetti_device.build_setter_command( - "pack_num", pack - ) + command = self.bluetti_device.build_setter_command( + "pack_num", pack ) + await self._async_send_command(command) - # We need to wait after switching packs for the data to be available - await asyncio.sleep(5) + try: + # Read pack_num field + command = self.bluetti_device.pack_num_field[0] + body = command.parse_response( + await self._async_send_command(command) + ) + _LOGGER.debug("Raw data: %s", body) + parsed = self.bluetti_device.parse( + command.starting_address, body + ) + _LOGGER.debug("Parsed data: %s", parsed) - pack_ok = False + # Check pack number + pack_number = parsed.get("pack_num") + if ( + not isinstance(pack_number, int) + or pack_number != pack + ): + _LOGGER.debug( + "Parsed pack_num(%s) does not match expected '%s', Skipping pack (maybe you don't have this connected)", + pack_number, + pack, + ) + continue + + except ParseError: + _LOGGER.warning("Got a pack parse exception, Skipping pack") + continue for command in pack_commands: # Request & parse result for each pack @@ -123,20 +148,6 @@ class DeviceReader: ) _LOGGER.debug("Parsed data: %s", parsed) - if not pack_ok: # Only check this once - pack_number = parsed.get("pack_num") - if ( - not isinstance(pack_number, int) - or pack_number != pack - ): - _LOGGER.debug( - "Parsed pack_num(%s) does not match expected '%s'", - pack_number, - pack, - ) - continue - pack_ok = True - for key, value in parsed.items(): parsed_data.update({key + str(pack): value})