def test_ensure_state_can_be_copied(hass_recorder): """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 patch("homeassistant.components.recorder.dt_util.utcnow", return_value=start): set_state("1") with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=point): set_state("2") hist = history.get_last_state_changes(hass, 2, entity_id) assert copy(hist[entity_id][0]) == hist[entity_id][0] assert copy(hist[entity_id][1]) == hist[entity_id][1]
def test_get_last_state_changes(hass_recorder): """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) with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=start): set_state("1") states = [] with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=point): states.append(set_state("2")) with patch("homeassistant.components.recorder.dt_util.utcnow", return_value=point2): states.append(set_state("3")) hist = history.get_last_state_changes(hass, 2, entity_id) assert states == hist[entity_id]
def get_last_state_changes(hass, number_of_states, entity_id): """Return the last number_of_states.""" return history.get_last_state_changes(hass, number_of_states, entity_id)