gurumitts / pylutron-caseta

Apache License 2.0
153 stars 97 forks source link

KeyError: No device associated with zone X #152

Closed jonoberheide closed 8 months ago

jonoberheide commented 11 months ago

Hi there! Using lutron_caseta as part of Home Assistant. When integrating with a QSX bridge, I get the following error:

2023-11-30 13:07:33.288 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry 192.168.30.20 for lutron_caseta
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 402, in async_setup
    result = await component.async_setup_entry(hass, self)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/lutron_caseta/__init__.py", line 176, in async_setup_entry
    await bridge.connect()
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 106, in connect
    await self._login_completed
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 692, in _login
    await self._load_ra3_devices()
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 801, in _load_ra3_devices
    await self._subscribe_to_multi_zone_status()
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 1291, in _subscribe_to_multi_zone_status
    self._handle_multi_zone_status(response)
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 603, in _handle_multi_zone_status
    self._handle_zone_status(zonestatus)
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 552, in _handle_zone_status
    device = self.get_device_by_zone_id(zone)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pylutron_caseta/smartbridge.py", line 201, in get_device_by_zone_id
    raise KeyError(f"No device associated with zone {zone_id}")
KeyError: 'No device associated with zone 5435'

When looking at the debug output, I see more JSON associated with other zones/devices, but not so much with 5435. Any ideas? Does this seem more like a pylutron_caseta issue (aka maybe just ignore empty zones) or a Lutron issue (aka I need to go debug why there is an empty zone defined)?

Thanks for your help! Happy to debug these interactively or send full debug logs.

jonoberheide commented 8 months ago

After investigating some more, I believe this error is occurring because not all zones/devices are being retrieved from the my QSX system. I noticed that /areas was not returning some areas I expected to see in the results, associated with the missing devices. The /areas call was returning 99 entries, which was a suspiciously specific number given other Lutron system limits that cap out at 99.

Looking more at the response payload, it appears there is pagination support in the protocol, that I will have to dig into a bit to figure out:

{'CommuniqueType': 'ReadResponse', 'Header': {'MessageBodyType': 'MultipleAreaDefinition', 'StatusCode': '200 OK', 'Url': '/area', 'Paging': {'ContinuationToken': 'eyJPYmplY3RJRCI6MzIwNTcsIk9iamVjdFR5cGUiOjJ9'}}, 

If anyone in this area has experience with the ContinuationToken, let me know!

jonoberheide commented 8 months ago

Reflecting that Paging object back into the request header returns the remaining areas. Which happen to correspond to the zones that I was missing and led to the original KeyError exception, I believe.

Thanks for @cbw and @dcode for the debugging help!

So this issue isn't exactly valid...I suppose it's more of a RFE for implementing paging support in the client!

jonoberheide commented 8 months ago

Closing this as #161 is merged.