Refactor bluetooth GATT writes to reduce duplicate code (#771)
This commit is contained in:
parent
15e6279246
commit
5e0b6c1b07
|
@ -809,30 +809,6 @@ class APIClient:
|
||||||
timeout,
|
timeout,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def bluetooth_gatt_write(
|
|
||||||
self,
|
|
||||||
address: int,
|
|
||||||
handle: int,
|
|
||||||
data: bytes,
|
|
||||||
response: bool,
|
|
||||||
timeout: float = DEFAULT_BLE_TIMEOUT,
|
|
||||||
) -> None:
|
|
||||||
req = BluetoothGATTWriteRequest(
|
|
||||||
address=address, handle=handle, response=response, data=data
|
|
||||||
)
|
|
||||||
|
|
||||||
if not response:
|
|
||||||
self._get_connection().send_message(req)
|
|
||||||
return
|
|
||||||
|
|
||||||
await self._send_bluetooth_message_await_response(
|
|
||||||
address,
|
|
||||||
handle,
|
|
||||||
req,
|
|
||||||
BluetoothGATTWriteResponse,
|
|
||||||
timeout,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def bluetooth_gatt_read_descriptor(
|
async def bluetooth_gatt_read_descriptor(
|
||||||
self,
|
self,
|
||||||
address: int,
|
address: int,
|
||||||
|
@ -868,6 +844,22 @@ class APIClient:
|
||||||
assert isinstance(resp, BluetoothGATTReadResponse)
|
assert isinstance(resp, BluetoothGATTReadResponse)
|
||||||
return bytearray(resp.data)
|
return bytearray(resp.data)
|
||||||
|
|
||||||
|
async def bluetooth_gatt_write(
|
||||||
|
self,
|
||||||
|
address: int,
|
||||||
|
handle: int,
|
||||||
|
data: bytes,
|
||||||
|
response: bool,
|
||||||
|
timeout: float = DEFAULT_BLE_TIMEOUT,
|
||||||
|
) -> None:
|
||||||
|
await self._bluetooth_gatt_write(
|
||||||
|
address,
|
||||||
|
handle,
|
||||||
|
BluetoothGATTWriteRequest(response=response, data=data),
|
||||||
|
timeout,
|
||||||
|
response,
|
||||||
|
)
|
||||||
|
|
||||||
async def bluetooth_gatt_write_descriptor(
|
async def bluetooth_gatt_write_descriptor(
|
||||||
self,
|
self,
|
||||||
address: int,
|
address: int,
|
||||||
|
@ -876,14 +868,28 @@ class APIClient:
|
||||||
timeout: float = DEFAULT_BLE_TIMEOUT,
|
timeout: float = DEFAULT_BLE_TIMEOUT,
|
||||||
wait_for_response: bool = True,
|
wait_for_response: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
req = BluetoothGATTWriteDescriptorRequest(
|
await self._bluetooth_gatt_write(
|
||||||
address=address, handle=handle, data=data
|
address,
|
||||||
|
handle,
|
||||||
|
BluetoothGATTWriteDescriptorRequest(data=data),
|
||||||
|
timeout,
|
||||||
|
wait_for_response,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def _bluetooth_gatt_write(
|
||||||
|
self,
|
||||||
|
address: int,
|
||||||
|
handle: int,
|
||||||
|
req: BluetoothGATTWriteDescriptorRequest | BluetoothGATTWriteRequest,
|
||||||
|
timeout: float,
|
||||||
|
wait_for_response: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Perform a GATT write to a char or descriptor."""
|
||||||
|
req.address = address
|
||||||
|
req.handle = handle
|
||||||
if not wait_for_response:
|
if not wait_for_response:
|
||||||
self._get_connection().send_message(req)
|
self._get_connection().send_message(req)
|
||||||
return
|
return
|
||||||
|
|
||||||
await self._send_bluetooth_message_await_response(
|
await self._send_bluetooth_message_await_response(
|
||||||
address,
|
address,
|
||||||
handle,
|
handle,
|
||||||
|
|
Loading…
Reference in New Issue