Migrate recorder instance to use HassKey (#118673)

This commit is contained in:
J. Nick Koston 2024-06-03 11:19:00 -05:00 committed by GitHub
parent 5d594a509c
commit 099ad77078
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 9 deletions

View File

@ -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()

View File

@ -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://"

View File

@ -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(