fix: guard against None in state template handlers - #152
Open
megakid wants to merge 1 commit into
Open
Conversation
state_attr() returns None, not a string, when the source entity is missing or unavailable. The state handlers guard only against the strings STATE_UNKNOWN and STATE_UNAVAILABLE, so None falls through to float()/int() and raises TypeError. Add None to the sentinel tuple in all 15 handlers. For the numeric handlers this prevents the TypeError; for the mode handlers the branch only logs "Received invalid ...", so this suppresses a spurious error when the source is merely unavailable. Closes jcwillox#117 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R6qHpuFJDAavAGsfB4fvXm
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #117
Problem
state_attr()returnsNone— not a string — when the source entity is missing or unavailable. Every state-handler callback guards only against the stringsSTATE_UNKNOWN/STATE_UNAVAILABLE, soNonefalls straight through tofloat()/int():Config that triggers it:
When the source zone drops out,
state_attryieldsNone. Worth noting this fires on every state change of the source entity while it is unavailable, not only at startup as #117 describes.Fix
Add
Noneto the sentinel tuple in all 15 handlers.In the numeric handlers (
_update_min_temp,_update_max_temp,_update_current_temp, the four humidity handlers, and the three target-temperature handlers) this prevents theTypeError. In the mode handlers (_update_hvac_mode,_update_preset_mode,_update_fan_mode,_update_swing_mode,_update_hvac_action) the branch only emits_LOGGER.error("Received invalid ..."), so this suppresses a spurious error when the source is merely unavailable — the same intent as the existing sentinels.No behaviour change when templates render normally.
Notes
Nonerather than switching to a positiveisinstance(temp, (int, float, str))check, to keep the change minimal.main, independent of Update make_template_entity_common_schema import #150.🤖 Generated with Claude Code
https://claude.ai/code/session_01R6qHpuFJDAavAGsfB4fvXm