Skip to content

Commit

Permalink
Make GroupedAPIItems process_item more resilient to unsupported resou…
Browse files Browse the repository at this point in the history
…rce types (#319)
  • Loading branch information
Kane610 authored Jul 8, 2022
1 parent d6d9225 commit 1ce10eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pydeconz/interfaces/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ class GroupedAPIItems(Generic[DataResource]):
resource_group: ResourceGroup

def __init__(
self, gateway: DeconzSession, api_items: list[APIItems[DataResource]]
self, gateway: DeconzSession, handlers: list[APIItems[DataResource]]
) -> None:
"""Initialize sensor manager."""
self.gateway = gateway
self._handlers = api_items
self._handlers = handlers

self._type_to_handler: dict[ResourceType, APIItems[DataResource]] = {
self._resource_type_to_handler: dict[ResourceType, APIItems[DataResource]] = {
resource_type: handler
for handler in api_items
for handler in handlers
if handler.resource_types is not None
for resource_type in handler.resource_types
}
Expand Down Expand Up @@ -198,7 +198,11 @@ def process_item(self, id: str, raw: dict[str, Any]) -> None:
handler.process_item(id, raw)
return

handler = self._type_to_handler[ResourceType(raw.get("type"))]
if (
resource_type := ResourceType(raw.get("type"))
) not in self._resource_type_to_handler:
return
handler = self._resource_type_to_handler[resource_type]
handler.process_item(id, raw)

def subscribe(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ async def test_api_items(mock_aioresponse, deconz_refresh_state):
unsub_apiitems_4()


async def test_unsupported_resource_type(deconz_refresh_state):
"""Verify that creation of APIItems works as expected."""
session = await deconz_refresh_state(
alarm_systems={"1": {"type": "unknown_type"}},
groups={"1": {"type": "unknown_type", "scenes": []}},
lights={"1": {"type": "unknown_type"}},
sensors={"1": {"type": "unknown_type"}},
)

assert len(session.alarmsystems.keys()) == 1
assert len(session.groups.keys()) == 1
assert len(session.lights.keys()) == 1 # Legacy support
assert len(session.sensors.keys()) == 0


@patch("pydeconz.gateway.sleep", new_callable=AsyncMock)
async def test_retry_on_bridge_busy(_, deconz_refresh_state):
"""Verify a max count of 4 bridge busy messages."""
Expand Down

0 comments on commit 1ce10eb

Please sign in to comment.