diff --git a/homeassistant/components/history/__init__.py b/homeassistant/components/history/__init__.py index 4933b00ffdeaf20d7fe6a930c651450c81c08efe..43ae21fea509b5118524d894778c8a0d923e8b36 100644 --- a/homeassistant/components/history/__init__.py +++ b/homeassistant/components/history/__init__.py @@ -12,7 +12,11 @@ import voluptuous as vol from homeassistant.components import recorder from homeassistant.components.http import HomeAssistantView -from homeassistant.components.recorder.models import DB_TIMEZONE, States +from homeassistant.components.recorder.models import ( + DB_TIMEZONE, + States, + process_timestamp, +) from homeassistant.components.recorder.util import execute, session_scope from homeassistant.const import ( ATTR_HIDDEN, @@ -304,6 +308,10 @@ def _sorted_states_to_json( elapsed = time.perf_counter() - timer_start _LOGGER.debug("getting %d first datapoints took %fs", len(result), elapsed) + # Called in a tight loop so cache the function + # here + _process_timestamp = process_timestamp + # Append all changes to it for ent_id, group in groupby(states, lambda state: state.entity_id): domain = split_entity_id(ent_id)[0] @@ -347,7 +355,7 @@ def _sorted_states_to_json( ent_results.append( { STATE_KEY: db_state.state, - LAST_CHANGED_KEY: f"{str(db_state.last_changed).replace(' ','T').split('.')[0]}{DB_TIMEZONE}", + LAST_CHANGED_KEY: f"{str(_process_timestamp(db_state.last_changed)).replace(' ','T').split('.')[0]}{DB_TIMEZONE}", } ) prev_state = db_state