Skip to content

Commit

Permalink
Ensure all transports can handle an iterable in get_characteristics
Browse files Browse the repository at this point in the history
- Add some missing typing
  • Loading branch information
bdraco committed Oct 31, 2023
1 parent 94bc38f commit 955171e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
12 changes: 6 additions & 6 deletions aiohomekit/controller/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ async def list_pairings(self):
@abstractmethod
async def get_characteristics(
self,
characteristics,
include_meta=False,
include_perms=False,
include_type=False,
include_events=False,
):
characteristics: Iterable[tuple[int, int]],
include_meta: bool = False,
include_perms: bool = False,
include_type: bool = False,
include_events: bool = False,
) -> dict[tuple[int, int], dict[str, Any]]:
"""Get characteristics."""

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions aiohomekit/controller/ble/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ async def list_pairings(self):
@disconnect_on_missing_services
async def get_characteristics(
self,
characteristics: list[tuple[int, int]],
characteristics: Iterable[tuple[int, int]],
) -> dict[tuple[int, int], dict[str, Any]]:
return await self._get_characteristics_without_retry(characteristics)

Expand Down Expand Up @@ -1328,7 +1328,7 @@ def _sort_characteristics_by_fetch_order(
@restore_connection_and_resume
async def _get_characteristics_without_retry(
self,
characteristics: list[tuple[int, int]],
characteristics: Iterable[tuple[int, int]],
notify_listeners: bool = False,
) -> dict[tuple[int, int], dict[str, Any]]:
accessory_chars = self.accessories.aid(BLE_AID).characteristics
Expand Down
13 changes: 10 additions & 3 deletions aiohomekit/controller/coap/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
import random
import struct
from typing import Any
from typing import Any, Iterable
import uuid

from aiocoap import Context, Message, resource
Expand Down Expand Up @@ -502,8 +502,15 @@ def _read_characteristics_exit(
logger.debug(f"Read characteristics: {results!r}")
return results

async def read_characteristics(self, ids: list[tuple[int, int]]):
iids = [int(aid_iid[1]) for aid_iid in ids]
async def read_characteristics(
self, characteristics: Iterable[tuple[int, int]]
) -> dict[tuple[int, int], dict[str, Any]]:
"""Read characteristics from the accessory."""
# _read_characteristics_exit expects a list of tuples
# as it does an ordered read so we need to convert
# to a list to preserve the order
ids = list(characteristics)
iids = [int(aid_iid[1]) for aid_iid in characteristics]
data = [b""] * len(iids)
pdu_results = await self.enc_ctx.post_all(OpCode.CHAR_READ, iids, data)
return self._read_characteristics_exit(ids, pdu_results)
Expand Down
4 changes: 2 additions & 2 deletions aiohomekit/controller/coap/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ async def async_populate_accessories_state(

async def get_characteristics(
self,
characteristics,
):
characteristics: Iterable[tuple[int, int]],
) -> dict[tuple[int, int], dict[str, Any]]:
await self._ensure_connected()
return await self.connection.read_characteristics(characteristics)

Expand Down
11 changes: 8 additions & 3 deletions aiohomekit/controller/ip/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ async def list_pairings(self):

async def get_characteristics(
self,
characteristics,
):
characteristics: Iterable[tuple[int, int]],
) -> dict[tuple[int, int], dict[str, Any]]:
"""
This method is used to get the current readouts of any characteristic of the accessory.
Expand All @@ -266,8 +266,13 @@ async def get_characteristics(
if not self.accessories:
await self.list_accessories_and_characteristics()

if isinstance(characteristics, set):
characteristics_set = characteristics
else:
characteristics_set = set(characteristics)

url = "/characteristics?id=" + ",".join(
str(x[0]) + "." + str(x[1]) for x in set(characteristics)
f"{aid}.{iid}" for aid, iid in characteristics_set
)

response = await self.connection.get_json(url)
Expand Down

0 comments on commit 955171e

Please sign in to comment.