Skip to content

fix: handle missing sensor fields gracefully (KeyError: 'name')#1155

Merged
bachya merged 2 commits into
bachya:devfrom
mattweinb:fix/missing-sensor-name-field
Jun 24, 2026
Merged

fix: handle missing sensor fields gracefully (KeyError: 'name')#1155
bachya merged 2 commits into
bachya:devfrom
mattweinb:fix/missing-sensor-name-field

Conversation

@mattweinb

Copy link
Copy Markdown

Problem

SimpliSafe's API occasionally returns sensor data that omits the name field
(and potentially other fields such as serial, status, flags, setting).
When this happens, the Device.name property raises KeyError: 'name',
crashing the Home Assistant SimpliSafe integration.

See: home-assistant/core#172599

Root cause

Device.name and several DeviceV3 properties use direct bracket access
(sensor_data[serial]["name"]) instead of .get(). There is no fallback
for fields the API may legally omit.

Fix

Replace all unsafe dict["key"] lookups in simplipy/device/__init__.py
with .get("key", default) calls:

  • Device.name → falls back to the device's serial number
  • Device.serial → falls back to the serial number stored at init
  • DeviceV3.error → guards the outer "status" key, defaults to False
  • DeviceV3.low_battery → guards "flags" dict, defaults to False
  • DeviceV3.offline → guards "flags" dict, defaults to False
  • DeviceV3.settings → defaults to {}

Testing

  • All 94 existing tests pass with 100% coverage
  • Two new regression tests simulate sparse API responses and confirm safe
    fallback behavior for both base Device and DeviceV3 properties
  • Verified working against a live SimpliSafe system via Home Assistant

Related

Fixes home-assistant/core#172599

SimpliSafe's API can omit 'name' (and other fields) from a sensor's data
payload. Direct bracket lookups raised KeyError, crashing the integration.

Replace all unsafe dict[key] accesses in Device and DeviceV3 with .get()
calls that return sensible defaults (serial number for name/serial, False
for boolean flags, empty dict for nested dicts/settings).

Add regression tests that simulate sparse API responses and verify safe
fallback behavior.

Fixes home-assistant/core#172599
@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a082f31) to head (f69e822).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev     #1155   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           16        16           
  Lines         1023      1023           
=========================================
  Hits          1023      1023           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bachya bachya left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you!

@bachya bachya merged commit 58a0bdc into bachya:dev Jun 24, 2026
10 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplisafe Key Error: 'name'

2 participants