Add initial state to Flux Switch (#27089)

* flux restore state

* Add config options

* Add tests

* Add more tests

* just restores state
This commit is contained in:
Santobert
2019-10-05 21:57:12 +02:00
committed by Paulus Schoutsen
parent 5ae497bfdc
commit 601d15701b
2 changed files with 57 additions and 1 deletions

View File

@@ -31,10 +31,12 @@ from homeassistant.const import (
CONF_LIGHTS,
CONF_MODE,
SERVICE_TURN_ON,
STATE_ON,
SUN_EVENT_SUNRISE,
SUN_EVENT_SUNSET,
)
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.sun import get_astral_event_date
from homeassistant.util import slugify
from homeassistant.util.color import (
@@ -169,7 +171,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
hass.services.async_register(DOMAIN, service_name, async_update)
class FluxSwitch(SwitchDevice):
class FluxSwitch(SwitchDevice, RestoreEntity):
"""Representation of a Flux switch."""
def __init__(
@@ -214,6 +216,12 @@ class FluxSwitch(SwitchDevice):
"""Return true if switch is on."""
return self.unsub_tracker is not None
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
last_state = await self.async_get_last_state()
if last_state and last_state.state == STATE_ON:
await self.async_turn_on()
async def async_turn_on(self, **kwargs):
"""Turn on flux."""
if self.is_on: