Fix restore state crashing invalid entity ID (#20367)

pull/20372/head
Paulus Schoutsen 2019-01-23 21:12:38 -08:00 committed by GitHub
parent 697c331903
commit af3afb673a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 3 deletions

View File

@ -4,7 +4,8 @@ import logging
from datetime import timedelta, datetime
from typing import Any, Dict, List, Set, Optional # noqa pylint_disable=unused-import
from homeassistant.core import HomeAssistant, callback, State, CoreState
from homeassistant.core import (
HomeAssistant, callback, State, CoreState, valid_entity_id)
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
import homeassistant.util.dt as dt_util
@ -80,7 +81,8 @@ class RestoreStateData():
else:
data.last_states = {
item['state']['entity_id']: StoredState.from_dict(item)
for item in stored_states}
for item in stored_states
if valid_entity_id(item['state']['entity_id'])}
_LOGGER.debug(
'Created cache with %s', list(data.last_states))

View File

@ -6,7 +6,8 @@ from homeassistant.core import CoreState, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.restore_state import (
RestoreStateData, RestoreEntity, StoredState, DATA_RESTORE_STATE_TASK)
RestoreStateData, RestoreEntity, StoredState, DATA_RESTORE_STATE_TASK,
STORAGE_KEY)
from homeassistant.util import dt as dt_util
from asynctest import patch
@ -218,3 +219,34 @@ async def test_state_saved_on_remove(hass):
# We should store the input boolean state when it is removed
assert data.last_states['input_boolean.b0'].state.state == 'on'
async def test_restoring_invalid_entity_id(hass, hass_storage):
"""Test restoring invalid entity IDs."""
entity = RestoreEntity()
entity.hass = hass
entity.entity_id = 'test.invalid__entity_id'
now = dt_util.utcnow().isoformat()
hass_storage[STORAGE_KEY] = {
'version': 1,
'key': STORAGE_KEY,
'data': [
{
'state': {
'entity_id': 'test.invalid__entity_id',
'state': 'off',
'attributes': {},
'last_changed': now,
'last_updated': now,
'context': {
'id': '3c2243ff5f30447eb12e7348cfd5b8ff',
'user_id': None
}
},
'last_seen': dt_util.utcnow().isoformat()
}
]
}
state = await entity.async_get_last_state()
assert state is None