Compare commits

...

1 Commits

Author SHA1 Message Date
Gilles Boccon-Gibod
7fa2eb7658 support with_response on adapters 2022-10-10 12:11:51 -07:00
3 changed files with 18 additions and 4 deletions

View File

@@ -346,8 +346,11 @@ class CharacteristicAdapter:
async def read_decoded_value(self): async def read_decoded_value(self):
return self.decode_value(await self.wrapped_characteristic.read_value()) return self.decode_value(await self.wrapped_characteristic.read_value())
async def write_decoded_value(self, value): async def write_decoded_value(self, value, with_response=False):
return await self.wrapped_characteristic.write_value(self.encode_value(value)) return await self.wrapped_characteristic.write_value(
self.encode_value(value),
with_response
)
def encode_value(self, value): def encode_value(self, value):
return value return value

View File

@@ -184,8 +184,8 @@ class Client:
# Wait until we can send (only one pending command at a time for the connection) # Wait until we can send (only one pending command at a time for the connection)
response = None response = None
async with self.request_semaphore: async with self.request_semaphore:
assert(self.pending_request is None) assert self.pending_request is None
assert(self.pending_response is None) assert self.pending_response is None
# Create a future value to hold the eventual response # Create a future value to hold the eventual response
self.pending_response = asyncio.get_running_loop().create_future() self.pending_response = asyncio.get_running_loop().create_future()

View File

@@ -164,6 +164,17 @@ async def test_characteristic_encoding():
await async_barrier() await async_barrier()
assert characteristic.value == bytes([124]) assert characteristic.value == bytes([124])
v = await cp.read_value()
assert v == 124
await cp.write_value(125, with_response=True)
await async_barrier()
assert characteristic.value == bytes([125])
cd = DelegatedCharacteristicAdapter(c, encode=lambda x: bytes([x // 2]))
await cd.write_value(100, with_response=True)
await async_barrier()
assert characteristic.value == bytes([50])
last_change = None last_change = None
def on_change(value): def on_change(value):