From bf8d7be74748a4deb16b81f2582fee6ef2a9d95d Mon Sep 17 00:00:00 2001 From: Hugo Bloem Date: Tue, 27 Aug 2024 16:39:01 +0000 Subject: [PATCH 1/2] fix: Retain RestoreOnDeactivate setting after reboot --- custom_components/stateful_scenes/switch.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/custom_components/stateful_scenes/switch.py b/custom_components/stateful_scenes/switch.py index 8dd86e7..9617b95 100644 --- a/custom_components/stateful_scenes/switch.py +++ b/custom_components/stateful_scenes/switch.py @@ -9,12 +9,13 @@ import voluptuous as vol from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity from homeassistant.config_entries import ConfigEntry -from homeassistant.const import EntityCategory +from homeassistant.const import EntityCategory, STATE_ON from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.restore_state import RestoreEntity from . import StatefulScenes from .const import ( @@ -165,7 +166,7 @@ def unregister_callback(self) -> None: self._scene.unregister_callback() -class RestoreOnDeactivate(SwitchEntity): +class RestoreOnDeactivate(SwitchEntity, RestoreEntity): """Switch entity to restore the scene on deactivation.""" _attr_name = "Restore On Deactivate" @@ -173,7 +174,7 @@ class RestoreOnDeactivate(SwitchEntity): _attr_should_poll = True _attr_assumed_state = True - def __init__(self, scene: StatefulScenes) -> None: + def __init__(self, scene: StatefulScenes.Scene) -> None: """Initialize.""" self._scene = scene self._name = f"{scene.name} Restore On Deactivate" @@ -221,3 +222,11 @@ def update(self) -> None: This is the only method that should fetch new data for Home Assistant. """ self._is_on = self._scene.restore_on_deactivate + + async def async_added_to_hass(self): + """Handle entity which will be added.""" + state = await self.async_get_last_state() + if not state: + return + self._scene.set_restore_on_deactivate(state.state == STATE_ON) + self._is_on = state.state == STATE_ON \ No newline at end of file From 1b7b30f515efd9758f08fbe7a9e9055882c03cbd Mon Sep 17 00:00:00 2001 From: Hugo Bloem Date: Tue, 27 Aug 2024 16:42:18 +0000 Subject: [PATCH 2/2] refactor: fix eof --- custom_components/stateful_scenes/switch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/stateful_scenes/switch.py b/custom_components/stateful_scenes/switch.py index 9617b95..76e049d 100644 --- a/custom_components/stateful_scenes/switch.py +++ b/custom_components/stateful_scenes/switch.py @@ -229,4 +229,4 @@ async def async_added_to_hass(self): if not state: return self._scene.set_restore_on_deactivate(state.state == STATE_ON) - self._is_on = state.state == STATE_ON \ No newline at end of file + self._is_on = state.state == STATE_ON