handling of fast cmds sequences

This commit is contained in:
patman15
2024-08-28 21:43:31 +02:00
parent 8e929f191e
commit 236c608dda
2 changed files with 15 additions and 23 deletions

View File

@@ -10,7 +10,7 @@ from bleak import BleakClient
from bleak.backends.device import BLEDevice from bleak.backends.device import BLEDevice
from bleak.exc import BleakError from bleak.exc import BleakError
from bleak.uuids import normalize_uuid_str from bleak.uuids import normalize_uuid_str
from bleak_retry_connector import establish_connection from bleak_retry_connector import close_stale_connections, establish_connection
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from homeassistant.components.cover import ATTR_CURRENT_POSITION from homeassistant.components.cover import ATTR_CURRENT_POSITION
@@ -89,7 +89,8 @@ class PowerViewBLE:
# self._connect_lock = ( # self._connect_lock = (
# asyncio.Lock() # asyncio.Lock()
# ) # TODO: try get rid of (device_info vs. normal cmds) # ) # TODO: try get rid of (device_info vs. normal cmds)
self._cmd_lock = asyncio.Lock() self._cmd_lock: Final = asyncio.Lock()
self._cmd_next = None
self._cipher: Final = ( self._cipher: Final = (
Cipher(algorithms.AES(home_key), modes.CTR(bytearray(16))) Cipher(algorithms.AES(home_key), modes.CTR(bytearray(16)))
if len(home_key) == 16 if len(home_key) == 16
@@ -112,17 +113,19 @@ class PowerViewBLE:
# general cmd: uint16_t cmd, uint8_t seqID, uint8_t data_len # general cmd: uint16_t cmd, uint8_t seqID, uint8_t data_len
async def _cmd(self, cmd: ShadeCmd, data: bytearray) -> None: async def _cmd(self, cmd: ShadeCmd, data: bytearray) -> None:
# if self._cmd_lock.locked(): self._cmd_next = cmd
if self._cmd_lock.locked():
# return LOGGER.debug("%s: device busy, queing %s", self.name, cmd.name)
return
async with self._cmd_lock: async with self._cmd_lock:
try: try:
await self._connect() await self._connect()
assert self._client is not None, "missing BT client" assert self._client is not None, "missing BT client"
cmd_run = self._cmd_next
tx_data = ( tx_data = (
bytearray( bytearray(
int.to_bytes(cmd.value, 2, byteorder="little") int.to_bytes(cmd_run.value, 2, byteorder="little")
+ bytes([self._seqcnt, len(data)]) + bytes([self._seqcnt, len(data)])
) )
+ data + data
@@ -136,10 +139,8 @@ class PowerViewBLE:
self._seqcnt += 1 self._seqcnt += 1
LOGGER.debug("waiting for response") LOGGER.debug("waiting for response")
try: try:
await asyncio.wait_for( await asyncio.wait_for(self._wait_event(), timeout=TIMEOUT)
self._wait_event(), timeout=TIMEOUT self._verify_response(self._data, self._seqcnt - 1, cmd_run)
) # TODO: how long to wait?!
self._verify_response(self._data, self._seqcnt - 1, cmd)
except TimeoutError as ex: except TimeoutError as ex:
raise TimeoutError("Device did not send confirmation.") from ex raise TimeoutError("Device did not send confirmation.") from ex
# finally: # finally:
@@ -269,16 +270,8 @@ class PowerViewBLE:
return return
start = time.time() start = time.time()
# if not isinstance(self._client, BleakClient): await close_stale_connections(self._ble_device)
# self._client = BleakClient( LOGGER.debug("%s: closing stale connections took %is", self.name, time.time()-start)
# self._ble_device,
# disconnected_callback=self._on_disconnect,
# services=[
# UUID_COV_SERVICE,
# # self.UUID_DEV_SERVICE,
# # self.UUID_BAT_SERVICE,
# ],
# )
self._client = await establish_connection( self._client = await establish_connection(
BleakClient, BleakClient,
self._ble_device, self._ble_device,
@@ -291,9 +284,8 @@ class PowerViewBLE:
], ],
) )
await self._client.start_notify(UUID_TX, self._notification_handler) await self._client.start_notify(UUID_TX, self._notification_handler)
# self._client.connect() # dangerous_use_bleak_cache = True
LOGGER.debug("\tconnect took %i", time.time() - start) LOGGER.debug("\tconnect took %is", time.time() - start)
# await self._query_dev_info() # await self._query_dev_info()

View File

@@ -17,5 +17,5 @@
"issue_tracker": "https://github.com/patman15/hdpv_ble/issues", "issue_tracker": "https://github.com/patman15/hdpv_ble/issues",
"loggers": ["hunterdouglas_powerview_ble"], "loggers": ["hunterdouglas_powerview_ble"],
"requirements": ["cryptography>=43.0.0"], "requirements": ["cryptography>=43.0.0"],
"version": 0.20 "version": 0.21
} }