From fd6fd765b2f3c13bd394a7711c6304937677c506 Mon Sep 17 00:00:00 2001 From: Jan Losinski Date: Tue, 22 Aug 2017 16:40:14 +0200 Subject: [PATCH] Pilight switch: restore last state after restart (#8580) * Pilight switch: restore last state after restart This uses the restore_state helper to set the last known state to pilight switches when the devices are initialized after a HA restart. Without this HA forget the state on every restart and needs to be told the sttae by retoggling the switches. This can cause unwanted effects as a switch toggling may emit an RF signal. * Make hound happy Signed-off-by: Jan Losinski * Remove entity_id generation as requested in review. * Make hound happy again. * fix comments * fix lint --- homeassistant/components/switch/pilight.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/pilight.py b/homeassistant/components/switch/pilight.py index b56367e80be..201aee0f58c 100644 --- a/homeassistant/components/switch/pilight.py +++ b/homeassistant/components/switch/pilight.py @@ -4,6 +4,7 @@ Support for switching devices via Pilight to on and off. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/switch.pilight/ """ +import asyncio import logging import voluptuous as vol @@ -12,7 +13,8 @@ import homeassistant.helpers.config_validation as cv import homeassistant.components.pilight as pilight from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) from homeassistant.const import (CONF_NAME, CONF_ID, CONF_SWITCHES, CONF_STATE, - CONF_PROTOCOL) + CONF_PROTOCOL, STATE_ON) +from homeassistant.helpers.restore_state import async_get_last_state _LOGGER = logging.getLogger(__name__) @@ -120,6 +122,13 @@ class PilightSwitch(SwitchDevice): if any(self._code_on_receive) or any(self._code_off_receive): hass.bus.listen(pilight.EVENT, self._handle_code) + @asyncio.coroutine + def async_added_to_hass(self): + """Call when entity about to be added to hass.""" + state = yield from async_get_last_state(self._hass, self.entity_id) + if state: + self._state = state.state == STATE_ON + @property def name(self): """Get the name of the switch.""" @@ -130,6 +139,11 @@ class PilightSwitch(SwitchDevice): """No polling needed, state set when correct code is received.""" return False + @property + def assumed_state(self): + """Return True if unable to access real state of the entity.""" + return True + @property def is_on(self): """Return true if switch is on."""