From 92ca95ca81f4d62510894cbb209b53eea7ee75f1 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 4 Oct 2022 03:13:48 +0200 Subject: [PATCH] Fix preserving long term statistics when entity_id is changed (#79556) --- homeassistant/components/recorder/statistics.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index ad948b560bb..7ba5c5f8c73 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -29,6 +29,7 @@ from homeassistant.core import Event, HomeAssistant, callback, valid_entity_id from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry from homeassistant.helpers.json import JSONEncoder +from homeassistant.helpers.start import async_at_start from homeassistant.helpers.storage import STORAGE_DIR from homeassistant.helpers.typing import UNDEFINED, UndefinedType from homeassistant.util import dt as dt_util @@ -299,13 +300,17 @@ def async_setup(hass: HomeAssistant) -> None: return True - if hass.is_running: + @callback + def setup_entity_registry_event_handler(hass: HomeAssistant) -> None: + """Subscribe to event registry events.""" hass.bus.async_listen( entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, _async_entity_id_changed, event_filter=entity_registry_changed_filter, ) + async_at_start(hass, setup_entity_registry_event_handler) + def get_start_time() -> datetime: """Return start time."""