-
-
Notifications
You must be signed in to change notification settings - Fork 262
Description
_get_device_info assumes info["get_device_info"] is always a dict, but when the device returns an error (e.g. INTERNAL_QUERY_ERROR), it gets set to a SmartErrorCode enum value instead. Trying to subscript that with di["model"] raises TypeError: 'SmartErrorCode' object is not subscriptable.
How it happens
- A device query fails and the response is set to
SmartErrorCode.INTERNAL_QUERY_ERROR(here) - A different module (e.g. Energy or AutoOff) raises
DeviceErrorwhen accessing its.dataproperty because its query also failed - Home Assistant catches that and tries to log a warning, which triggers
__repr__on the device __repr__callsself.model→device_info.short_name→_get_device_info()_get_device_info()doesdi = info["get_device_info"]and thendi["model"]— butdiis aSmartErrorCode, not a dict
Stack trace
File "/usr/local/lib/python3.13/site-packages/kasa/smart/smartdevice.py", line 898, in _get_device_info
short_name = di["model"]
~~^^^^^^^^^
TypeError: 'SmartErrorCode' object is not subscriptable
Full chain (from Home Assistant logs):
File "/usr/local/lib/python3.13/site-packages/kasa/device.py", line 509, in __repr__
f" {self.alias} ({self.model}){update_needed}>"
^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/kasa/smart/smartdevice.py", line 590, in model
return self.device_info.short_name
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/kasa/device.py", line 344, in device_info
return self._get_device_info(self._last_update, self._discovery_info)
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/site-packages/kasa/smart/smartdevice.py", line 898, in _get_device_info
short_name = di["model"]
~~^^^^^^^^^
TypeError: 'SmartErrorCode' object is not subscriptable
The original error that triggers the logging is a module-level DeviceError:
kasa.exceptions.DeviceError: get_current_power for Energy (error_code=INTERNAL_QUERY_ERROR)
or:
kasa.exceptions.DeviceError: get_auto_off_config for AutoOff (error_code=INTERNAL_QUERY_ERROR)
Impact
This makes it impossible to log any warning about a failed module query, since the logging itself crashes. It produces a noisy --- Logging error --- traceback on every coordinator update cycle.
Suggested fix
_get_device_info (or __repr__/model) should handle the case where info["get_device_info"] is a SmartErrorCode instead of a dict — either by raising a more descriptive error or by returning a fallback value.
Version
python-kasa installed via Home Assistant (pip package), observed on the version bundled with HA as of March 2026. The bug is still present on current master (smartdevice.py#L901).