fix: prevent KeyError when action settings entity key is missing - #31
fix: prevent KeyError when action settings entity key is missing#31brombomb wants to merge 3 commits into
Conversation
|
Hm, I regularly restart Home Assistant while the plugin is connected and it never failed to reconnect. I would like to understand and reproduce the problem before merging. |
|
Again from Gemini, and this probably hints at my setup pretty well... Step-by-Step Reproduction
Here is a sanitized version of my Home Screen Layout Keys in Home.json with Empty / Unset Entity IDs ('entity': '')These keys have an active Home Assistant action with a domain assigned, but the target entity ID is empty (""). During a reconnect, refresh() attempts to inspect these keys: • Key 0x0 (Action #0: HomeAssistantPlugin::ShowText) What's frustrating here, is I have configured many of these keys. For example the Key 0x0 should be:
All the keys except CPU and Weather are HA driven here (same Home screen)
I don't know if this specific issue is wiping my keys, or another crash is. Why Your Button Configs Kept Resetting to ai_taskHere is the exact mechanism causing your buttons to reset:
|


Real-World Situation & Crash Recovery Dynamics
This issue specifically occurs during StreamController crash recovery, ungraceful exits, or Flatpak sandbox state loss:
~/.var/app/com.core447.StreamController/data/pages/Home.json). If StreamController crashes, is force-killed, or experiences an unhandled exception before a clean shutdown, in-memory action settings are not written to disk, leaving the action entry with empty/unpopulated settings ({}or{"version": 1}).ShowTextactions for Top, Middle, and Bottom slots), a reconnect event fires multiplerefresh()calls in rapid succession.home_assistant_backend.pyiterates over active actions. When an un-persisted or partially-configured action is encountered,self.settings.get_entity()attemptssettings['entity']['entity'], throwingKeyError: 'entity'. This unhandled exception breaks the callback loop, preventing subsequent actions from initializing cleanly and creating a persistent crash-restart loop.Steps to Reproduce
{}).StreamController.log: every background refresh pass for the recovered action raisesKeyError: 'entity'and aborts subsequent action setups on that key.Environment & System Info
Linux 7.0.0-28-generic x86_64)3.14.4com.core447.StreamController/stable)v1.1.1Proposed Fix
_get_*_settings()helpers acrossBaseSettings,ShowTextSettings,ShowIconSettings,PerformActionSettings,CustomizationSettings, andLevelDialSettingsto safely check if settings sections exist.deepcopy(DEFAULT_SETTINGS)and returning fallback/empty values ("") when settings keys are transiently missing during HA reconnects or resets.test_missing_entity_keyintest_base_settings.py.AI Disclosure
This pull request was diagnosed, developed, and verified with assistance from Google Antigravity (AGY). Antigravity was used to analyze stack traces, audit settings deserialization, develop defensive type-safe routines, author unit tests, and verify overall code correctness.