Skip to content

fix: guard against None in state template handlers - #152

Open
megakid wants to merge 1 commit into
jcwillox:mainfrom
megakid:fix/none-guards-state-templates
Open

fix: guard against None in state template handlers#152
megakid wants to merge 1 commit into
jcwillox:mainfrom
megakid:fix/none-guards-state-templates

Conversation

@megakid

@megakid megakid commented Sep 3, 2026

Copy link
Copy Markdown

Closes #117

Problem

state_attr() returns None — not a string — when the source entity is missing or unavailable. Every state-handler callback guards only against the strings STATE_UNKNOWN / STATE_UNAVAILABLE, so None falls straight through to float()/int():

File "custom_components/climate_template/climate.py", line 591, in _update_current_temp
    self._attr_current_temperature = float(temp)
TypeError: float() argument must be a string or a real number, not 'NoneType'

Config that triggers it:

- platform: climate_template
  name: "Master Bedroom Auto"
  current_temperature_template: "{{ state_attr('climate.master_bedroom', 'current_temperature') }}"

When the source zone drops out, state_attr yields None. 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 None to 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 the TypeError. 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

🤖 Generated with Claude Code

https://claude.ai/code/session_01R6qHpuFJDAavAGsfB4fvXm

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
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.

TypeError: float() argument must be a string or a real number, not 'NoneType' at startup

1 participant