def test_get_states(self): """Test getting states at a specific point in time.""" self.test_setup() states = [] now = dt_util.utcnow() with patch( "homeassistant.components.recorder.dt_util.utcnow", return_value=now ): for i in range(5): state = ha.State( "test.point_in_time_{}".format(i % 5), f"State {i}", {"attribute_test": i}, ) mock_state_change_event(self.hass, state) states.append(state) wait_recording_done(self.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( "test.point_in_time_{}".format(i % 5), f"State {i}", {"attribute_test": i}, ) mock_state_change_event(self.hass, state) wait_recording_done(self.hass) # Get states returns everything before POINT for state1, state2 in zip( states, sorted( history.get_states(self.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(self.hass, future, states[0].entity_id) time_before_recorder_ran = now - timedelta(days=1000) assert history.get_states(self.hass, time_before_recorder_ran) == [] assert history.get_state(self.hass, time_before_recorder_ran, "demo.id") is None
def _load_restore_cache(hass: HomeAssistant): """Load the restore cache to be used by other components.""" @callback def remove_cache(event): """Remove the states cache.""" hass.data.pop(DATA_RESTORE_CACHE, None) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, remove_cache) last_run = last_recorder_run(hass) if last_run is None or last_run.end is None: _LOGGER.debug('Not creating cache - no suitable last run found: %s', last_run) hass.data[DATA_RESTORE_CACHE] = {} return last_end_time = last_run.end - timedelta(seconds=1) # Unfortunately the recorder_run model do not return offset-aware time last_end_time = last_end_time.replace(tzinfo=dt_util.UTC) _LOGGER.debug("Last run: %s - %s", last_run.start, last_end_time) states = get_states(hass, last_end_time, run=last_run) # Cache the states hass.data[DATA_RESTORE_CACHE] = { state.entity_id: state for state in states } _LOGGER.debug('Created cache with %s', list(hass.data[DATA_RESTORE_CACHE]))
def _load_restore_cache(hass: HomeAssistant): """Load the restore cache to be used by other components.""" @callback def remove_cache(event): """Remove the states cache.""" hass.data.pop(DATA_RESTORE_CACHE, None) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, remove_cache) last_run = last_recorder_run(hass) if last_run is None or last_run.end is None: _LOGGER.debug('Not creating cache - no suitable last run found: %s', last_run) hass.data[DATA_RESTORE_CACHE] = {} return last_end_time = last_run.end - timedelta(seconds=1) # Unfortunately the recorder_run model do not return offset-aware time last_end_time = last_end_time.replace(tzinfo=dt_util.UTC) _LOGGER.debug("Last run: %s - %s", last_run.start, last_end_time) states = get_states(hass, last_end_time, run=last_run) # Cache the states hass.data[DATA_RESTORE_CACHE] = { state.entity_id: state for state in states} _LOGGER.debug('Created cache with %s', list(hass.data[DATA_RESTORE_CACHE]))
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] # Create 10 states for 5 different entities # After the first 5, sleep a second and save the time # history.get_states takes the latest states BEFORE point X for i in range(10): state = ha.State("test.point_in_time_{}".format(i % 5), "State {}".format(i), {"attribute_test": i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() if i < 5: states.append(state) if i == 4: time.sleep(1) point = dt_util.utcnow() self.assertEqual(states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] # Create 10 states for 5 different entities # After the first 5, sleep a second and save the time # history.get_states takes the latest states BEFORE point X for i in range(10): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() if i < 5: states.append(state) if i == 4: time.sleep(1) point = dt_util.utcnow() self.assertEqual( states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] for i in range(5): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() states.append(state) recorder._INSTANCE.block_till_done() point = dt_util.utcnow() + timedelta(seconds=1) with patch('homeassistant.util.dt.utcnow', return_value=point): for i in range(5): state = ha.State('test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() # Get states returns everything before POINT self.assertEqual( states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual(states[0], history.get_state(point, states[0].entity_id))
def test_get_states(self): """Test getting states at a specific point in time.""" self.init_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( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) states.append(state) self.wait_recording_done() future = now + timedelta(seconds=1) with patch('homeassistant.components.recorder.dt_util.utcnow', return_value=future): for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.wait_recording_done() # Get states returns everything before POINT for state1, state2 in zip( states, sorted(history.get_states(self.hass, future), key=lambda state: state.entity_id)): assert state1 == state2 # Test get_state here because we have a DB setup self.assertEqual( states[0], history.get_state(self.hass, future, states[0].entity_id))
def test_get_states(self): """ Test getting states at a specific point in time. """ self.init_recorder() states = [] for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() states.append(state) recorder._INSTANCE.block_till_done() point = dt_util.utcnow() + timedelta(seconds=1) with patch('homeassistant.util.dt.utcnow', return_value=point): for i in range(5): state = ha.State( 'test.point_in_time_{}'.format(i % 5), "State {}".format(i), {'attribute_test': i}) mock_state_change_event(self.hass, state) self.hass.pool.block_till_done() # Get states returns everything before POINT self.assertEqual(states, sorted(history.get_states(point), key=lambda state: state.entity_id)) # Test get_state here because we have a DB setup self.assertEqual( states[0], history.get_state(point, states[0].entity_id))