first working polling

This commit is contained in:
Patrick762 committed 2023-07-31 14:00:25 +02:00
1 parent fad9fba574
commit cf149067d8
2 files changed
+13 -17

No files matched your search

-1
View File
@@ -32,7 +32,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Setup platforms # Setup platforms
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
_LOGGER.error("Device address: %s", address)
return True return True
+13 -16
View File
@@ -62,11 +62,10 @@ class PollingCoordinator(DataUpdateCoordinator):
hass, hass,
_LOGGER, _LOGGER,
name="Bluetti polling coordinator", name="Bluetti polling coordinator",
update_interval=timedelta(seconds=10), update_interval=timedelta(seconds=20),
) )
self._address = address self._address = address
self.notify_future = None self.notify_future = None
self.command_queue = asyncio.Queue()
self.current_command = None self.current_command = None
self.notify_response = bytearray() self.notify_response = bytearray()
self.bluetti_device = build_device(address, device_name) self.bluetti_device = build_device(address, device_name)
@@ -88,10 +87,6 @@ class PollingCoordinator(DataUpdateCoordinator):
self.logger.error("Device type not found") self.logger.error("Device type not found")
return return
# Fill command_queue
for command in self.bluetti_device.polling_commands:
await self.command_queue.put(command)
# Polling # Polling
client = BleakClient(device) client = BleakClient(device)
@@ -102,17 +97,17 @@ class PollingCoordinator(DataUpdateCoordinator):
BluetoothClient.NOTIFY_UUID, self._notification_handler BluetoothClient.NOTIFY_UUID, self._notification_handler
) )
while not self.command_queue.empty(): for command in self.bluetti_device.polling_commands:
try: try:
# Prepare to make request # Prepare to make request
current_command = await self.command_queue.get() self.current_command = command
self.current_command = current_command
self.notify_future = self.hass.loop.create_future() self.notify_future = self.hass.loop.create_future()
self.notify_response = bytearray() self.notify_response = bytearray()
# Make request # Make request
self.logger.debug("Requesting %s", command)
await client.write_gatt_char( await client.write_gatt_char(
BluetoothClient.WRITE_UUID, bytes(current_command) BluetoothClient.WRITE_UUID, bytes(command)
) )
# Wait for response # Wait for response
@@ -121,20 +116,22 @@ class PollingCoordinator(DataUpdateCoordinator):
) )
# Process data # Process data
self.logger.error("Got %s bytes", len(res))
response = cast(bytes, res) response = cast(bytes, res)
body = current_command.parse_response(response) body = command.parse_response(response)
parsed = self.bluetti_device.parse(command.starting_address, body) parsed = self.bluetti_device.parse(command.starting_address, body)
# TODO: Use parsed data self.logger.info("Parsed data: %s", parsed)
self.logger.error("Processing parsed data") # Pass data back to sensors
self.command_queue.task_done() except TimeoutError:
self.logger.error("Polling timed out")
except ParseError: except ParseError:
self.logger.debug("Got a parse exception...") self.logger.debug("Got a parse exception...")
except ModbusError as err: except ModbusError as err:
self.logger.debug( self.logger.debug(
"Got an invalid request error for %s: %s", "Got an invalid request error for %s: %s",
current_command, command,
err, err,
) )
except (BadConnectionError, BleakError) as err: except (BadConnectionError, BleakError) as err:
@@ -195,5 +192,5 @@ class Battery(CoordinatorEntity, SensorEntity):
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator.""" """Handle updated data from the coordinator."""
_LOGGER.error("Updating state of %s", self._attr_unique_id) _LOGGER.debug("Updating state of %s", self._attr_unique_id)
self.async_write_ha_state() self.async_write_ha_state()