Skip to content

Commit

Permalink
Querying in both directions by linked service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed Mar 5, 2020
1 parent effaf33 commit 3c89ee6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
36 changes: 30 additions & 6 deletions aiohomekit/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def iid(self, iid: int) -> Service:
return next(filter(lambda service: service.iid == iid, self._services))

def filter(
self, *, service_type: str = None, characteristics: Dict[str, str] = None
self,
*,
service_type: str = None,
characteristics: Dict[str, str] = None,
parent_service: Service = None,
child_service: Service = None,
) -> Iterable[Service]:
matches = iter(self._services)

Expand All @@ -61,14 +66,33 @@ def filter(
lambda service: service[characteristic].value == value, matches
)

if parent_service:
matches = filter(lambda service: service in parent_service.linked, matches)

if child_service:
matches = filter(lambda service: child_service in service.linked, matches)

return matches

def one(
self, *, service_type: str = None, characteristics: Dict[str, str] = None
def first(
self,
*,
service_type: str = None,
characteristics: Dict[str, str] = None,
parent_service: Service = None,
child_service: Service = None,
) -> Service:
return next(
self.filter(service_type=service_type, characteristics=characteristics)
)
try:
return next(
self.filter(
service_type=service_type,
characteristics=characteristics,
parent_service=parent_service,
child_service=child_service,
)
)
except StopIteration:
return None

def append(self, service: Service):
self._services.append(service)
Expand Down
27 changes: 24 additions & 3 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

def test_hue_bridge():
a = Accessories.from_file("tests/fixtures/hue_bridge.json").aid(6623462389072572)
service = a.services.one(service_type=ServicesTypes.ACCESSORY_INFORMATION)
service = a.services.first(service_type=ServicesTypes.ACCESSORY_INFORMATION)
char = service.characteristics[0]
assert char.iid == 37
assert char.perms == ["pr"]
Expand All @@ -34,7 +34,7 @@ def test_hue_bridge():
def test_linked_services():
a = Accessories.from_file("tests/fixtures/hue_bridge.json").aid(6623462389072572)

service = a.services.one(service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH)
service = a.services.first(service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH)
assert len(service.linked) > 0
assert service.linked[0].short_type == ServicesTypes.SERVICE_LABEL

Expand All @@ -43,9 +43,30 @@ def test_get_by_name():
name = "Hue dimmer switch button 3"
a = Accessories.from_file("tests/fixtures/hue_bridge.json").aid(6623462389072572)

service = a.services.one(
service = a.services.first(
service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH,
characteristics={CharacteristicsTypes.NAME: name},
)

assert service[CharacteristicsTypes.NAME].value == name


def test_get_by_linked():
name = "Hue dimmer switch button 3"
a = Accessories.from_file("tests/fixtures/hue_bridge.json").aid(6623462389072572)

switch = a.services.first(
service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH,
characteristics={CharacteristicsTypes.NAME: name},
)

service_label = a.services.first(parent_service=switch)
assert service_label[CharacteristicsTypes.SERVICE_LABEL_NAMESPACE].value == 1

switch = a.services.first(
service_type=ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH,
characteristics={CharacteristicsTypes.NAME: name},
child_service=service_label,
)

assert switch[CharacteristicsTypes.NAME].value == "Hue dimmer switch button 3"

0 comments on commit 3c89ee6

Please sign in to comment.