response
stringlengths
1
33.1k
instruction
stringlengths
22
582k
Return the path to the schema module.
def get_schema_module_path(schema_version_postfix: str) -> str: """Return the path to the schema module.""" return f"tests.components.recorder.db_schema_{schema_version_postfix}"
Fixture to initialize the db with the old schema.
def old_db_schema(schema_version_postfix: str) -> Iterator[None]: """Fixture to initialize the db with the old schema.""" schema_module = get_schema_module_path(schema_version_postfix) importlib.import_module(schema_module) old_db_schema = sys.modules[schema_module] with ( patch.object(recorder, "db_schema", old_db_schema), patch.object( recorder.migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION ), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), patch.object(core, "StateAttributes", old_db_schema.StateAttributes), patch.object(migration.EntityIDMigration, "task", core.RecorderTask), patch( CREATE_ENGINE_TARGET, new=partial( create_engine_test_for_schema_version_postfix, schema_version_postfix=schema_version_postfix, ), ), ): yield
Process a timestamp into datetime object.
def _process_timestamp(ts): """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into datetime object.
def process_timestamp(ts): """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts): """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts): """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts): """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts: datetime | None) -> str | None: """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts: datetime | None) -> str | None: """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts: datetime | None) -> str | None: """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts: datetime | None) -> str | None: """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into UTC isotime.
def process_timestamp_to_utc_isoformat(ts: datetime | None) -> str | None: """Process a timestamp into UTC isotime.""" if ts is None: return None if ts.tzinfo == dt_util.UTC: return ts.isoformat() if ts.tzinfo is None: return f"{ts.isoformat()}{DB_TIMEZONE}" return ts.astimezone(dt_util.UTC).isoformat()
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Process a timestamp into datetime object.
def process_timestamp(ts: datetime | None) -> datetime | None: """Process a timestamp into datetime object.""" if ts is None: return None if ts.tzinfo is None: return ts.replace(tzinfo=dt_util.UTC) return dt_util.as_utc(ts)
Compile UnusedDateTime and Unused as CHAR(0) on mysql, mariadb, and sqlite.
def compile_char_zero(type_: TypeDecorator, compiler: Any, **kw: Any) -> str: """Compile UnusedDateTime and Unused as CHAR(0) on mysql, mariadb, and sqlite.""" return "CHAR(0)"
Compile Unused as CHAR(1) on postgresql.
def compile_char_one(type_: TypeDecorator, compiler: Any, **kw: Any) -> str: """Compile Unused as CHAR(1) on postgresql.""" return "CHAR(1)"
Test states meta is migrated when entity_id is changed.
def test_rename_entity_without_collision( hass_recorder: Callable[..., HomeAssistant], caplog: pytest.LogCaptureFixture ) -> None: """Test states meta is migrated when entity_id is changed.""" hass = hass_recorder() setup_component(hass, "sensor", {}) entity_reg = mock_registry(hass) @callback def add_entry(): reg_entry = entity_reg.async_get_or_create( "sensor", "test", "unique_0000", suggested_object_id="test1", ) assert reg_entry.entity_id == "sensor.test1" hass.add_job(add_entry) hass.block_till_done() zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist) @callback def rename_entry(): entity_reg.async_update_entity("sensor.test1", new_entity_id="sensor.test99") hass.add_job(rename_entry) wait_recording_done(hass) hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) states["sensor.test99"] = states.pop("sensor.test1") assert_dict_of_states_equal_without_context_and_last_changed(states, hist) hass.states.set("sensor.test99", "post_migrate") wait_recording_done(hass) new_hist = history.get_significant_states( hass, zero, dt_util.utcnow(), list(set(states) | {"sensor.test99", "sensor.test1"}), ) assert not new_hist.get("sensor.test1") assert new_hist["sensor.test99"][-1].state == "post_migrate" with session_scope(hass=hass) as session: assert _count_entity_id_in_states_meta(hass, session, "sensor.test99") == 1 assert _count_entity_id_in_states_meta(hass, session, "sensor.test1") == 1 assert "the new entity_id is already in use" not in caplog.text
Test states meta is not migrated when there is a collision.
def test_rename_entity_collision( hass_recorder: Callable[..., HomeAssistant], caplog: pytest.LogCaptureFixture ) -> None: """Test states meta is not migrated when there is a collision.""" hass = hass_recorder() setup_component(hass, "sensor", {}) entity_reg = mock_registry(hass) @callback def add_entry(): reg_entry = entity_reg.async_get_or_create( "sensor", "test", "unique_0000", suggested_object_id="test1", ) assert reg_entry.entity_id == "sensor.test1" hass.add_job(add_entry) hass.block_till_done() zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist) assert len(hist["sensor.test1"]) == 3 hass.states.set("sensor.test99", "collision") hass.states.remove("sensor.test99") hass.block_till_done() # Rename entity sensor.test1 to sensor.test99 @callback def rename_entry(): entity_reg.async_update_entity("sensor.test1", new_entity_id="sensor.test99") hass.add_job(rename_entry) wait_recording_done(hass) # History is not migrated on collision hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert len(hist["sensor.test1"]) == 3 assert len(hist["sensor.test99"]) == 2 with session_scope(hass=hass) as session: assert _count_entity_id_in_states_meta(hass, session, "sensor.test99") == 1 hass.states.set("sensor.test99", "post_migrate") wait_recording_done(hass) new_hist = history.get_significant_states( hass, zero, dt_util.utcnow(), list(set(states) | {"sensor.test99", "sensor.test1"}), ) assert new_hist["sensor.test99"][-1].state == "post_migrate" assert len(hist["sensor.test99"]) == 2 with session_scope(hass=hass) as session: assert _count_entity_id_in_states_meta(hass, session, "sensor.test99") == 1 assert _count_entity_id_in_states_meta(hass, session, "sensor.test1") == 1 # We should hit the safeguard in the states_meta_manager assert "the new entity_id is already in use" in caplog.text # We should not hit the safeguard in the entity_registry assert "Blocked attempt to insert duplicated state rows" not in caplog.text
Test states meta is not migrated when there is a collision. This test disables the safeguard in the states_meta_manager and relies on the filter_unique_constraint_integrity_error safeguard.
def test_rename_entity_collision_without_states_meta_safeguard( hass_recorder: Callable[..., HomeAssistant], caplog: pytest.LogCaptureFixture ) -> None: """Test states meta is not migrated when there is a collision. This test disables the safeguard in the states_meta_manager and relies on the filter_unique_constraint_integrity_error safeguard. """ hass = hass_recorder() setup_component(hass, "sensor", {}) entity_reg = mock_registry(hass) @callback def add_entry(): reg_entry = entity_reg.async_get_or_create( "sensor", "test", "unique_0000", suggested_object_id="test1", ) assert reg_entry.entity_id == "sensor.test1" hass.add_job(add_entry) hass.block_till_done() zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist) assert len(hist["sensor.test1"]) == 3 hass.states.set("sensor.test99", "collision") hass.states.remove("sensor.test99") hass.block_till_done() wait_recording_done(hass) # Verify history before collision hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert len(hist["sensor.test1"]) == 3 assert len(hist["sensor.test99"]) == 2 instance = recorder.get_instance(hass) # Patch out the safeguard in the states meta manager # so that we hit the filter_unique_constraint_integrity_error safeguard in the entity_registry with patch.object(instance.states_meta_manager, "get", return_value=None): # Rename entity sensor.test1 to sensor.test99 @callback def rename_entry(): entity_reg.async_update_entity( "sensor.test1", new_entity_id="sensor.test99" ) hass.add_job(rename_entry) wait_recording_done(hass) # History is not migrated on collision hist = history.get_significant_states( hass, zero, four, list(set(states) | {"sensor.test99", "sensor.test1"}) ) assert len(hist["sensor.test1"]) == 3 assert len(hist["sensor.test99"]) == 2 with session_scope(hass=hass) as session: assert _count_entity_id_in_states_meta(hass, session, "sensor.test99") == 1 hass.states.set("sensor.test99", "post_migrate") wait_recording_done(hass) new_hist = history.get_significant_states( hass, zero, dt_util.utcnow(), list(set(states) | {"sensor.test99", "sensor.test1"}), ) assert new_hist["sensor.test99"][-1].state == "post_migrate" assert len(hist["sensor.test99"]) == 2 with session_scope(hass=hass) as session: assert _count_entity_id_in_states_meta(hass, session, "sensor.test99") == 1 assert _count_entity_id_in_states_meta(hass, session, "sensor.test1") == 1 # We should not hit the safeguard in the states_meta_manager assert "the new entity_id is already in use" not in caplog.text # We should hit the safeguard in the entity_registry assert "Blocked attempt to insert duplicated state rows" in caplog.text
Test we can extract a filter from configuration without altering it.
def test_extract_include_exclude_filter_conf() -> None: """Test we can extract a filter from configuration without altering it.""" include_filter = extract_include_exclude_filter_conf(SIMPLE_INCLUDE_FILTER) assert include_filter == { CONF_EXCLUDE: { CONF_DOMAINS: set(), CONF_ENTITIES: set(), CONF_ENTITY_GLOBS: set(), }, CONF_INCLUDE: { CONF_DOMAINS: {"homeassistant"}, CONF_ENTITIES: {"sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*"}, }, } exclude_filter = extract_include_exclude_filter_conf(SIMPLE_EXCLUDE_FILTER) assert exclude_filter == { CONF_INCLUDE: { CONF_DOMAINS: set(), CONF_ENTITIES: set(), CONF_ENTITY_GLOBS: set(), }, CONF_EXCLUDE: { CONF_DOMAINS: {"homeassistant"}, CONF_ENTITIES: {"sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*"}, }, } include_exclude_filter = extract_include_exclude_filter_conf( SIMPLE_INCLUDE_EXCLUDE_FILTER ) assert include_exclude_filter == { CONF_INCLUDE: { CONF_DOMAINS: {"homeassistant"}, CONF_ENTITIES: {"sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*"}, }, CONF_EXCLUDE: { CONF_DOMAINS: {"homeassistant"}, CONF_ENTITIES: {"sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*"}, }, } include_exclude_filter[CONF_EXCLUDE][CONF_ENTITIES] = {"cover.altered"} # verify it really is a copy assert SIMPLE_INCLUDE_EXCLUDE_FILTER[CONF_EXCLUDE][CONF_ENTITIES] != { "cover.altered" } empty_include_filter = extract_include_exclude_filter_conf(EMPTY_INCLUDE_FILTER) assert empty_include_filter == { CONF_EXCLUDE: { CONF_DOMAINS: set(), CONF_ENTITIES: set(), CONF_ENTITY_GLOBS: set(), }, CONF_INCLUDE: { CONF_DOMAINS: set(), CONF_ENTITIES: set(), CONF_ENTITY_GLOBS: set(), }, }
Test we can merge two filters together.
def test_merge_include_exclude_filters() -> None: """Test we can merge two filters together.""" include_exclude_filter_base = extract_include_exclude_filter_conf( SIMPLE_INCLUDE_EXCLUDE_FILTER ) include_filter_add = extract_include_exclude_filter_conf( SIMPLE_INCLUDE_FILTER_DIFFERENT_ENTITIES ) merged_filter = merge_include_exclude_filters( include_exclude_filter_base, include_filter_add ) assert merged_filter == { CONF_EXCLUDE: { CONF_DOMAINS: {"homeassistant"}, CONF_ENTITIES: {"sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*"}, }, CONF_INCLUDE: { CONF_DOMAINS: {"other", "homeassistant"}, CONF_ENTITIES: {"not_sensor.one", "sensor.one"}, CONF_ENTITY_GLOBS: {"climate.*", "not_climate.*"}, }, }
Fixture to initialize the db with the old schema 32.
def db_schema_32(): """Fixture to initialize the db with the old schema 32.""" with old_db_schema("32"): yield
Test getting states at a specific point in time for entities that never have been recorded.
def test_get_full_significant_states_with_session_entity_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) with session_scope(hass=hass, read_only=True) as session: assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"] ) == {} ) assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], ) == {} )
Test getting states at a specific point in time for entities that never have been recorded.
def test_significant_states_with_session_entity_minimal_response_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) with session_scope(hass=hass, read_only=True) as session: assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"], minimal_response=True, ) == {} ) assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], minimal_response=True, ) == {} )
Test get_significant_states_with_session with a single entity.
def test_significant_states_with_session_single_entity( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_significant_states_with_session with a single entity.""" hass = hass_recorder() hass.states.set("demo.id", "any", {"attr": True}) hass.states.set("demo.id", "any2", {"attr": True}) wait_recording_done(hass) now = dt_util.utcnow() with session_scope(hass=hass, read_only=True) as session: states = history.get_significant_states_with_session( hass, session, now - timedelta(days=1), now, entity_ids=["demo.id"], minimal_response=False, ) assert len(states["demo.id"]) == 2
Test state change during period.
def test_state_changes_during_period( hass_recorder: Callable[..., HomeAssistant], attributes, no_attributes, limit ) -> None: """Test state change during period.""" hass = hass_recorder() entity_id = "media_player.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state, attributes) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point = start + timedelta(seconds=1) end = point + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [ set_state("idle"), set_state("Netflix"), set_state("Plex"), set_state("YouTube"), ] freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes, limit=limit ) assert_multiple_states_equal_without_context(states[:limit], hist[entity_id])
Test state change during period.
def test_state_changes_during_period_last_reported( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period.""" hass = hass_recorder() entity_id = "media_player.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point1 = start + timedelta(seconds=1) point2 = point1 + timedelta(seconds=1) end = point2 + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") freezer.move_to(point1) set_state("YouTube") freezer.move_to(point2) states = [set_state("YouTube")] freezer.move_to(end) set_state("Netflix") hist = history.state_changes_during_period(hass, start, end, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Test state change during period descending.
def test_state_changes_during_period_descending( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period descending.""" hass = hass_recorder() entity_id = "media_player.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state, {"any": 1}) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow().replace(microsecond=0) point = start + timedelta(seconds=1) point2 = start + timedelta(seconds=1, microseconds=100) point3 = start + timedelta(seconds=1, microseconds=200) point4 = start + timedelta(seconds=1, microseconds=300) end = point + timedelta(seconds=1, microseconds=400) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [set_state("idle")] freezer.move_to(point2) states.append(set_state("Netflix")) freezer.move_to(point3) states.append(set_state("Plex")) freezer.move_to(point4) states.append(set_state("YouTube")) freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=False ) assert_multiple_states_equal_without_context(states, hist[entity_id]) hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=True ) assert_multiple_states_equal_without_context( states, list(reversed(list(hist[entity_id]))) ) start_time = point2 + timedelta(microseconds=10) hist = history.state_changes_during_period( hass, start_time, # Pick a point where we will generate a start time state end, entity_id, no_attributes=False, descending=True, include_start_time_state=True, ) hist_states = list(hist[entity_id]) assert hist_states[-1].last_updated == start_time assert hist_states[-1].last_changed == start_time assert len(hist_states) == 3 # Make sure they are in descending order assert ( hist_states[0].last_updated > hist_states[1].last_updated > hist_states[2].last_updated ) assert ( hist_states[0].last_changed > hist_states[1].last_changed > hist_states[2].last_changed ) hist = history.state_changes_during_period( hass, start_time, # Pick a point where we will generate a start time state end, entity_id, no_attributes=False, descending=False, include_start_time_state=True, ) hist_states = list(hist[entity_id]) assert hist_states[0].last_updated == start_time assert hist_states[0].last_changed == start_time assert len(hist_states) == 3 # Make sure they are in ascending order assert ( hist_states[0].last_updated < hist_states[1].last_updated < hist_states[2].last_updated ) assert ( hist_states[0].last_changed < hist_states[1].last_changed < hist_states[2].last_changed )
Test number of state changes.
def test_get_last_state_changes(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test number of state changes.""" hass = hass_recorder() entity_id = "sensor.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) point2 = point + timedelta(minutes=1, seconds=1) states = [] with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) states.append(set_state("2")) freezer.move_to(point2) states.append(set_state("3")) hist = history.get_last_state_changes(hass, 2, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Test number of state changes.
def test_get_last_state_changes_last_reported( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test number of state changes.""" hass = hass_recorder() entity_id = "sensor.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) point2 = point + timedelta(minutes=1, seconds=1) states = [] with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) states.append(set_state("1")) freezer.move_to(point2) states.append(set_state("2")) hist = history.get_last_state_changes(hass, 2, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Test getting the last state change for an entity.
def test_get_last_state_change(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test getting the last state change for an entity.""" hass = hass_recorder() entity_id = "sensor.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) point2 = point + timedelta(minutes=1, seconds=1) states = [] with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) set_state("2") freezer.move_to(point2) states.append(set_state("3")) hist = history.get_last_state_changes(hass, 1, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Ensure a state can pass though copy(). The filter integration uses copy() on states from history.
def test_ensure_state_can_be_copied( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Ensure a state can pass though copy(). The filter integration uses copy() on states from history. """ hass = hass_recorder() entity_id = "sensor.test" def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) set_state("2") hist = history.get_last_state_changes(hass, 2, entity_id) assert_states_equal_without_context(copy(hist[entity_id][0]), hist[entity_id][0]) assert_states_equal_without_context(copy(hist[entity_id][1]), hist[entity_id][1])
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() zero, four, states = record_states(hass) hist = history.get_significant_states(hass, zero, four, entity_ids=list(states)) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_minimal_response( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, minimal_response=True, entity_ids=list(states) ) entites_with_reducable_states = [ "media_player.test", "media_player.test3", ] # All states for media_player.test state are reduced # down to last_changed and state when minimal_response # is set except for the first state. # is set. We use JSONEncoder to make sure that are # pre-encoded last_changed is always the same as what # will happen with encoding a native state for entity_id in entites_with_reducable_states: entity_states = states[entity_id] for state_idx in range(1, len(entity_states)): input_state = entity_states[state_idx] orig_last_changed = json.dumps( process_timestamp(input_state.last_changed), cls=JSONEncoder, ).replace('"', "") orig_state = input_state.state entity_states[state_idx] = { "last_changed": orig_last_changed, "state": orig_state, } assert len(hist) == len(states) assert_states_equal_without_context( states["media_player.test"][0], hist["media_player.test"][0] ) assert states["media_player.test"][1] == hist["media_player.test"][1] assert states["media_player.test"][2] == hist["media_player.test"][2] assert_multiple_states_equal_without_context( states["media_player.test2"], hist["media_player.test2"] ) assert_states_equal_without_context( states["media_player.test3"][0], hist["media_player.test3"][0] ) assert states["media_player.test3"][1] == hist["media_player.test3"][1] assert_multiple_states_equal_without_context( states["script.can_cancel_this_one"], hist["script.can_cancel_this_one"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test2"], hist["thermostat.test2"] )
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_with_initial( time_zone, hass_recorder: Callable[..., HomeAssistant] ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() hass.config.set_time_zone(time_zone) zero, four, states = record_states(hass) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: if entity_id == "media_player.test": states[entity_id] = states[entity_id][1:] for state in states[entity_id]: # If the state is recorded before the start time # start it will have its last_updated and last_changed # set to the start time. if state.last_updated < one_and_half: state.last_updated = one_and_half state.last_changed = one_and_half hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=True, entity_ids=list(states) ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_without_initial( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: states[entity_id] = [ s for s in states[entity_id] if s.last_changed not in (one, one_with_microsecond) ] del states["media_player.test2"] del states["thermostat.test3"] hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=False, entity_ids=list(states), ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_entity_id( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() zero, four, states = record_states(hass) del states["media_player.test2"] del states["media_player.test3"] del states["thermostat.test"] del states["thermostat.test2"] del states["thermostat.test3"] del states["script.can_cancel_this_one"] hist = history.get_significant_states(hass, zero, four, ["media_player.test"]) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_multiple_entity_ids( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, ["media_player.test", "thermostat.test"], ) assert_multiple_states_equal_without_context_and_last_changed( states["media_player.test"], hist["media_player.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] )
Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order.
def test_get_significant_states_are_ordered( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order. """ hass = hass_recorder() zero, four, _states = record_states(hass) entity_ids = ["media_player.test", "media_player.test2"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids entity_ids = ["media_player.test2", "media_player.test"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids
Test significant states when significant_states_only is set.
def test_get_significant_states_only( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test significant states when significant_states_only is set.""" hass = hass_recorder() entity_id = "sensor.test" def set_state(state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=4) points = [start + timedelta(minutes=i) for i in range(1, 4)] states = [] with freeze_time(start) as freezer: set_state("123", attributes={"attribute": 10.64}) freezer.move_to(points[0]) # Attributes are different, state not states.append(set_state("123", attributes={"attribute": 21.42})) freezer.move_to(points[1]) # state is different, attributes not states.append(set_state("32", attributes={"attribute": 21.42})) freezer.move_to(points[2]) # everything is different states.append(set_state("412", attributes={"attribute": 54.23})) hist = history.get_significant_states( hass, start, significant_changes_only=True, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 2 assert not any( state.last_updated == states[0].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[1].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[2].last_updated for state in hist[entity_id] ) hist = history.get_significant_states( hass, start, significant_changes_only=False, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 3 assert_multiple_states_equal_without_context_and_last_changed( states, hist[entity_id] )
Record some test states. We inject a bunch of state updates from media player, zone and thermostat.
def record_states(hass) -> tuple[datetime, datetime, dict[str, list[State]]]: """Record some test states. We inject a bunch of state updates from media player, zone and thermostat. """ mp = "media_player.test" mp2 = "media_player.test2" mp3 = "media_player.test3" therm = "thermostat.test" therm2 = "thermostat.test2" therm3 = "thermostat.test3" zone = "zone.home" script_c = "script.can_cancel_this_one" def set_state(entity_id, state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) zero = dt_util.utcnow() one = zero + timedelta(seconds=1) two = one + timedelta(seconds=1) three = two + timedelta(seconds=1) four = three + timedelta(seconds=1) states = {therm: [], therm2: [], therm3: [], mp: [], mp2: [], mp3: [], script_c: []} with freeze_time(one) as freezer: states[mp].append( set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[mp2].append( set_state(mp2, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) states[mp3].append( set_state(mp3, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[therm].append( set_state(therm, 20, attributes={"current_temperature": 19.5}) ) # This state will be updated set_state(therm3, 20, attributes={"current_temperature": 19.5}) freezer.move_to(one + timedelta(microseconds=1)) states[mp].append( set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) freezer.move_to(two) # This state will be skipped only different in time set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)}) # This state will be skipped because domain is excluded set_state(zone, "zoning") states[script_c].append( set_state(script_c, "off", attributes={"can_cancel": True}) ) states[therm].append( set_state(therm, 21, attributes={"current_temperature": 19.8}) ) states[therm2].append( set_state(therm2, 20, attributes={"current_temperature": 19}) ) # This state will be updated set_state(therm3, 20, attributes={"current_temperature": 19.5}) freezer.move_to(three) states[mp].append( set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)}) ) states[mp3].append( set_state(mp3, "Netflix", attributes={"media_title": str(sentinel.mt3)}) ) # Attributes changed even though state is the same states[therm].append( set_state(therm, 21, attributes={"current_temperature": 20}) ) states[therm3].append( set_state(therm3, 20, attributes={"current_temperature": 19.5}) ) return zero, four, states
Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results.
def test_state_changes_during_period_multiple_entities_single_test( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results. """ hass = hass_recorder() start = dt_util.utcnow() test_entites = {f"sensor.{i}": str(i) for i in range(30)} for entity_id, value in test_entites.items(): hass.states.set(entity_id, value) wait_recording_done(hass) end = dt_util.utcnow() for entity_id, value in test_entites.items(): hist = history.state_changes_during_period(hass, start, end, entity_id) assert len(hist) == 1 assert hist[entity_id][0].state == value
Test at least one entity id is required for get_significant_states.
def test_get_significant_states_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for get_significant_states.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_ids must be provided"): history.get_significant_states(hass, now, None)
Test at least one entity id is required for state_changes_during_period.
def test_state_changes_during_period_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for state_changes_during_period.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_id must be provided"): history.state_changes_during_period(hass, now, None)
Test passing filters is no longer supported.
def test_get_significant_states_with_filters_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test passing filters is no longer supported.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(NotImplementedError, match="Filters are no longer supported"): history.get_significant_states( hass, now, None, ["media_player.test"], Filters() )
Test get_significant_states returns an empty dict when entities not in the db.
def test_get_significant_states_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_significant_states returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert history.get_significant_states(hass, now, None, ["nonexistent.entity"]) == {}
Test state_changes_during_period returns an empty dict when entities not in the db.
def test_state_changes_during_period_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state_changes_during_period returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert ( history.state_changes_during_period(hass, now, None, "nonexistent.entity") == {} )
Test get_last_state_changes returns an empty dict when entities not in the db.
def test_get_last_state_changes_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_last_state_changes returns an empty dict when entities not in the db.""" hass = hass_recorder() assert history.get_last_state_changes(hass, 1, "nonexistent.entity") == {}
Fixture to initialize the db with the old schema 30.
def db_schema_30(): """Fixture to initialize the db with the old schema 30.""" with old_db_schema("30"): yield
Test getting states at a specific point in time for entities that never have been recorded.
def test_get_full_significant_states_with_session_entity_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) instance = recorder.get_instance(hass) with ( session_scope(hass=hass) as session, patch.object(instance.states_meta_manager, "active", False), ): assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"] ) == {} ) assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], ) == {} )
Test getting states at a specific point in time for entities that never have been recorded.
def test_significant_states_with_session_entity_minimal_response_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) instance = recorder.get_instance(hass) with ( session_scope(hass=hass) as session, patch.object(instance.states_meta_manager, "active", False), ): assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"], minimal_response=True, ) == {} ) assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], minimal_response=True, ) == {} )
Test state change during period.
def test_state_changes_during_period( hass_recorder: Callable[..., HomeAssistant], attributes, no_attributes, limit ) -> None: """Test state change during period.""" hass = hass_recorder() entity_id = "media_player.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state, attributes) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point = start + timedelta(seconds=1) end = point + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [ set_state("idle"), set_state("Netflix"), set_state("Plex"), set_state("YouTube"), ] freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes, limit=limit ) assert_multiple_states_equal_without_context(states[:limit], hist[entity_id])
Test state change during period descending.
def test_state_changes_during_period_descending( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period descending.""" hass = hass_recorder() entity_id = "media_player.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state, {"any": 1}) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point = start + timedelta(seconds=1) point2 = start + timedelta(seconds=1, microseconds=2) point3 = start + timedelta(seconds=1, microseconds=3) point4 = start + timedelta(seconds=1, microseconds=4) end = point + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [set_state("idle")] freezer.move_to(point2) states.append(set_state("Netflix")) freezer.move_to(point3) states.append(set_state("Plex")) freezer.move_to(point4) states.append(set_state("YouTube")) freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=False ) assert_multiple_states_equal_without_context(states, hist[entity_id]) hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=True ) assert_multiple_states_equal_without_context( states, list(reversed(list(hist[entity_id]))) )
Test number of state changes.
def test_get_last_state_changes(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test number of state changes.""" hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) point2 = point + timedelta(minutes=1, seconds=1) states = [] with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) states.append(set_state("2")) freezer.move_to(point2) states.append(set_state("3")) hist = history.get_last_state_changes(hass, 2, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Ensure a state can pass though copy(). The filter integration uses copy() on states from history.
def test_ensure_state_can_be_copied( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Ensure a state can pass though copy(). The filter integration uses copy() on states from history. """ hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) set_state("2") hist = history.get_last_state_changes(hass, 2, entity_id) assert_states_equal_without_context( copy(hist[entity_id][0]), hist[entity_id][0] ) assert_states_equal_without_context( copy(hist[entity_id][1]), hist[entity_id][1] )
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) hist = history.get_significant_states(hass, zero, four, entity_ids=list(states)) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_minimal_response( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, minimal_response=True, entity_ids=list(states) ) entites_with_reducable_states = [ "media_player.test", "media_player.test3", ] # All states for media_player.test state are reduced # down to last_changed and state when minimal_response # is set except for the first state. # is set. We use JSONEncoder to make sure that are # pre-encoded last_changed is always the same as what # will happen with encoding a native state for entity_id in entites_with_reducable_states: entity_states = states[entity_id] for state_idx in range(1, len(entity_states)): input_state = entity_states[state_idx] orig_last_changed = json.dumps( process_timestamp(input_state.last_changed), cls=JSONEncoder, ).replace('"', "") orig_state = input_state.state entity_states[state_idx] = { "last_changed": orig_last_changed, "state": orig_state, } assert len(hist) == len(states) assert_states_equal_without_context( states["media_player.test"][0], hist["media_player.test"][0] ) assert states["media_player.test"][1] == hist["media_player.test"][1] assert states["media_player.test"][2] == hist["media_player.test"][2] assert_multiple_states_equal_without_context( states["media_player.test2"], hist["media_player.test2"] ) assert_states_equal_without_context( states["media_player.test3"][0], hist["media_player.test3"][0] ) assert states["media_player.test3"][1] == hist["media_player.test3"][1] assert_multiple_states_equal_without_context( states["script.can_cancel_this_one"], hist["script.can_cancel_this_one"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test2"], hist["thermostat.test2"] )
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_with_initial( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: if entity_id == "media_player.test": states[entity_id] = states[entity_id][1:] for state in states[entity_id]: if state.last_changed in (one, one_with_microsecond): state.last_changed = one_and_half state.last_updated = one_and_half hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=True, entity_ids=list(states), ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_without_initial( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: states[entity_id] = [ s for s in states[entity_id] if s.last_changed not in (one, one_with_microsecond) ] del states["media_player.test2"] hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=False, entity_ids=list(states), ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_entity_id( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) del states["media_player.test2"] del states["media_player.test3"] del states["thermostat.test"] del states["thermostat.test2"] del states["script.can_cancel_this_one"] hist = history.get_significant_states(hass, zero, four, ["media_player.test"]) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_multiple_entity_ids( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) del states["media_player.test2"] del states["media_player.test3"] del states["thermostat.test2"] del states["script.can_cancel_this_one"] hist = history.get_significant_states( hass, zero, four, ["media_player.test", "thermostat.test"], ) assert_multiple_states_equal_without_context_and_last_changed( states["media_player.test"], hist["media_player.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] )
Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order.
def test_get_significant_states_are_ordered( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order. """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, _states = record_states(hass) entity_ids = ["media_player.test", "media_player.test2"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids entity_ids = ["media_player.test2", "media_player.test"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids
Test significant states when significant_states_only is set.
def test_get_significant_states_only( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test significant states when significant_states_only is set.""" hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=4) points = [start + timedelta(minutes=i) for i in range(1, 4)] states = [] with freeze_time(start) as freezer: set_state("123", attributes={"attribute": 10.64}) freezer.move_to(points[0]) # Attributes are different, state not states.append(set_state("123", attributes={"attribute": 21.42})) freezer.move_to(points[1]) # state is different, attributes not states.append(set_state("32", attributes={"attribute": 21.42})) freezer.move_to(points[2]) # everything is different states.append(set_state("412", attributes={"attribute": 54.23})) hist = history.get_significant_states( hass, start, significant_changes_only=True, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 2 assert not any( state.last_updated == states[0].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[1].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[2].last_updated for state in hist[entity_id] ) hist = history.get_significant_states( hass, start, significant_changes_only=False, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 3 assert_multiple_states_equal_without_context_and_last_changed( states, hist[entity_id] )
Record some test states. We inject a bunch of state updates from media player, zone and thermostat.
def record_states(hass) -> tuple[datetime, datetime, dict[str, list[State]]]: """Record some test states. We inject a bunch of state updates from media player, zone and thermostat. """ mp = "media_player.test" mp2 = "media_player.test2" mp3 = "media_player.test3" therm = "thermostat.test" therm2 = "thermostat.test2" zone = "zone.home" script_c = "script.can_cancel_this_one" def set_state(entity_id, state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) zero = dt_util.utcnow() one = zero + timedelta(seconds=1) two = one + timedelta(seconds=1) three = two + timedelta(seconds=1) four = three + timedelta(seconds=1) states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []} with freeze_time(one) as freezer: states[mp].append( set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[mp2].append( set_state(mp2, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) states[mp3].append( set_state(mp3, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[therm].append( set_state(therm, 20, attributes={"current_temperature": 19.5}) ) freezer.move_to(one + timedelta(microseconds=1)) states[mp].append( set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) freezer.move_to(two) # This state will be skipped only different in time set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)}) # This state will be skipped because domain is excluded set_state(zone, "zoning") states[script_c].append( set_state(script_c, "off", attributes={"can_cancel": True}) ) states[therm].append( set_state(therm, 21, attributes={"current_temperature": 19.8}) ) states[therm2].append( set_state(therm2, 20, attributes={"current_temperature": 19}) ) freezer.move_to(three) states[mp].append( set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)}) ) states[mp3].append( set_state(mp3, "Netflix", attributes={"media_title": str(sentinel.mt3)}) ) # Attributes changed even though state is the same states[therm].append( set_state(therm, 21, attributes={"current_temperature": 20}) ) return zero, four, states
Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results.
def test_state_changes_during_period_multiple_entities_single_test( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results. """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): start = dt_util.utcnow() test_entites = {f"sensor.{i}": str(i) for i in range(30)} for entity_id, value in test_entites.items(): hass.states.set(entity_id, value) wait_recording_done(hass) end = dt_util.utcnow() for entity_id, value in test_entites.items(): hist = history.state_changes_during_period(hass, start, end, entity_id) assert len(hist) == 1 assert hist[entity_id][0].state == value
Test at least one entity id is required for get_significant_states.
def test_get_significant_states_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for get_significant_states.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_ids must be provided"): history.get_significant_states(hass, now, None)
Test at least one entity id is required for state_changes_during_period.
def test_state_changes_during_period_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for state_changes_during_period.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_id must be provided"): history.state_changes_during_period(hass, now, None)
Test passing filters is no longer supported.
def test_get_significant_states_with_filters_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test passing filters is no longer supported.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(NotImplementedError, match="Filters are no longer supported"): history.get_significant_states( hass, now, None, ["media_player.test"], Filters() )
Test get_significant_states returns an empty dict when entities not in the db.
def test_get_significant_states_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_significant_states returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert history.get_significant_states(hass, now, None, ["nonexistent.entity"]) == {}
Test state_changes_during_period returns an empty dict when entities not in the db.
def test_state_changes_during_period_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state_changes_during_period returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert ( history.state_changes_during_period(hass, now, None, "nonexistent.entity") == {} )
Test get_last_state_changes returns an empty dict when entities not in the db.
def test_get_last_state_changes_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_last_state_changes returns an empty dict when entities not in the db.""" hass = hass_recorder() assert history.get_last_state_changes(hass, 1, "nonexistent.entity") == {}
Fixture to initialize the db with the old schema 32.
def db_schema_32(): """Fixture to initialize the db with the old schema 32.""" with old_db_schema("32"): yield
Test getting states at a specific point in time for entities that never have been recorded.
def test_get_full_significant_states_with_session_entity_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) instance = recorder.get_instance(hass) with ( session_scope(hass=hass) as session, patch.object(instance.states_meta_manager, "active", False), ): assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"] ) == {} ) assert ( history.get_full_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], ) == {} )
Test getting states at a specific point in time for entities that never have been recorded.
def test_significant_states_with_session_entity_minimal_response_no_matches( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test getting states at a specific point in time for entities that never have been recorded.""" hass = hass_recorder() now = dt_util.utcnow() time_before_recorder_ran = now - timedelta(days=1000) instance = recorder.get_instance(hass) with ( session_scope(hass=hass) as session, patch.object(instance.states_meta_manager, "active", False), ): assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id"], minimal_response=True, ) == {} ) assert ( history.get_significant_states_with_session( hass, session, time_before_recorder_ran, now, entity_ids=["demo.id", "demo.id2"], minimal_response=True, ) == {} )
Test state change during period.
def test_state_changes_during_period( hass_recorder: Callable[..., HomeAssistant], attributes, no_attributes, limit ) -> None: """Test state change during period.""" hass = hass_recorder() entity_id = "media_player.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state, attributes) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point = start + timedelta(seconds=1) end = point + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [ set_state("idle"), set_state("Netflix"), set_state("Plex"), set_state("YouTube"), ] freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes, limit=limit ) assert_multiple_states_equal_without_context(states[:limit], hist[entity_id])
Test state change during period descending.
def test_state_changes_during_period_descending( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period descending.""" hass = hass_recorder() entity_id = "media_player.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state, {"any": 1}) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() point = start + timedelta(seconds=1) point2 = start + timedelta(seconds=1, microseconds=2) point3 = start + timedelta(seconds=1, microseconds=3) point4 = start + timedelta(seconds=1, microseconds=4) end = point + timedelta(seconds=1) with freeze_time(start) as freezer: set_state("idle") set_state("YouTube") freezer.move_to(point) states = [set_state("idle")] freezer.move_to(point2) states.append(set_state("Netflix")) freezer.move_to(point3) states.append(set_state("Plex")) freezer.move_to(point4) states.append(set_state("YouTube")) freezer.move_to(end) set_state("Netflix") set_state("Plex") hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=False ) assert_multiple_states_equal_without_context(states, hist[entity_id]) hist = history.state_changes_during_period( hass, start, end, entity_id, no_attributes=False, descending=True ) assert_multiple_states_equal_without_context( states, list(reversed(list(hist[entity_id]))) )
Test number of state changes.
def test_get_last_state_changes(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test number of state changes.""" hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) point2 = point + timedelta(minutes=1, seconds=1) states = [] with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) states.append(set_state("2")) freezer.move_to(point2) states.append(set_state("3")) hist = history.get_last_state_changes(hass, 2, entity_id) assert_multiple_states_equal_without_context(states, hist[entity_id])
Ensure a state can pass though copy(). The filter integration uses copy() on states from history.
def test_ensure_state_can_be_copied( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Ensure a state can pass though copy(). The filter integration uses copy() on states from history. """ hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state): """Set the state.""" hass.states.set(entity_id, state) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=2) point = start + timedelta(minutes=1) with freeze_time(start) as freezer: set_state("1") freezer.move_to(point) set_state("2") hist = history.get_last_state_changes(hass, 2, entity_id) assert_states_equal_without_context( copy(hist[entity_id][0]), hist[entity_id][0] ) assert_states_equal_without_context( copy(hist[entity_id][1]), hist[entity_id][1] )
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states(hass_recorder: Callable[..., HomeAssistant]) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) hist = history.get_significant_states(hass, zero, four, entity_ids=list(states)) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_minimal_response( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. When minimal responses is set only the first and last states return a complete state. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) hist = history.get_significant_states( hass, zero, four, minimal_response=True, entity_ids=list(states) ) entites_with_reducable_states = [ "media_player.test", "media_player.test3", ] # All states for media_player.test state are reduced # down to last_changed and state when minimal_response # is set except for the first state. # is set. We use JSONEncoder to make sure that are # pre-encoded last_changed is always the same as what # will happen with encoding a native state for entity_id in entites_with_reducable_states: entity_states = states[entity_id] for state_idx in range(1, len(entity_states)): input_state = entity_states[state_idx] orig_last_changed = json.dumps( process_timestamp(input_state.last_changed), cls=JSONEncoder, ).replace('"', "") orig_state = input_state.state entity_states[state_idx] = { "last_changed": orig_last_changed, "state": orig_state, } assert len(hist) == len(states) assert_states_equal_without_context( states["media_player.test"][0], hist["media_player.test"][0] ) assert states["media_player.test"][1] == hist["media_player.test"][1] assert states["media_player.test"][2] == hist["media_player.test"][2] assert_multiple_states_equal_without_context( states["media_player.test2"], hist["media_player.test2"] ) assert_states_equal_without_context( states["media_player.test3"][0], hist["media_player.test3"][0] ) assert states["media_player.test3"][1] == hist["media_player.test3"][1] assert_multiple_states_equal_without_context( states["script.can_cancel_this_one"], hist["script.can_cancel_this_one"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test2"], hist["thermostat.test2"] )
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_with_initial( time_zone, hass_recorder: Callable[..., HomeAssistant] ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() hass.config.set_time_zone(time_zone) zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: if entity_id == "media_player.test": states[entity_id] = states[entity_id][1:] for state in states[entity_id]: if state.last_changed == one: state.last_changed = one_and_half hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=True, entity_ids=list(states) ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned).
def test_get_significant_states_without_initial( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned. We should get back every thermostat change that includes an attribute change, but only the state updates for media player (attribute changes are not significant and not returned). """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_with_microsecond = zero + timedelta(seconds=1, microseconds=1) one_and_half = zero + timedelta(seconds=1.5) for entity_id in states: states[entity_id] = [ s for s in states[entity_id] if s.last_changed not in (one, one_with_microsecond) ] del states["media_player.test2"] hist = history.get_significant_states( hass, one_and_half, four, include_start_time_state=False, entity_ids=list(states), ) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_entity_id( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) del states["media_player.test2"] del states["media_player.test3"] del states["thermostat.test"] del states["thermostat.test2"] del states["script.can_cancel_this_one"] hist = history.get_significant_states(hass, zero, four, ["media_player.test"]) assert_dict_of_states_equal_without_context_and_last_changed(states, hist)
Test that only significant states are returned for one entity.
def test_get_significant_states_multiple_entity_ids( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test that only significant states are returned for one entity.""" hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, states = record_states(hass) del states["media_player.test2"] del states["media_player.test3"] del states["thermostat.test2"] del states["script.can_cancel_this_one"] hist = history.get_significant_states( hass, zero, four, ["media_player.test", "thermostat.test"], ) assert_multiple_states_equal_without_context_and_last_changed( states["media_player.test"], hist["media_player.test"] ) assert_multiple_states_equal_without_context_and_last_changed( states["thermostat.test"], hist["thermostat.test"] )
Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order.
def test_get_significant_states_are_ordered( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test order of results from get_significant_states. When entity ids are given, the results should be returned with the data in the same order. """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): zero, four, _states = record_states(hass) entity_ids = ["media_player.test", "media_player.test2"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids entity_ids = ["media_player.test2", "media_player.test"] hist = history.get_significant_states(hass, zero, four, entity_ids) assert list(hist.keys()) == entity_ids
Test significant states when significant_states_only is set.
def test_get_significant_states_only( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test significant states when significant_states_only is set.""" hass = hass_recorder() entity_id = "sensor.test" instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): def set_state(state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) start = dt_util.utcnow() - timedelta(minutes=4) points = [start + timedelta(minutes=i) for i in range(1, 4)] states = [] with freeze_time(start) as freezer: set_state("123", attributes={"attribute": 10.64}) freezer.move_to(points[0]) # Attributes are different, state not states.append(set_state("123", attributes={"attribute": 21.42})) freezer.move_to(points[1]) # state is different, attributes not states.append(set_state("32", attributes={"attribute": 21.42})) freezer.move_to(points[2]) # everything is different states.append(set_state("412", attributes={"attribute": 54.23})) hist = history.get_significant_states( hass, start, significant_changes_only=True, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 2 assert not any( state.last_updated == states[0].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[1].last_updated for state in hist[entity_id] ) assert any( state.last_updated == states[2].last_updated for state in hist[entity_id] ) hist = history.get_significant_states( hass, start, significant_changes_only=False, entity_ids=list({state.entity_id for state in states}), ) assert len(hist[entity_id]) == 3 assert_multiple_states_equal_without_context_and_last_changed( states, hist[entity_id] )
Record some test states. We inject a bunch of state updates from media player, zone and thermostat.
def record_states(hass) -> tuple[datetime, datetime, dict[str, list[State]]]: """Record some test states. We inject a bunch of state updates from media player, zone and thermostat. """ mp = "media_player.test" mp2 = "media_player.test2" mp3 = "media_player.test3" therm = "thermostat.test" therm2 = "thermostat.test2" zone = "zone.home" script_c = "script.can_cancel_this_one" def set_state(entity_id, state, **kwargs): """Set the state.""" hass.states.set(entity_id, state, **kwargs) wait_recording_done(hass) return hass.states.get(entity_id) zero = dt_util.utcnow() one = zero + timedelta(seconds=1) two = one + timedelta(seconds=1) three = two + timedelta(seconds=1) four = three + timedelta(seconds=1) states = {therm: [], therm2: [], mp: [], mp2: [], mp3: [], script_c: []} with freeze_time(one) as freezer: states[mp].append( set_state(mp, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[mp2].append( set_state(mp2, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) states[mp3].append( set_state(mp3, "idle", attributes={"media_title": str(sentinel.mt1)}) ) states[therm].append( set_state(therm, 20, attributes={"current_temperature": 19.5}) ) freezer.move_to(one + timedelta(microseconds=1)) states[mp].append( set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt2)}) ) freezer.move_to(two) # This state will be skipped only different in time set_state(mp, "YouTube", attributes={"media_title": str(sentinel.mt3)}) # This state will be skipped because domain is excluded set_state(zone, "zoning") states[script_c].append( set_state(script_c, "off", attributes={"can_cancel": True}) ) states[therm].append( set_state(therm, 21, attributes={"current_temperature": 19.8}) ) states[therm2].append( set_state(therm2, 20, attributes={"current_temperature": 19}) ) freezer.move_to(three) states[mp].append( set_state(mp, "Netflix", attributes={"media_title": str(sentinel.mt4)}) ) states[mp3].append( set_state(mp3, "Netflix", attributes={"media_title": str(sentinel.mt3)}) ) # Attributes changed even though state is the same states[therm].append( set_state(therm, 21, attributes={"current_temperature": 20}) ) return zero, four, states
Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results.
def test_state_changes_during_period_multiple_entities_single_test( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state change during period with multiple entities in the same test. This test ensures the sqlalchemy query cache does not generate incorrect results. """ hass = hass_recorder() instance = recorder.get_instance(hass) with patch.object(instance.states_meta_manager, "active", False): start = dt_util.utcnow() test_entites = {f"sensor.{i}": str(i) for i in range(30)} for entity_id, value in test_entites.items(): hass.states.set(entity_id, value) wait_recording_done(hass) end = dt_util.utcnow() for entity_id, value in test_entites.items(): hist = history.state_changes_during_period(hass, start, end, entity_id) assert len(hist) == 1 assert hist[entity_id][0].state == value
Test at least one entity id is required for get_significant_states.
def test_get_significant_states_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for get_significant_states.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_ids must be provided"): history.get_significant_states(hass, now, None)
Test at least one entity id is required for state_changes_during_period.
def test_state_changes_during_period_without_entity_ids_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test at least one entity id is required for state_changes_during_period.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(ValueError, match="entity_id must be provided"): history.state_changes_during_period(hass, now, None)
Test passing filters is no longer supported.
def test_get_significant_states_with_filters_raises( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test passing filters is no longer supported.""" hass = hass_recorder() now = dt_util.utcnow() with pytest.raises(NotImplementedError, match="Filters are no longer supported"): history.get_significant_states( hass, now, None, ["media_player.test"], Filters() )
Test get_significant_states returns an empty dict when entities not in the db.
def test_get_significant_states_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_significant_states returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert history.get_significant_states(hass, now, None, ["nonexistent.entity"]) == {}
Test state_changes_during_period returns an empty dict when entities not in the db.
def test_state_changes_during_period_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test state_changes_during_period returns an empty dict when entities not in the db.""" hass = hass_recorder() now = dt_util.utcnow() assert ( history.state_changes_during_period(hass, now, None, "nonexistent.entity") == {} )
Test get_last_state_changes returns an empty dict when entities not in the db.
def test_get_last_state_changes_with_non_existent_entity_ids_returns_empty( hass_recorder: Callable[..., HomeAssistant], ) -> None: """Test get_last_state_changes returns an empty dict when entities not in the db.""" hass = hass_recorder() assert history.get_last_state_changes(hass, 1, "nonexistent.entity") == {}
Fixture to initialize the db with the old schema 42.
def db_schema_42(): """Fixture to initialize the db with the old schema 42.""" with old_db_schema("42"): yield