Skip to content

_get_device_info crashes when get_device_info response is a SmartErrorCode #1671

@loganrosen

Description

@loganrosen

_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

  1. A device query fails and the response is set to SmartErrorCode.INTERNAL_QUERY_ERROR (here)
  2. A different module (e.g. Energy or AutoOff) raises DeviceError when accessing its .data property because its query also failed
  3. Home Assistant catches that and tries to log a warning, which triggers __repr__ on the device
  4. __repr__ calls self.modeldevice_info.short_name_get_device_info()
  5. _get_device_info() does di = info["get_device_info"] and then di["model"] — but di is a SmartErrorCode, 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions