def test_purge_old_events(self): """Test deleting old events.""" self._add_test_events() events = recorder.query_events('SELECT * FROM events WHERE ' 'event_type LIKE "EVENT_TEST%"') self.assertEqual(len(events), 5) # run purge_old_data() recorder._INSTANCE.purge_days = 4 recorder._INSTANCE._purge_old_data() # now we should only have 3 events left events = recorder.query_events('SELECT * FROM events WHERE ' 'event_type LIKE "EVENT_TEST%"') self.assertEqual(len(events), 3)
def test_saving_event(self): """Test saving and restoring an event.""" event_type = 'EVENT_TEST' event_data = {'test_attr': 5, 'test_attr_10': 'nice'} events = [] def event_listener(event): """Record events from eventbus.""" if event.event_type == event_type: events.append(event) self.hass.bus.listen(MATCH_ALL, event_listener) self.hass.bus.fire(event_type, event_data) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() db_events = recorder.query_events( 'SELECT * FROM events WHERE event_type = ?', (event_type, )) assert len(events) == 1 assert len(db_events) == 1 event = events[0] db_event = db_events[0] assert event.event_type == db_event.event_type assert event.data == db_event.data assert event.origin == db_event.origin # Recorder uses SQLite and stores datetimes as integer unix timestamps assert event.time_fired.replace(microsecond=0) == \ db_event.time_fired.replace(microsecond=0)
def _handle_get_logbook(handler, path_match, data): """ Return logbook entries. """ start_today = dt_util.now().replace(hour=0, minute=0, second=0) handler.write_json(humanify( recorder.query_events( QUERY_EVENTS_AFTER, (dt_util.as_utc(start_today),))))
def test_purge_disabled(self): """Test leaving purge_days disabled.""" self._add_test_states() self._add_test_events() # make sure we start with 5 states and events states = recorder.query_states('SELECT * FROM states') events = recorder.query_events('SELECT * FROM events WHERE ' 'event_type LIKE "EVENT_TEST%"') self.assertEqual(len(states), 5) self.assertEqual(len(events), 5) # run purge_old_data() recorder._INSTANCE.purge_days = None recorder._INSTANCE._purge_old_data() # we should have all of our states still states = recorder.query_states('SELECT * FROM states') events = recorder.query_events('SELECT * FROM events WHERE ' 'event_type LIKE "EVENT_TEST%"') self.assertEqual(len(states), 5) self.assertEqual(len(events), 5)
def get(self, request, date=None): """Retrieve logbook entries.""" if date: start_day = dt_util.start_of_local_day(date) else: start_day = dt_util.start_of_local_day() end_day = start_day + timedelta(days=1) events = recorder.query_events( QUERY_EVENTS_BETWEEN, (dt_util.as_utc(start_day), dt_util.as_utc(end_day))) return self.json(humanify(events))
def _handle_get_logbook(handler, path_match, data): """ Return logbook entries. """ date_str = path_match.group("date") if date_str: start_date = dt_util.date_str_to_date(date_str) if start_date is None: handler.write_json_message("Error parsing JSON", HTTP_BAD_REQUEST) return start_day = dt_util.start_of_local_day(start_date) else: start_day = dt_util.start_of_local_day() end_day = start_day + timedelta(days=1) events = recorder.query_events(QUERY_EVENTS_BETWEEN, (dt_util.as_utc(start_day), dt_util.as_utc(end_day))) handler.write_json(humanify(events))
def _handle_get_logbook(handler, path_match, data): """ Return logbook entries. """ date_str = path_match.group('date') if date_str: start_date = dt_util.date_str_to_date(date_str) if start_date is None: handler.write_json_message("Error parsing JSON", HTTP_BAD_REQUEST) return start_day = dt_util.start_of_local_day(start_date) else: start_day = dt_util.start_of_local_day() end_day = start_day + timedelta(days=1) events = recorder.query_events( QUERY_EVENTS_BETWEEN, (dt_util.as_utc(start_day), dt_util.as_utc(end_day))) handler.write_json(humanify(events))
def test_saving_event(self): """ Tests saving and restoring an event. """ event_type = 'EVENT_TEST' event_data = {'test_attr': 5, 'test_attr_10': 'nice'} events = [] def event_listener(event): """ Records events from eventbus. """ if event.event_type == event_type: events.append(event) self.hass.bus.listen(MATCH_ALL, event_listener) self.hass.bus.fire(event_type, event_data) self.hass.pool.block_till_done() recorder._INSTANCE.block_till_done() db_events = recorder.query_events( 'SELECT * FROM events WHERE event_type = ?', (event_type, )) self.assertEqual(events, db_events)
def _handle_get_logbook(handler, path_match, data): """ Return logbook entries. """ start_today = datetime.now().date() handler.write_json( humanify(recorder.query_events(QUERY_EVENTS_AFTER, (start_today, ))))