utilities/tp-link-to-influxdb#2: Kasa Exception Handling



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.23
Created: 04-Sep-22 11:25



Description

We don't currently trap any exceptions that python-kasa might throw.

It's not been needed so far - the library tends to trap issues and return empty values.

However, it'd still be prudent to add our own handling to ensure that if an exception were to find it's way through, it won't affect collection of data from subsequent devices.



Toggle State Changes

Activity


assigned to @btasker

Turns out the library doesn't catch everything

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/kasa/protocol.py", line 131, in _query
    await self._connect(timeout)
  File "/usr/local/lib/python3.10/site-packages/kasa/protocol.py", line 81, in _connect
    self.reader, self.writer = await asyncio.wait_for(task, timeout=timeout)
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.10/asyncio/streams.py", line 47, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    return await fut
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('192.168.3.163', 9999)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/collect.py", line 181, in <module>
    main()
  File "/app/collect.py", line 68, in main
    now_usage_w, today_usage = poll_kasa(kasa['ip'])
  File "/app/collect.py", line 106, in poll_kasa
    asyncio.run(p.update())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.10/site-packages/kasa/smartdevice.py", line 314, in update
    self._last_update = await self.protocol.query(req)
  File "/usr/local/lib/python3.10/site-packages/kasa/protocol.py", line 73, in query
    return await self._query(request, retry_count, timeout)
  File "/usr/local/lib/python3.10/site-packages/kasa/protocol.py", line 140, in _query
    raise SmartDeviceException(
kasa.exceptions.SmartDeviceException: Unable to connect to the device: 192.168.3.163: [Errno 113] Connect call failed ('192.168.3.163', 9999)
verified

mentioned in commit github-mirror/tplink_to_influxdb@0e929480adf7a6ec294544ce857b8b4288af23d6

Commit: github-mirror/tplink_to_influxdb@0e929480adf7a6ec294544ce857b8b4288af23d6 
Author: B Tasker                            
                            
Date: 2022-12-28T08:45:59.000+00:00 

Message

Trap Kasa exceptions utilities/tp-link-to-influxdb#2

+6 -3 (9 lines changed)