Fix restore state crashing invalid entity ID (#20367)
This commit is contained in:
parent
697c331903
commit
af3afb673a
2 changed files with 37 additions and 3 deletions
|
@ -4,7 +4,8 @@ import logging
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta, datetime
|
||||||
from typing import Any, Dict, List, Set, Optional # noqa pylint_disable=unused-import
|
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 (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
@ -80,7 +81,8 @@ class RestoreStateData():
|
||||||
else:
|
else:
|
||||||
data.last_states = {
|
data.last_states = {
|
||||||
item['state']['entity_id']: StoredState.from_dict(item)
|
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(
|
_LOGGER.debug(
|
||||||
'Created cache with %s', list(data.last_states))
|
'Created cache with %s', list(data.last_states))
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,8 @@ from homeassistant.core import CoreState, State
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.restore_state import (
|
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 homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from asynctest import patch
|
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
|
# We should store the input boolean state when it is removed
|
||||||
assert data.last_states['input_boolean.b0'].state.state == 'on'
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue