Esempio n. 1
0
def test_get_states(hass_recorder):
    """Test getting states at a specific point in time."""
    hass = hass_recorder()
    now, future, states = _setup_get_states(hass)
    # Get states returns everything before POINT for all entities
    for state1, state2 in zip(
            states,
            sorted(history.get_states(hass, future),
                   key=lambda state: state.entity_id),
    ):
        assert state1 == state2

    # Get states returns everything before POINT for tested entities
    entities = [f"test.point_in_time_{i % 5}" for i in range(5)]
    for state1, state2 in zip(
            states,
            sorted(
                history.get_states(hass, future, entities),
                key=lambda state: state.entity_id,
            ),
    ):
        assert state1 == state2

    # Test get_state here because we have a DB setup
    assert states[0] == history.get_state(hass, future, states[0].entity_id)

    time_before_recorder_ran = now - timedelta(days=1000)
    assert history.get_states(hass, time_before_recorder_ran) == []

    assert history.get_state(hass, time_before_recorder_ran, "demo.id") is None
Esempio n. 2
0
async def test_get_states_query_during_migration_to_schema_25_multiple_entities(
    hass: ha.HomeAssistant,
    async_setup_recorder_instance: SetupRecorderInstanceT,
):
    """Test we can query data prior to schema 25 and during migration to schema 25."""
    instance = await async_setup_recorder_instance(hass, {})

    start = dt_util.utcnow()
    point = start + timedelta(seconds=1)
    end = point + timedelta(seconds=1)
    entity_id_1 = "light.test"
    entity_id_2 = "switch.test"
    entity_ids = [entity_id_1, entity_id_2]

    await hass.async_add_executor_job(_add_db_entries, hass, point, entity_ids)

    no_attributes = True
    hist = history.get_states(hass,
                              end,
                              entity_ids,
                              no_attributes=no_attributes)
    assert hist[0].attributes == {}
    assert hist[1].attributes == {}

    no_attributes = False
    hist = history.get_states(hass,
                              end,
                              entity_ids,
                              no_attributes=no_attributes)
    assert hist[0].attributes == {"name": "the shared light"}
    assert hist[1].attributes == {"name": "the shared light"}

    with instance.engine.connect() as conn:
        conn.execute(text("update states set attributes_id=NULL;"))
        conn.execute(text("drop table state_attributes;"))
        conn.commit()

    with patch.object(instance, "migration_in_progress", True):
        no_attributes = True
        hist = history.get_states(hass,
                                  end,
                                  entity_ids,
                                  no_attributes=no_attributes)
        assert hist[0].attributes == {}
        assert hist[1].attributes == {}

        no_attributes = False
        hist = history.get_states(hass,
                                  end,
                                  entity_ids,
                                  no_attributes=no_attributes)
        assert hist[0].attributes == {"name": "the light"}
        assert hist[1].attributes == {"name": "the light"}
Esempio n. 3
0
def test_get_states(hass_recorder):
    """Test getting states at a specific point in time."""
    hass = hass_recorder()
    states = []

    now = dt_util.utcnow()
    with patch("homeassistant.components.recorder.dt_util.utcnow",
               return_value=now):
        for i in range(5):
            state = ha.State(
                f"test.point_in_time_{i % 5}",
                f"State {i}",
                {"attribute_test": i},
            )

            mock_state_change_event(hass, state)

            states.append(state)

        wait_recording_done(hass)

    future = now + timedelta(seconds=1)
    with patch("homeassistant.components.recorder.dt_util.utcnow",
               return_value=future):
        for i in range(5):
            state = ha.State(
                f"test.point_in_time_{i % 5}",
                f"State {i}",
                {"attribute_test": i},
            )

            mock_state_change_event(hass, state)

        wait_recording_done(hass)

    # Get states returns everything before POINT
    for state1, state2 in zip(
            states,
            sorted(history.get_states(hass, future),
                   key=lambda state: state.entity_id),
    ):
        assert state1 == state2

    # Test get_state here because we have a DB setup
    assert states[0] == history.get_state(hass, future, states[0].entity_id)

    time_before_recorder_ran = now - timedelta(days=1000)
    assert history.get_states(hass, time_before_recorder_ran) == []

    assert history.get_state(hass, time_before_recorder_ran, "demo.id") is None
Esempio n. 4
0
def get_states(hass,
               utc_point_in_time,
               entity_ids=None,
               run=None,
               filters=None):
    """Return the states at a specific point in time."""
    return history.get_states(hass,
                              utc_point_in_time,
                              entity_ids=None,
                              run=None,
                              filters=None)
async def test_get_states_query_during_migration_to_schema_25(
    hass: ha.HomeAssistant,
    async_setup_recorder_instance: SetupRecorderInstanceT,
):
    """Test we can query data prior to schema 25 and during migration to schema 25."""
    instance = await async_setup_recorder_instance(hass, {})

    start = dt_util.utcnow()
    point = start + timedelta(seconds=1)
    end = point + timedelta(seconds=1)
    entity_id = "light.test"
    await hass.async_add_executor_job(_add_db_entries, hass, point, [entity_id])

    no_attributes = True
    hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes)
    state = hist[0]
    assert state.attributes == {}

    no_attributes = False
    hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes)
    state = hist[0]
    assert state.attributes == {"name": "the shared light"}

    instance.engine.execute("update states set attributes_id=NULL;")
    instance.engine.execute("drop table state_attributes;")

    with patch.object(instance, "migration_in_progress", True):
        no_attributes = True
        hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes)
        state = hist[0]
        assert state.attributes == {}

        no_attributes = False
        hist = history.get_states(hass, end, [entity_id], no_attributes=no_attributes)
        state = hist[0]
        assert state.attributes == {"name": "the light"}