diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 26b9f471b9e..a5a49e7df60 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -129,8 +129,7 @@ def is_entity_recorded(hass: HomeAssistant, entity_id: str) -> bool: """ if DATA_INSTANCE not in hass.data: return False - instance = get_instance(hass) - return instance.entity_filter(entity_id) + return hass.data[DATA_INSTANCE].entity_filter(entity_id) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -165,6 +164,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: entity_filter=entity_filter, exclude_event_types=exclude_event_types, ) + get_instance.cache_clear() instance.async_initialize() instance.async_register() instance.start() diff --git a/homeassistant/components/recorder/const.py b/homeassistant/components/recorder/const.py index 1869bb32239..97418ee364a 100644 --- a/homeassistant/components/recorder/const.py +++ b/homeassistant/components/recorder/const.py @@ -1,6 +1,7 @@ """Recorder constants.""" from enum import StrEnum +from typing import TYPE_CHECKING from homeassistant.const import ( ATTR_ATTRIBUTION, @@ -10,8 +11,15 @@ from homeassistant.const import ( EVENT_RECORDER_HOURLY_STATISTICS_GENERATED, # noqa: F401 ) from homeassistant.helpers.json import JSON_DUMP # noqa: F401 +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from .core import Recorder # noqa: F401 + + +DATA_INSTANCE: HassKey["Recorder"] = HassKey("recorder_instance") + -DATA_INSTANCE = "recorder_instance" SQLITE_URL_PREFIX = "sqlite://" MARIADB_URL_PREFIX = "mariadb://" MARIADB_PYMYSQL_URL_PREFIX = "mariadb+pymysql://" diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 667150d5a15..939a016c960 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -739,8 +739,7 @@ def async_migration_in_progress(hass: HomeAssistant) -> bool: """ if DATA_INSTANCE not in hass.data: return False - instance = get_instance(hass) - return instance.migration_in_progress + return hass.data[DATA_INSTANCE].migration_in_progress def async_migration_is_live(hass: HomeAssistant) -> bool: @@ -751,8 +750,7 @@ def async_migration_is_live(hass: HomeAssistant) -> bool: """ if DATA_INSTANCE not in hass.data: return False - instance: Recorder = hass.data[DATA_INSTANCE] - return instance.migration_is_live + return hass.data[DATA_INSTANCE].migration_is_live def second_sunday(year: int, month: int) -> date: @@ -771,10 +769,10 @@ def is_second_sunday(date_time: datetime) -> bool: return bool(second_sunday(date_time.year, date_time.month).day == date_time.day) +@functools.lru_cache(maxsize=1) def get_instance(hass: HomeAssistant) -> Recorder: """Get the recorder instance.""" - instance: Recorder = hass.data[DATA_INSTANCE] - return instance + return hass.data[DATA_INSTANCE] PERIOD_SCHEMA = vol.Schema(