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
@@ -21,7 +21,7 @@ from homeassistant.const import (
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.event import track_point_in_time
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -116,7 +116,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
)])
|
||||
|
||||
|
||||
class ManualAlarm(alarm.AlarmControlPanel):
|
||||
class ManualAlarm(alarm.AlarmControlPanel, RestoreEntity):
|
||||
"""
|
||||
Representation of an alarm status.
|
||||
|
||||
@@ -310,7 +310,7 @@ class ManualAlarm(alarm.AlarmControlPanel):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added to hass."""
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
self._state = state.state
|
||||
self._state_ts = state.last_updated
|
||||
@@ -108,8 +108,7 @@ class MqttAlarm(MqttAvailability, MqttDiscoveryUpdate,
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe mqtt events."""
|
||||
await MqttAvailability.async_added_to_hass(self)
|
||||
await MqttDiscoveryUpdate.async_added_to_hass(self)
|
||||
await super().async_added_to_hass()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -21,7 +21,7 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import extract_domain_configs, script, condition
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.util.dt import utcnow
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
@@ -182,7 +182,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class AutomationEntity(ToggleEntity):
|
||||
class AutomationEntity(ToggleEntity, RestoreEntity):
|
||||
"""Entity to show status of entity."""
|
||||
|
||||
def __init__(self, automation_id, name, async_attach_triggers, cond_func,
|
||||
@@ -227,12 +227,13 @@ class AutomationEntity(ToggleEntity):
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Startup with initial state or previous state."""
|
||||
await super().async_added_to_hass()
|
||||
if self._initial_state is not None:
|
||||
enable_automation = self._initial_state
|
||||
_LOGGER.debug("Automation %s initial state %s from config "
|
||||
"initial_state", self.entity_id, enable_automation)
|
||||
else:
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
enable_automation = state.state == STATE_ON
|
||||
self._last_triggered = state.attributes.get('last_triggered')
|
||||
@@ -291,6 +292,7 @@ class AutomationEntity(ToggleEntity):
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Remove listeners when removing automation from HASS."""
|
||||
await super().async_will_remove_from_hass()
|
||||
await self.async_turn_off()
|
||||
|
||||
async def async_enable(self):
|
||||
|
||||
@@ -102,8 +102,7 @@ class MqttBinarySensor(MqttAvailability, MqttDiscoveryUpdate,
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe mqtt events."""
|
||||
await MqttAvailability.async_added_to_hass(self)
|
||||
await MqttDiscoveryUpdate.async_added_to_hass(self)
|
||||
await super().async_added_to_hass()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -23,7 +23,7 @@ from homeassistant.helpers import condition
|
||||
from homeassistant.helpers.event import (
|
||||
async_track_state_change, async_track_time_interval)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -96,7 +96,7 @@ async def async_setup_platform(hass, config, async_add_entities,
|
||||
precision)])
|
||||
|
||||
|
||||
class GenericThermostat(ClimateDevice):
|
||||
class GenericThermostat(ClimateDevice, RestoreEntity):
|
||||
"""Representation of a Generic Thermostat device."""
|
||||
|
||||
def __init__(self, hass, name, heater_entity_id, sensor_entity_id,
|
||||
@@ -155,8 +155,9 @@ class GenericThermostat(ClimateDevice):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added."""
|
||||
await super().async_added_to_hass()
|
||||
# Check If we have an old state
|
||||
old_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
old_state = await self.async_get_last_state()
|
||||
if old_state is not None:
|
||||
# If we have no initial temperature, restore
|
||||
if self._target_temp is None:
|
||||
|
||||
@@ -221,8 +221,7 @@ class MqttClimate(MqttAvailability, MqttDiscoveryUpdate, ClimateDevice):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle being added to home assistant."""
|
||||
await MqttAvailability.async_added_to_hass(self)
|
||||
await MqttDiscoveryUpdate.async_added_to_hass(self)
|
||||
await super().async_added_to_hass()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -10,9 +10,8 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -86,7 +85,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class Counter(Entity):
|
||||
class Counter(RestoreEntity):
|
||||
"""Representation of a counter."""
|
||||
|
||||
def __init__(self, object_id, name, initial, restore, step, icon):
|
||||
@@ -128,10 +127,11 @@ class Counter(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity about to be added to Home Assistant."""
|
||||
await super().async_added_to_hass()
|
||||
# __init__ will set self._state to self._initial, only override
|
||||
# if needed.
|
||||
if self._restore:
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
if state is not None:
|
||||
self._state = int(state.state)
|
||||
|
||||
|
||||
@@ -205,8 +205,7 @@ class MqttCover(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe MQTT events."""
|
||||
await MqttAvailability.async_added_to_hass(self)
|
||||
await MqttDiscoveryUpdate.async_added_to_hass(self)
|
||||
await super().async_added_to_hass()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -22,9 +22,8 @@ from homeassistant.components.zone.zone import async_active_zone
|
||||
from homeassistant.config import load_yaml_config_file, async_log_exception
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_per_platform, discovery
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.typing import GPSType, ConfigType, HomeAssistantType
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant import util
|
||||
@@ -396,7 +395,7 @@ class DeviceTracker:
|
||||
await asyncio.wait(tasks, loop=self.hass.loop)
|
||||
|
||||
|
||||
class Device(Entity):
|
||||
class Device(RestoreEntity):
|
||||
"""Represent a tracked device."""
|
||||
|
||||
host_name = None # type: str
|
||||
@@ -564,7 +563,8 @@ class Device(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Add an entity."""
|
||||
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 not state:
|
||||
return
|
||||
self._state = state.state
|
||||
|
||||
@@ -151,8 +151,7 @@ class MqttFan(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()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -38,20 +38,6 @@ SIGNIFICANT_DOMAINS = ('thermostat', 'climate')
|
||||
IGNORE_DOMAINS = ('zone', 'scene',)
|
||||
|
||||
|
||||
def last_recorder_run(hass):
|
||||
"""Retrieve the last closed recorder run from the database."""
|
||||
from homeassistant.components.recorder.models import RecorderRuns
|
||||
|
||||
with session_scope(hass=hass) as session:
|
||||
res = (session.query(RecorderRuns)
|
||||
.filter(RecorderRuns.end.isnot(None))
|
||||
.order_by(RecorderRuns.end.desc()).first())
|
||||
if res is None:
|
||||
return None
|
||||
session.expunge(res)
|
||||
return res
|
||||
|
||||
|
||||
def get_significant_states(hass, start_time, end_time=None, entity_ids=None,
|
||||
filters=None, include_start_time_state=True):
|
||||
"""
|
||||
|
||||
@@ -15,7 +15,7 @@ from homeassistant.loader import bind_hass
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
DOMAIN = 'input_boolean'
|
||||
|
||||
@@ -84,7 +84,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class InputBoolean(ToggleEntity):
|
||||
class InputBoolean(ToggleEntity, RestoreEntity):
|
||||
"""Representation of a boolean input."""
|
||||
|
||||
def __init__(self, object_id, name, initial, icon):
|
||||
@@ -117,10 +117,11 @@ class InputBoolean(ToggleEntity):
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity about to be added to hass."""
|
||||
# If not None, we got an initial value.
|
||||
await super().async_added_to_hass()
|
||||
if self._state is not None:
|
||||
return
|
||||
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
self._state = state and state.state == STATE_ON
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
|
||||
@@ -11,9 +11,8 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
|
||||
@@ -97,7 +96,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class InputDatetime(Entity):
|
||||
class InputDatetime(RestoreEntity):
|
||||
"""Representation of a datetime input."""
|
||||
|
||||
def __init__(self, object_id, name, has_date, has_time, icon, initial):
|
||||
@@ -112,6 +111,7 @@ class InputDatetime(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added."""
|
||||
await super().async_added_to_hass()
|
||||
restore_val = None
|
||||
|
||||
# Priority 1: Initial State
|
||||
@@ -120,7 +120,7 @@ class InputDatetime(Entity):
|
||||
|
||||
# Priority 2: Old state
|
||||
if restore_val is None:
|
||||
old_state = await async_get_last_state(self.hass, self.entity_id)
|
||||
old_state = await self.async_get_last_state()
|
||||
if old_state is not None:
|
||||
restore_val = old_state.state
|
||||
|
||||
|
||||
@@ -11,9 +11,8 @@ import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_NAME, CONF_MODE)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -123,7 +122,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class InputNumber(Entity):
|
||||
class InputNumber(RestoreEntity):
|
||||
"""Representation of a slider."""
|
||||
|
||||
def __init__(self, object_id, name, initial, minimum, maximum, step, icon,
|
||||
@@ -178,10 +177,11 @@ class InputNumber(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
if self._current_value is not None:
|
||||
return
|
||||
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
value = state and float(state.state)
|
||||
|
||||
# Check against None because value can be 0
|
||||
|
||||
@@ -10,9 +10,8 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -116,7 +115,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class InputSelect(Entity):
|
||||
class InputSelect(RestoreEntity):
|
||||
"""Representation of a select input."""
|
||||
|
||||
def __init__(self, object_id, name, initial, options, icon):
|
||||
@@ -129,10 +128,11 @@ class InputSelect(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added."""
|
||||
await super().async_added_to_hass()
|
||||
if self._current_option is not None:
|
||||
return
|
||||
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
if not state or state.state not in self._options:
|
||||
self._current_option = self._options[0]
|
||||
else:
|
||||
|
||||
@@ -11,9 +11,8 @@ import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, CONF_ICON, CONF_NAME, CONF_MODE)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -104,7 +103,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class InputText(Entity):
|
||||
class InputText(RestoreEntity):
|
||||
"""Represent a text box."""
|
||||
|
||||
def __init__(self, object_id, name, initial, minimum, maximum, icon,
|
||||
@@ -157,10 +156,11 @@ class InputText(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Run when entity about to be added to hass."""
|
||||
await super().async_added_to_hass()
|
||||
if self._current_value is not None:
|
||||
return
|
||||
|
||||
state = await async_get_last_state(self.hass, self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
value = state and state.state
|
||||
|
||||
# Check against None because value can be 0
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -111,8 +111,7 @@ class MqttLock(MqttAvailability, MqttDiscoveryUpdate, LockDevice):
|
||||
|
||||
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 message_received(topic, payload, qos):
|
||||
|
||||
@@ -840,6 +840,7 @@ class MqttAvailability(Entity):
|
||||
|
||||
This method must be run in the event loop and returns a coroutine.
|
||||
"""
|
||||
await super().async_added_to_hass()
|
||||
await self._availability_subscribe_topics()
|
||||
|
||||
async def availability_discovery_update(self, config: dict):
|
||||
@@ -900,6 +901,8 @@ class MqttDiscoveryUpdate(Entity):
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to discovery updates."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.components.mqtt.discovery import (
|
||||
ALREADY_DISCOVERED, MQTT_DISCOVERY_UPDATED)
|
||||
|
||||
@@ -28,7 +28,6 @@ import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entityfilter import generate_filter
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from . import migration, purge
|
||||
from .const import DATA_INSTANCE
|
||||
@@ -83,12 +82,6 @@ CONFIG_SCHEMA = vol.Schema({
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def wait_connection_ready(hass):
|
||||
"""Wait till the connection is ready."""
|
||||
return await hass.data[DATA_INSTANCE].async_db_ready
|
||||
|
||||
|
||||
def run_information(hass, point_in_time: Optional[datetime] = None):
|
||||
"""Return information about current run.
|
||||
|
||||
|
||||
@@ -10,9 +10,8 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import track_time_change
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
REQUIREMENTS = ['fastdotcom==0.0.3']
|
||||
@@ -51,7 +50,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
hass.services.register(DOMAIN, 'update_fastdotcom', update)
|
||||
|
||||
|
||||
class SpeedtestSensor(Entity):
|
||||
class SpeedtestSensor(RestoreEntity):
|
||||
"""Implementation of a FAst.com sensor."""
|
||||
|
||||
def __init__(self, speedtest_data):
|
||||
@@ -86,7 +85,8 @@ class SpeedtestSensor(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity which will be added."""
|
||||
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 not state:
|
||||
return
|
||||
self._state = state.state
|
||||
|
||||
@@ -119,8 +119,7 @@ class MqttSensor(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()
|
||||
await self._subscribe_topics()
|
||||
|
||||
async def discovery_update(self, discovery_payload):
|
||||
|
||||
@@ -11,9 +11,8 @@ import voluptuous as vol
|
||||
from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA
|
||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import track_time_change
|
||||
from homeassistant.helpers.restore_state import async_get_last_state
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
REQUIREMENTS = ['speedtest-cli==2.0.2']
|
||||
@@ -76,7 +75,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
hass.services.register(DOMAIN, 'update_speedtest', update)
|
||||
|
||||
|
||||
class SpeedtestSensor(Entity):
|
||||
class SpeedtestSensor(RestoreEntity):
|
||||
"""Implementation of a speedtest.net sensor."""
|
||||
|
||||
def __init__(self, speedtest_data, sensor_type):
|
||||
@@ -137,7 +136,8 @@ class SpeedtestSensor(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle all entity which are about to be added."""
|
||||
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 not state:
|
||||
return
|
||||
self._state = state.state
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import voluptuous as vol
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (ATTR_ENTITY_ID, CONF_ICON, CONF_NAME)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -97,7 +97,7 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class Timer(Entity):
|
||||
class Timer(RestoreEntity):
|
||||
"""Representation of a timer."""
|
||||
|
||||
def __init__(self, hass, object_id, name, icon, duration):
|
||||
@@ -146,8 +146,7 @@ class Timer(Entity):
|
||||
if self._state is not None:
|
||||
return
|
||||
|
||||
restore_state = self._hass.helpers.restore_state
|
||||
state = await restore_state.async_get_last_state(self.entity_id)
|
||||
state = await self.async_get_last_state()
|
||||
self._state = state and state.state == state
|
||||
|
||||
async def async_start(self, duration):
|
||||
|
||||
Reference in new issue
Block a user