refactor send command & return response within new func
This commit is contained in:
1 parent
7c25cbbe27
commit
32cfc34c37
1 file changed
+47
-101
@@ -184,7 +184,7 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
parsed_data: dict = {}
|
||||
|
||||
try:
|
||||
async with async_timeout.timeout(45):
|
||||
async with async_timeout.timeout(30):
|
||||
|
||||
# Reconnect if not connected
|
||||
if not self.client.is_connected:
|
||||
@@ -199,112 +199,34 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
|
||||
for command in self.bluetti_device.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)
|
||||
body = command.parse_response(
|
||||
await self.async_send_command(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)
|
||||
parsed_data.update(parsed)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
# pack polling
|
||||
for pack in range (1, self.bluetti_device.pack_num_max + 1):
|
||||
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
|
||||
# Set current pack number
|
||||
await self.async_send_command(
|
||||
self.bluetti_device.build_setter_command('pack_num', pack)
|
||||
)
|
||||
|
||||
for command in self.bluetti_device.pack_polling_commands:
|
||||
# Request & parse result for each pack
|
||||
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)
|
||||
body = command.parse_response(
|
||||
await self.async_send_command(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')
|
||||
@@ -315,22 +237,8 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
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:
|
||||
self.logger.debug("Polling timed out for device %s", mac_loggable(self._address))
|
||||
@@ -348,6 +256,44 @@ class PollingCoordinator(DataUpdateCoordinator):
|
||||
# Pass data back to sensors
|
||||
return parsed_data
|
||||
|
||||
async def async_send_command(self, command: ReadHoldingRegisters) -> bytes:
|
||||
"""Send command and return response"""
|
||||
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 (%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))
|
||||
return cast(bytes, res)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
def _notification_handler(self, _sender: int, data: bytearray):
|
||||
"""Handle bt data."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user