add polling for battery pack voltage & percent
This commit is contained in:
1 parent
296c9cbfd3
commit
b8a73e0ab9
2 files changed
+107
-3
No files matched your search
@@ -80,6 +80,10 @@ class DummyDevice(BluettiDevice):
|
|||||||
super().__init__(device.address, device.type, device.sn)
|
super().__init__(device.address, device.type, device.sn)
|
||||||
self._parent = device
|
self._parent = device
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pack_num_max(self):
|
||||||
|
return self._parent.pack_num_max
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
def polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
if self.type == "EP600":
|
if self.type == "EP600":
|
||||||
@@ -112,7 +116,7 @@ class DummyDevice(BluettiDevice):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
def pack_polling_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
return self._parent.pack_logging_commands
|
return self._parent.pack_polling_commands
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logging_commands(self) -> List[ReadHoldingRegisters]:
|
def logging_commands(self) -> List[ReadHoldingRegisters]:
|
||||||
@@ -180,7 +184,7 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
parsed_data: dict = {}
|
parsed_data: dict = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(15):
|
async with async_timeout.timeout(45):
|
||||||
|
|
||||||
# Reconnect if not connected
|
# Reconnect if not connected
|
||||||
if not self.client.is_connected:
|
if not self.client.is_connected:
|
||||||
@@ -238,6 +242,98 @@ class PollingCoordinator(DataUpdateCoordinator):
|
|||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# pack polling
|
||||||
|
for pack in range (1, self.bluetti_device.pack_num_max + 1):
|
||||||
|
# TODO need to set pack num ...
|
||||||
|
# We need to wait after switching packs for the data to be available (as per bluetti_mqtt device_handler.py)
|
||||||
|
try:
|
||||||
|
# Prepare to make request
|
||||||
|
command = self.bluetti_device.build_setter_command('pack_num', pack)
|
||||||
|
self.current_command = command
|
||||||
|
self.notify_future = self.hass.loop.create_future()
|
||||||
|
self.notify_response = bytearray()
|
||||||
|
|
||||||
|
# Make request
|
||||||
|
self.logger.debug("Requesting %s : pack_num(%s)", command, pack)
|
||||||
|
await self.client.write_gatt_char(
|
||||||
|
BluetoothClient.WRITE_UUID, bytes(command)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Wait for response
|
||||||
|
res = await asyncio.wait_for(
|
||||||
|
self.notify_future, timeout=BluetoothClient.RESPONSE_TIMEOUT
|
||||||
|
)
|
||||||
|
# nothing to process - setting current pack_num
|
||||||
|
|
||||||
|
except TimeoutError:
|
||||||
|
self.logger.debug(
|
||||||
|
"Polling timed out (address: %s)", mac_loggable(self._address)
|
||||||
|
)
|
||||||
|
except ModbusError as err:
|
||||||
|
self.logger.warning(
|
||||||
|
"Got an invalid request error for %s: %s",
|
||||||
|
command,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
except (BadConnectionError, BleakError) as err:
|
||||||
|
self.logger.warning(
|
||||||
|
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
||||||
|
)
|
||||||
|
|
||||||
|
for command in self.bluetti_device.pack_polling_commands:
|
||||||
|
try:
|
||||||
|
# Prepare to make request
|
||||||
|
self.current_command = command
|
||||||
|
self.notify_future = self.hass.loop.create_future()
|
||||||
|
self.notify_response = bytearray()
|
||||||
|
|
||||||
|
# Make request
|
||||||
|
self.logger.debug("Requesting %s", command)
|
||||||
|
await self.client.write_gatt_char(
|
||||||
|
BluetoothClient.WRITE_UUID, bytes(command)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Wait for response
|
||||||
|
res = await asyncio.wait_for(
|
||||||
|
self.notify_future, timeout=BluetoothClient.RESPONSE_TIMEOUT
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process data
|
||||||
|
self.logger.debug("Got %s bytes", len(res))
|
||||||
|
response = cast(bytes, res)
|
||||||
|
body = command.parse_response(response)
|
||||||
|
parsed = self.bluetti_device.parse(
|
||||||
|
command.starting_address, body
|
||||||
|
)
|
||||||
|
|
||||||
|
self.logger.debug("Parsed data: %s", parsed)
|
||||||
|
|
||||||
|
packNo = parsed.get('pack_num')
|
||||||
|
if not isinstance(packNo, int) or packNo != pack:
|
||||||
|
self.logger.debug("Parsed pack_num(%s) does not match expected '%s'", packNo, pack)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for key, value in parsed.items():
|
||||||
|
parsed_data.update({key+str(pack):value})
|
||||||
|
|
||||||
|
except TimeoutError:
|
||||||
|
self.logger.debug(
|
||||||
|
"Polling timed out (address: %s)", mac_loggable(self._address)
|
||||||
|
)
|
||||||
|
except ParseError:
|
||||||
|
self.logger.warning("Got a parse exception...")
|
||||||
|
except ModbusError as err:
|
||||||
|
self.logger.warning(
|
||||||
|
"Got an invalid request error for %s: %s",
|
||||||
|
command,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
except (BadConnectionError, BleakError) as err:
|
||||||
|
self.logger.warning(
|
||||||
|
"Needed to disconnect due to error: %s (This can also be the case if you used device controls)", err
|
||||||
|
)
|
||||||
|
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self.logger.debug("Polling timed out for device %s", mac_loggable(self._address))
|
self.logger.debug("Polling timed out for device %s", mac_loggable(self._address))
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from bluetti_mqtt.mqtt_client import (
|
|||||||
NORMAL_DEVICE_FIELDS,
|
NORMAL_DEVICE_FIELDS,
|
||||||
DC_INPUT_FIELDS,
|
DC_INPUT_FIELDS,
|
||||||
MqttFieldType,
|
MqttFieldType,
|
||||||
|
battery_pack_fields,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import device_info as dev_info, get_unique_id
|
from . import device_info as dev_info, get_unique_id
|
||||||
@@ -59,8 +60,14 @@ async def async_setup_entry(
|
|||||||
all_fields = NORMAL_DEVICE_FIELDS
|
all_fields = NORMAL_DEVICE_FIELDS
|
||||||
all_fields.update(DC_INPUT_FIELDS)
|
all_fields.update(DC_INPUT_FIELDS)
|
||||||
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
|
all_fields.update(ADDITIONAL_DEVICE_FIELDS)
|
||||||
|
# add pack fields for device
|
||||||
|
_LOGGER.info("Device type(%s) pack_num_max(%s)", bluetti_device.type, bluetti_device.pack_num_max)
|
||||||
|
for pack in range (1, bluetti_device.pack_num_max + 1):
|
||||||
|
for name, field in battery_pack_fields(pack).items():
|
||||||
|
all_fields.update({name+str(pack):field})
|
||||||
|
|
||||||
for field_key, field_config in all_fields.items():
|
for field_key, field_config in all_fields.items():
|
||||||
if bluetti_device.has_field(field_key):
|
if bluetti_device.has_field(field_key) or field_config.id_override is not None:
|
||||||
category = None
|
category = None
|
||||||
if field_config.setter is True or field_key in DIAGNOSTIC_FIELDS:
|
if field_config.setter is True or field_key in DIAGNOSTIC_FIELDS:
|
||||||
category = EntityCategory.DIAGNOSTIC
|
category = EntityCategory.DIAGNOSTIC
|
||||||
@@ -144,6 +151,7 @@ class BluettiSensor(CoordinatorEntity, SensorEntity):
|
|||||||
|
|
||||||
response_data = self.coordinator.data.get(self._response_key)
|
response_data = self.coordinator.data.get(self._response_key)
|
||||||
if response_data is None:
|
if response_data is None:
|
||||||
|
_LOGGER.debug("No data for available for (%s)",self._response_key)
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user