mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Fix input_number invalid state restore handling (#74213)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
f721b9e3df
commit
555e9c6762
@ -1,6 +1,7 @@
|
|||||||
"""Support to set a numeric value from a slider or text box."""
|
"""Support to set a numeric value from a slider or text box."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from contextlib import suppress
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -281,8 +282,10 @@ class InputNumber(RestoreEntity):
|
|||||||
if self._current_value is not None:
|
if self._current_value is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
value: float | None = None
|
||||||
value = state and float(state.state)
|
if state := await self.async_get_last_state():
|
||||||
|
with suppress(ValueError):
|
||||||
|
value = float(state.state)
|
||||||
|
|
||||||
# Check against None because value can be 0
|
# Check against None because value can be 0
|
||||||
if value is not None and self._minimum <= value <= self._maximum:
|
if value is not None and self._minimum <= value <= self._maximum:
|
||||||
|
@ -255,6 +255,29 @@ async def test_restore_state(hass):
|
|||||||
assert float(state.state) == 10
|
assert float(state.state) == 10
|
||||||
|
|
||||||
|
|
||||||
|
async def test_restore_invalid_state(hass):
|
||||||
|
"""Ensure an invalid restore state is handled."""
|
||||||
|
mock_restore_cache(
|
||||||
|
hass, (State("input_number.b1", "="), State("input_number.b2", "200"))
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.state = CoreState.starting
|
||||||
|
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
DOMAIN,
|
||||||
|
{DOMAIN: {"b1": {"min": 2, "max": 100}, "b2": {"min": 10, "max": 100}}},
|
||||||
|
)
|
||||||
|
|
||||||
|
state = hass.states.get("input_number.b1")
|
||||||
|
assert state
|
||||||
|
assert float(state.state) == 2
|
||||||
|
|
||||||
|
state = hass.states.get("input_number.b2")
|
||||||
|
assert state
|
||||||
|
assert float(state.state) == 10
|
||||||
|
|
||||||
|
|
||||||
async def test_initial_state_overrules_restore_state(hass):
|
async def test_initial_state_overrules_restore_state(hass):
|
||||||
"""Ensure states are restored on startup."""
|
"""Ensure states are restored on startup."""
|
||||||
mock_restore_cache(
|
mock_restore_cache(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user