Skip to content

Fix KeyError 'deviceType' in get_status for IR remote devices#44

Merged
SeraphicRav merged 1 commit into
SeraphicCorp:mainfrom
sarveshbathija:fix/get-status-keyerror-on-missing-deviceType
Apr 10, 2026
Merged

Fix KeyError 'deviceType' in get_status for IR remote devices#44
SeraphicRav merged 1 commit into
SeraphicCorp:mainfrom
sarveshbathija:fix/get-status-keyerror-on-missing-deviceType

Conversation

@sarveshbathija
Copy link
Copy Markdown
Contributor

Summary

Since #42 (released as 2.11.0) SwitchBotAPI.get_status does an unconditional response["deviceType"] lookup to detect Keypad devices. The SwitchBot Cloud API returns an empty body for IR remote devices — the docstring on get_status itself even notes "No status for IR devices" — so the unwrapped response dict has no deviceType key, and every coordinator backing an IR remote (and any other device whose status response omits deviceType) now raises KeyError: 'deviceType'.

In Home Assistant 2026.4.x (which bumped this library to 2.11.0 in home-assistant/core#164663) this surfaces as the switchbot_cloud integration repeatedly failing setup with the following traceback:

Unexpected error fetching switchbot_cloud data
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 426, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/components/switchbot_cloud/coordinator.py", line 70, in _async_update_data
    status: Status = await self._api.get_status(self._device_id)
  File "/usr/local/lib/python3.14/site-packages/switchbot_api/__init__.py", line 265, in get_status
    device_type = response["deviceType"]
                  ~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'deviceType'

Fix

Use dict.get so the missing-key case falls through to the existing "return response as-is" path. This restores the pre-#42 behavior for IR remotes while preserving the keypad special-casing introduced in #42:

- device_type = response["deviceType"]
+ device_type = response.get("deviceType")

if device_type and device_type in KeyPadCommands.get_supported_devices(): short-circuits cleanly when device_type is None, so no further changes are needed.

Test plan

  • Added tests/fixtures/ir_remote.json modeling the empty-body response the SwitchBot Cloud API returns for IR remotes ({"statusCode": 100, "body": {}, "message": "success"})
  • Added test_device_status_ir_remote regression test asserting get_status returns {} instead of raising
  • Verified the new test fails on main with the exact KeyError: 'deviceType' from the bug report, and passes with this fix applied
  • Full suite green: pytest tests/ → 8 passed, 100% coverage, 4 snapshots passed
  • ruff format --check clean on touched files

Refs

The SwitchBot Cloud API returns an empty body for IR remote devices
(the docstring on `get_status` already notes "No status for IR
devices"), so the unwrapped response dict has no `deviceType` key.

Since SeraphicCorp#42 introduced an unconditional `response["deviceType"]` lookup
to detect Keypad devices, every coordinator backing an IR remote (and
any other device whose status response omits `deviceType`) now raises
`KeyError: 'deviceType'`. In Home Assistant this surfaces as the
`switchbot_cloud` integration repeatedly failing to set up:

    File ".../switchbot_api/__init__.py", line 265, in get_status
        device_type = response["deviceType"]
    KeyError: 'deviceType'

Use `dict.get` so the missing-key case falls through to the existing
"return response as-is" path, matching the pre-SeraphicCorp#42 behavior for IR
remotes while preserving the new keypad special-casing.

Adds a regression test + fixture covering an empty status body.
@sarveshbathija
Copy link
Copy Markdown
Contributor Author

Hello @SeraphicRav ?

Copy link
Copy Markdown
Contributor

@SeraphicRav SeraphicRav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution ! Sorry for the review delay !

@SeraphicRav SeraphicRav merged commit bc7eabc into SeraphicCorp:main Apr 10, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants