fix: handle missing sensor fields gracefully (KeyError: 'name')#1155
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SimpliSafe's API occasionally returns sensor data that omits the
namefield(and potentially other fields such as
serial,status,flags,setting).When this happens, the
Device.nameproperty raisesKeyError: 'name',crashing the Home Assistant SimpliSafe integration.
See: home-assistant/core#172599
Root cause
Device.nameand severalDeviceV3properties use direct bracket access(
sensor_data[serial]["name"]) instead of.get(). There is no fallbackfor fields the API may legally omit.
Fix
Replace all unsafe
dict["key"]lookups insimplipy/device/__init__.pywith
.get("key", default)calls:Device.name→ falls back to the device's serial numberDevice.serial→ falls back to the serial number stored at initDeviceV3.error→ guards the outer"status"key, defaults toFalseDeviceV3.low_battery→ guards"flags"dict, defaults toFalseDeviceV3.offline→ guards"flags"dict, defaults toFalseDeviceV3.settings→ defaults to{}Testing
fallback behavior for both base
DeviceandDeviceV3propertiesRelated
Fixes home-assistant/core#172599