Modbus switch, allow restore "unknown" (#99533)

This commit is contained in:
jan iversen 2023-09-03 17:48:25 +02:00 committed by GitHub
parent c938b9e7a3
commit d19f617c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -21,6 +21,7 @@ from homeassistant.const import (
CONF_SLAVE,
CONF_STRUCTURE,
CONF_UNIQUE_ID,
STATE_OFF,
STATE_ON,
)
from homeassistant.core import callback
@ -309,7 +310,10 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
"""Handle entity which will be added."""
await self.async_base_added_to_hass()
if state := await self.async_get_last_state():
self._attr_is_on = state.state == STATE_ON
if state.state == STATE_ON:
self._attr_is_on = True
elif state.state == STATE_OFF:
self._attr_is_on = False
async def async_turn(self, command: int) -> None:
"""Evaluate switch result."""

View File

@ -250,7 +250,7 @@ async def test_lazy_error_switch(
@pytest.mark.parametrize(
"mock_test_state",
[(State(ENTITY_ID, STATE_ON),)],
[(State(ENTITY_ID, STATE_ON),), (State(ENTITY_ID, STATE_OFF),)],
indirect=True,
)
@pytest.mark.parametrize(