gurumitts / pylutron-caseta

Apache License 2.0
153 stars 98 forks source link

Refactor login process to be more async #163

Open bdraco opened 6 months ago

bdraco commented 6 months ago

This is an attempt to address https://github.com/home-assistant/core/issues/110067

jonoberheide commented 6 months ago

My test program, of mostly just connecting to the bridge, seems to blow up on this branch:


/usr/lib/python3.11/asyncio/events.py:80: RuntimeWarning: coroutine 'Smartbridge._load_ra3_zones' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/usr/lib/python3.11/asyncio/events.py:80: RuntimeWarning: coroutine 'Smartbridge._load_ra3_control_stations' was never awaited
  self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/home/jono/lutron/test.py", line 30, in <module>
    loop.run_until_complete(example())
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jono/lutron/test.py", line 13, in example
    await bridge.connect()
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 108, in connect
    await self._login_completed
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 811, in _login
    await asyncio.gather(*subscribe_coros)
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 1433, in _subscribe_to_multi_zone_status
    self._handle_multi_zone_status(response)
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 711, in _handle_multi_zone_status
    self._handle_zone_status(zonestatus)
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 653, in _handle_zone_status
    device = self.get_device_by_zone_id(zone)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 203, in get_device_by_zone_id
    raise KeyError(f"No device associated with zone {zone_id}")
KeyError: 'No device associated with zone 5408'
jonoberheide commented 6 months ago

Now it's hitting the REQUEST_TIMEOUT limit...maybe it's trying to do too much in parallel?

Traceback (most recent call last):
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 262, in _request
    response = await self._leap.request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/leap.py", line 75, in request
    return await future
           ^^^^^^^^^^^^
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/jono/lutron/test.py", line 30, in <module>
    loop.run_until_complete(example())
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jono/lutron/test.py", line 13, in example
    await bridge.connect()
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 109, in connect
    await self._login_completed
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 805, in _login
    await asyncio.gather(*load_coros)
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 930, in _load_ra3_devices
    await asyncio.gather(*coros)
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 971, in _load_ra3_control_stations
    station_json = await self._request(
                   ^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/lutron/pylutron-caseta/src/pylutron_caseta/smartbridge.py", line 261, in _request
    async with asyncio_timeout(REQUEST_TIMEOUT):
  File "/usr/lib/python3.11/asyncio/timeouts.py", line 111, in __aexit__
    raise TimeoutError from exc_val
TimeoutError
bdraco commented 6 months ago

It will probably get answered but the bridge takes a while so that timeout is probably too short now