From 6be2abaaf2836f3ff991ae39cdc3709be4d62204 Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Fri, 31 May 2024 14:23:07 +0200 Subject: [PATCH] fix pack polling --- .../base_devices/BluettiDevice.py | 2 +- .../base_devices/ProtocolV1Device.py | 2 +- .../bluetti_bt_lib/bluetooth/device_reader.py | 39 ++++--------------- .../bluetti_bt_lib/devices/ac200l.py | 2 +- .../bluetti_bt_lib/devices/ac200m.py | 2 +- .../bluetti_bt_lib/devices/ac300.py | 2 +- .../bluetti_bt_lib/devices/ac500.py | 2 +- .../bluetti_bt_lib/devices/ep500.py | 2 +- .../bluetti_bt_lib/devices/ep500p.py | 2 +- custom_components/bluetti_bt/sensor.py | 2 +- 10 files changed, 16 insertions(+), 41 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 44a16c3..0d1edae 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 @@ -45,7 +45,7 @@ class BluettiDevice: @property def pack_num_field(self) -> List[ReadHoldingRegisters]: - """The address 'range' of the pack num""" + """The address 'range' of the pack num result. Matches pack_num_result""" return [] def has_field(self, field: str): 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 91bd5a4..8160342 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 @@ -60,5 +60,5 @@ class ProtocolV1Device(BluettiDevice): @property def pack_num_field(self) -> List[ReadHoldingRegisters]: return [ - ReadHoldingRegisters(3006, 1), + ReadHoldingRegisters(96, 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 a88beb0..e721ded 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 @@ -106,36 +106,11 @@ class DeviceReader: command = self.bluetti_device.build_setter_command( "pack_num", pack ) - await self._async_send_command(command) - - 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) - - # 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 + body = command.parse_response( + await self._async_send_command(command) + ) + _LOGGER.debug("Raw data set: %s", body) + # b'\x00\x02' b'\x00\x03' here (seems like result) for command in pack_commands: # Request & parse result for each pack @@ -198,9 +173,9 @@ class DeviceReader: return cast(bytes, res) except TimeoutError: - _LOGGER.warning("Polling single command timed out") + _LOGGER.debug("Polling single command timed out") except ModbusError as err: - _LOGGER.warning( + _LOGGER.debug( "Got an invalid request error for %s: %s", command, err, diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200l.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200l.py index 67be43d..f4c16b5 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200l.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200l.py @@ -24,7 +24,7 @@ class AC200L(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200m.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200m.py index aa574b1..fb0d97c 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200m.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac200m.py @@ -24,7 +24,7 @@ class AC200M(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac300.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac300.py index b8fcb64..e7a9303 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac300.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac300.py @@ -30,7 +30,7 @@ class AC300(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac500.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac500.py index f4ac5ff..3159dde 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac500.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ac500.py @@ -30,7 +30,7 @@ class AC500(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500.py index 3bf7192..1554ce9 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500.py @@ -30,7 +30,7 @@ class EP500(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500p.py b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500p.py index e3748ae..ba19241 100644 --- a/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500p.py +++ b/custom_components/bluetti_bt/bluetti_bt_lib/devices/ep500p.py @@ -30,7 +30,7 @@ class EP500P(ProtocolV1Device): # Battery packs self.struct.add_uint_field("pack_num_max", 91) # internal self.struct.add_decimal_field("total_battery_voltage", 92, 1) - self.struct.add_uint_field("pack_num", 96) # internal + self.struct.add_uint_field("pack_num_result", 96) # internal self.struct.add_decimal_field("pack_voltage", 98, 2) # Full pack voltage self.struct.add_uint_field("pack_battery_percent", 99) self.struct.add_decimal_array_field("cell_voltages", 105, 16, 2) # internal diff --git a/custom_components/bluetti_bt/sensor.py b/custom_components/bluetti_bt/sensor.py index f437837..ba61abe 100644 --- a/custom_components/bluetti_bt/sensor.py +++ b/custom_components/bluetti_bt/sensor.py @@ -156,7 +156,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity): response_data = self.coordinator.data.get(self._response_key) if response_data is None: - _LOGGER.warning("No data for available for (%s)", self._response_key) + _LOGGER.debug("No data for available for (%s)", self._response_key) self._attr_available = False self.async_write_ha_state() return