Skip to content

Refactor temperature retrieval with None check - #157

Open
digidolphin wants to merge 1 commit into
jcwillox:mainfrom
digidolphin:main
Open

Refactor temperature retrieval with None check#157
digidolphin wants to merge 1 commit into
jcwillox:mainfrom
digidolphin:main

Conversation

@digidolphin

Copy link
Copy Markdown

Problem

When the target temperature is set to 0 and Home Assistant restarts,
the climate entity resets to 21 (DEFAULT_TEMP). Any other value
(e.g. 1–15) is restored correctly — only 0 is affected.

Cause

In async_added_to_hass the restored temperature is checked for
truthiness via the walrus operator:

if temperature := previous_state.attributes.get(ATTR_TEMPERATURE, DEFAULT_TEMP):
    self._attr_target_temperature = float(temperature)

When the stored value is 0, the condition evaluates to False
(0 is falsy in Python), so the restore is skipped and the entity
keeps the DEFAULT_TEMP (21) assigned in __init__.

Fix

Check explicitly for None instead of truthiness:

if (temperature := previous_state.attributes.get(ATTR_TEMPERATURE)) is not None:
    self._attr_target_temperature = float(temperature)

The DEFAULT_TEMP fallback in .get() is redundant and has been
removed: if the attribute is missing, the default set in __init__
already applies.

Testing

  • Set target temperature to 0, restart HA → 0 is now restored
  • Set target temperature to e.g. 5 or 21, restart HA → still restored correctly
  • No previous state (fresh entity) → defaults to 21 as before

When the stored value is `0`, the condition evaluates to `False`
(0 is falsy in Python), so the restore is skipped and the entity
keeps the `DEFAULT_TEMP` (21) assigned in `__init__`.

The `DEFAULT_TEMP` fallback in `.get()` is redundant and has been
removed: if the attribute is missing, the default set in `__init__`
already applies.
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.

1 participant