From c7f5beb794031368d5d6af1b14a623aa84419b53 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 24 Jan 2019 17:53:01 -0800 Subject: [PATCH] history allowed to load states with invalid entity IDs (#20399) --- homeassistant/components/recorder/models.py | 3 +++ homeassistant/core.py | 7 +++++-- tests/components/recorder/test_models.py | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/recorder/models.py b/homeassistant/components/recorder/models.py index 7a655c29434..d1be17b83d5 100644 --- a/homeassistant/components/recorder/models.py +++ b/homeassistant/components/recorder/models.py @@ -131,6 +131,9 @@ class States(Base): # type: ignore _process_timestamp(self.last_changed), _process_timestamp(self.last_updated), context=context, + # Temp, because database can still store invalid entity IDs + # Remove with 1.0 or in 2020. + temp_invalid_id_bypass=True ) except ValueError: # When json.loads fails diff --git a/homeassistant/core.py b/homeassistant/core.py index 2fe999f6d13..6ddefd2022d 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -664,11 +664,14 @@ class State: attributes: Optional[Dict] = None, last_changed: Optional[datetime.datetime] = None, last_updated: Optional[datetime.datetime] = None, - context: Optional[Context] = None) -> None: + context: Optional[Context] = None, + # Temp, because database can still store invalid entity IDs + # Remove with 1.0 or in 2020. + temp_invalid_id_bypass: Optional[bool] = False) -> None: """Initialize a new state.""" state = str(state) - if not valid_entity_id(entity_id): + if not valid_entity_id(entity_id) and not temp_invalid_id_bypass: raise InvalidEntityFormatError(( "Invalid entity id encountered: {}. " "Format should be .").format(entity_id)) diff --git a/tests/components/recorder/test_models.py b/tests/components/recorder/test_models.py index 3d1beb3a642..b56a7632df3 100644 --- a/tests/components/recorder/test_models.py +++ b/tests/components/recorder/test_models.py @@ -142,3 +142,12 @@ class TestRecorderRuns(unittest.TestCase): assert sorted(run.entity_ids()) == ['sensor.humidity', 'sensor.lux'] assert run.entity_ids(in_run2) == ['sensor.humidity'] + + +def test_states_from_native_invalid_entity_id(): + """Test loading a state from an invalid entity ID.""" + event = States() + event.entity_id = "test.invalid__id" + event.attributes = "{}" + state = event.to_native() + assert state.entity_id == 'test.invalid__id'