diff --git a/homeassistant/components/logbook/processor.py b/homeassistant/components/logbook/processor.py index 8d577089ea4..05566e52509 100644 --- a/homeassistant/components/logbook/processor.py +++ b/homeassistant/components/logbook/processor.py @@ -97,7 +97,7 @@ class LogbookRun: event_cache: EventCache entity_name_cache: EntityNameCache include_entity_name: bool - format_time: Callable[[Row | EventAsRow], Any] + timestamp: bool memoize_new_contexts: bool = True @@ -126,16 +126,13 @@ class EventProcessor: self.context_id = context_id logbook_config: LogbookConfig = hass.data[DOMAIN] self.filters: Filters | None = logbook_config.sqlalchemy_filter - format_time = ( - _row_time_fired_timestamp if timestamp else _row_time_fired_isoformat - ) self.logbook_run = LogbookRun( context_lookup={None: None}, external_events=logbook_config.external_events, event_cache=EventCache({}), entity_name_cache=EntityNameCache(self.hass), include_entity_name=include_entity_name, - format_time=format_time, + timestamp=timestamp, ) self.context_augmenter = ContextAugmenter(self.logbook_run) @@ -217,7 +214,7 @@ def _humanify( event_cache = logbook_run.event_cache entity_name_cache = logbook_run.entity_name_cache include_entity_name = logbook_run.include_entity_name - format_time = logbook_run.format_time + timestamp = logbook_run.timestamp memoize_new_contexts = logbook_run.memoize_new_contexts # Process rows @@ -231,6 +228,15 @@ def _humanify( if event_type == EVENT_CALL_SERVICE: continue + + time_fired_ts = row[TIME_FIRED_TS_POS] + if timestamp: + when = time_fired_ts or time.time() + else: + when = process_timestamp_to_utc_isoformat( + dt_util.utc_from_timestamp(time_fired_ts) or dt_util.utcnow() + ) + if event_type is PSEUDO_EVENT_STATE_CHANGED: entity_id = row[ENTITY_ID_POS] assert entity_id is not None @@ -244,7 +250,7 @@ def _humanify( continue data = { - LOGBOOK_ENTRY_WHEN: format_time(row), + LOGBOOK_ENTRY_WHEN: when, LOGBOOK_ENTRY_STATE: row[STATE_POS], LOGBOOK_ENTRY_ENTITY_ID: entity_id, } @@ -265,7 +271,7 @@ def _humanify( "Error with %s describe event for %s", domain, event_type ) continue - data[LOGBOOK_ENTRY_WHEN] = format_time(row) + data[LOGBOOK_ENTRY_WHEN] = when data[LOGBOOK_ENTRY_DOMAIN] = domain context_augmenter.augment(data, row, context_id_bin) yield data @@ -279,7 +285,7 @@ def _humanify( if entry_domain is None and entry_entity_id is not None: entry_domain = split_entity_id(str(entry_entity_id))[0] data = { - LOGBOOK_ENTRY_WHEN: format_time(row), + LOGBOOK_ENTRY_WHEN: when, LOGBOOK_ENTRY_NAME: event_data.get(ATTR_NAME), LOGBOOK_ENTRY_MESSAGE: event_data.get(ATTR_MESSAGE), LOGBOOK_ENTRY_DOMAIN: entry_domain, @@ -399,18 +405,6 @@ def _rows_match(row: Row | EventAsRow, other_row: Row | EventAsRow) -> bool: ) -def _row_time_fired_isoformat(row: Row | EventAsRow) -> str: - """Convert the row timed_fired to isoformat.""" - return process_timestamp_to_utc_isoformat( - dt_util.utc_from_timestamp(row[TIME_FIRED_TS_POS]) or dt_util.utcnow() - ) - - -def _row_time_fired_timestamp(row: Row | EventAsRow) -> float: - """Convert the row timed_fired to timestamp.""" - return row[TIME_FIRED_TS_POS] or time.time() - - class EntityNameCache: """A cache to lookup the name for an entity. diff --git a/tests/components/logbook/common.py b/tests/components/logbook/common.py index c55b6230418..afa8b7fcde5 100644 --- a/tests/components/logbook/common.py +++ b/tests/components/logbook/common.py @@ -79,7 +79,7 @@ def mock_humanify(hass_, rows): event_cache, entity_name_cache, include_entity_name=True, - format_time=processor._row_time_fired_isoformat, + timestamp=False, ) context_augmenter = processor.ContextAugmenter(logbook_run) return list(