mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Restore automation last_triggered as datetime & fix test (#24951)
* Restore automation last_triggered as datetime & fix test * last_triggered is always a string
This commit is contained in:
parent
073327831f
commit
c80683bb15
@ -18,7 +18,7 @@ from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.util.dt import utcnow
|
||||
from homeassistant.util.dt import parse_datetime, utcnow
|
||||
|
||||
DOMAIN = 'automation'
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
@ -227,7 +227,9 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
enable_automation = state.state == STATE_ON
|
||||
self._last_triggered = state.attributes.get('last_triggered')
|
||||
last_triggered = state.attributes.get('last_triggered')
|
||||
if last_triggered is not None:
|
||||
self._last_triggered = parse_datetime(last_triggered)
|
||||
_LOGGER.debug("Loaded automation %s with state %s from state "
|
||||
" storage last state %s", self.entity_id,
|
||||
enable_automation, state)
|
||||
|
@ -28,9 +28,11 @@ from homeassistant.const import (
|
||||
ATTR_DISCOVERED, ATTR_SERVICE, DEVICE_DEFAULT_NAME,
|
||||
EVENT_HOMEASSISTANT_CLOSE, EVENT_PLATFORM_DISCOVERED, EVENT_STATE_CHANGED,
|
||||
EVENT_TIME_CHANGED, SERVER_PORT, STATE_ON, STATE_OFF)
|
||||
from homeassistant.core import State
|
||||
from homeassistant.helpers import (
|
||||
area_registry, device_registry, entity, entity_platform, entity_registry,
|
||||
intent, restore_state, storage)
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
from homeassistant.util.async_ import (
|
||||
@ -761,9 +763,14 @@ def mock_restore_cache(hass, states):
|
||||
data = restore_state.RestoreStateData(hass)
|
||||
now = date_util.utcnow()
|
||||
|
||||
data.last_states = {
|
||||
state.entity_id: restore_state.StoredState(state, now)
|
||||
for state in states}
|
||||
last_states = {}
|
||||
for state in states:
|
||||
restored_state = state.as_dict()
|
||||
restored_state['attributes'] = json.loads(json.dumps(
|
||||
restored_state['attributes'], cls=JSONEncoder))
|
||||
last_states[state.entity_id] = restore_state.StoredState(
|
||||
State.from_dict(restored_state), now)
|
||||
data.last_states = last_states
|
||||
_LOGGER.debug('Restore cache: %s', data.last_states)
|
||||
assert len(data.last_states) == len(states), \
|
||||
"Duplicate entity_id? {}".format(states)
|
||||
|
Loading…
x
Reference in New Issue
Block a user