Restore states through a JSON store instead of recorder (#17270)

* Restore states through a JSON store

* Accept entity_id directly in restore state helper

* Keep states stored between runs for a limited time

* Remove warning
This commit is contained in:
Adam Mills authored and Paulus Schoutsen committed 2018-11-28 13:16:43 +01:00
1 parent a039c3209b
commit 5c3a4e3d10
46 files changed
+493 -422

No files matched your search

+5 -6
View File
@@ -24,7 +24,7 @@ from homeassistant.components import mqtt, switch
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import HomeAssistantType, ConfigType
from homeassistant.helpers.restore_state import async_get_last_state
from homeassistant.helpers.restore_state import RestoreEntity
_LOGGER = logging.getLogger(__name__)
@@ -102,8 +102,9 @@ async def _async_setup_entity(hass, config, async_add_entities,
async_add_entities([newswitch])
# pylint: disable=too-many-ancestors
class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
SwitchDevice):
SwitchDevice, RestoreEntity):
"""Representation of a switch that can be toggled using MQTT."""
def __init__(self, name, icon,
@@ -136,8 +137,7 @@ class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
async def async_added_to_hass(self):
"""Subscribe to MQTT events."""
await MqttAvailability.async_added_to_hass(self)
await MqttDiscoveryUpdate.async_added_to_hass(self)
await super().async_added_to_hass()
@callback
def state_message_received(topic, payload, qos):
@@ -161,8 +161,7 @@ class MqttSwitch(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
self._qos)
if self._optimistic:
last_state = await async_get_last_state(self.hass,
self.entity_id)
last_state = await self.async_get_last_state()
if last_state:
self._state = last_state.state == STATE_ON
+4 -3
View File
@@ -13,7 +13,7 @@ from homeassistant.components import pilight
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, CONF_ID, CONF_SWITCHES, CONF_STATE,
CONF_PROTOCOL, STATE_ON)
from homeassistant.helpers.restore_state import async_get_last_state
from homeassistant.helpers.restore_state import RestoreEntity
_LOGGER = logging.getLogger(__name__)
@@ -97,7 +97,7 @@ class _ReceiveHandle:
switch.set_state(turn_on=turn_on, send_code=self.echo)
class PilightSwitch(SwitchDevice):
class PilightSwitch(SwitchDevice, RestoreEntity):
"""Representation of a Pilight switch."""
def __init__(self, hass, name, code_on, code_off, code_on_receive,
@@ -123,7 +123,8 @@ class PilightSwitch(SwitchDevice):
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
state = await async_get_last_state(self._hass, self.entity_id)
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
self._state = state.state == STATE_ON