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:
1 parent
a039c3209b
commit
5c3a4e3d10
46 files changed
+493
-422
No files matched your search
@@ -18,7 +18,7 @@ from homeassistant.components.light import (
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.util.color import (
|
||||
color_temperature_mired_to_kelvin, color_hs_to_RGB)
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
REQUIREMENTS = ['limitlessled==1.1.3']
|
||||
|
||||
@@ -157,7 +157,7 @@ def state(new_state):
|
||||
return decorator
|
||||
|
||||
|
||||
class LimitlessLEDGroup(Light):
|
||||
class LimitlessLEDGroup(Light, RestoreEntity):
|
||||
"""Representation of a LimitessLED group."""
|
||||
|
||||
def __init__(self, group, config):
|
||||
@@ -189,7 +189,8 @@ class LimitlessLEDGroup(Light):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity about to be added to hass event."""
|
||||
last_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
await super().async_added_to_hass()
|
||||
last_state = await self.async_get_last_state()
|
||||
if last_state:
|
||||
self._is_on = (last_state.state == STATE_ON)
|
||||
self._brightness = last_state.attributes.get('brightness')
|
||||
|
||||
@@ -22,7 +22,7 @@ from homeassistant.components.mqtt import (
|
||||
CONF_AVAILABILITY_TOPIC, CONF_COMMAND_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
||||
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, CONF_RETAIN, CONF_STATE_TOPIC,
|
||||
MqttAvailability, MqttDiscoveryUpdate)
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
@@ -166,7 +166,7 @@ async def async_setup_entity_basic(hass, config, async_add_entities,
|
||||
)])
|
||||
|
||||
|
||||
class MqttLight(MqttAvailability, MqttDiscoveryUpdate, Light):
|
||||
class MqttLight(MqttAvailability, MqttDiscoveryUpdate, Light, RestoreEntity):
|
||||
"""Representation of a MQTT light."""
|
||||
|
||||
def __init__(self, name, unique_id, effect_list, topic, templates,
|
||||
@@ -237,8 +237,7 @@ class MqttLight(MqttAvailability, MqttDiscoveryUpdate, Light):
|
||||
|
||||
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()
|
||||
|
||||
templates = {}
|
||||
for key, tpl in list(self._templates.items()):
|
||||
@@ -248,7 +247,7 @@ class MqttLight(MqttAvailability, MqttDiscoveryUpdate, Light):
|
||||
tpl.hass = self.hass
|
||||
templates[key] = tpl.async_render_with_possible_json_value
|
||||
|
||||
last_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
last_state = await self.async_get_last_state()
|
||||
|
||||
@callback
|
||||
def state_received(topic, payload, qos):
|
||||
|
||||
@@ -25,7 +25,7 @@ from homeassistant.const import (
|
||||
CONF_RGB, CONF_WHITE_VALUE, CONF_XY, STATE_ON)
|
||||
from homeassistant.core import callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||
import homeassistant.util.color as color_util
|
||||
|
||||
@@ -121,7 +121,8 @@ async def async_setup_entity_json(hass: HomeAssistantType, config: ConfigType,
|
||||
)])
|
||||
|
||||
|
||||
class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate, Light):
|
||||
class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate, Light,
|
||||
RestoreEntity):
|
||||
"""Representation of a MQTT JSON light."""
|
||||
|
||||
def __init__(self, name, unique_id, effect_list, topic, qos, retain,
|
||||
@@ -183,10 +184,9 @@ class MqttLightJson(MqttAvailability, MqttDiscoveryUpdate, Light):
|
||||
|
||||
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()
|
||||
|
||||
last_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
last_state = await self.async_get_last_state()
|
||||
|
||||
@callback
|
||||
def state_received(topic, payload, qos):
|
||||
|
||||
@@ -21,7 +21,7 @@ from homeassistant.components.mqtt import (
|
||||
MqttAvailability)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import homeassistant.util.color as color_util
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -102,7 +102,7 @@ async def async_setup_entity_template(hass, config, async_add_entities,
|
||||
)])
|
||||
|
||||
|
||||
class MqttTemplate(MqttAvailability, Light):
|
||||
class MqttTemplate(MqttAvailability, Light, RestoreEntity):
|
||||
"""Representation of a MQTT Template light."""
|
||||
|
||||
def __init__(self, hass, name, effect_list, topics, templates, optimistic,
|
||||
@@ -153,7 +153,7 @@ class MqttTemplate(MqttAvailability, Light):
|
||||
"""Subscribe to MQTT events."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
last_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
last_state = await self.async_get_last_state()
|
||||
|
||||
@callback
|
||||
def state_received(topic, payload, qos):
|
||||
|
||||
Reference in new issue
Block a user