Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RestoreOnDeactivate setting retention #109

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions custom_components/stateful_scenes/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -165,15 +166,15 @@ 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"
_attr_entity_category = EntityCategory.CONFIG
_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"
Expand Down Expand Up @@ -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