mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Migrate recorder instance to use HassKey (#118673)
This commit is contained in:
parent
5d594a509c
commit
099ad77078
@ -129,8 +129,7 @@ def is_entity_recorded(hass: HomeAssistant, entity_id: str) -> bool:
|
|||||||
"""
|
"""
|
||||||
if DATA_INSTANCE not in hass.data:
|
if DATA_INSTANCE not in hass.data:
|
||||||
return False
|
return False
|
||||||
instance = get_instance(hass)
|
return hass.data[DATA_INSTANCE].entity_filter(entity_id)
|
||||||
return instance.entity_filter(entity_id)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
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,
|
entity_filter=entity_filter,
|
||||||
exclude_event_types=exclude_event_types,
|
exclude_event_types=exclude_event_types,
|
||||||
)
|
)
|
||||||
|
get_instance.cache_clear()
|
||||||
instance.async_initialize()
|
instance.async_initialize()
|
||||||
instance.async_register()
|
instance.async_register()
|
||||||
instance.start()
|
instance.start()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Recorder constants."""
|
"""Recorder constants."""
|
||||||
|
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
@ -10,8 +11,15 @@ from homeassistant.const import (
|
|||||||
EVENT_RECORDER_HOURLY_STATISTICS_GENERATED, # noqa: F401
|
EVENT_RECORDER_HOURLY_STATISTICS_GENERATED, # noqa: F401
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.json import JSON_DUMP # 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://"
|
SQLITE_URL_PREFIX = "sqlite://"
|
||||||
MARIADB_URL_PREFIX = "mariadb://"
|
MARIADB_URL_PREFIX = "mariadb://"
|
||||||
MARIADB_PYMYSQL_URL_PREFIX = "mariadb+pymysql://"
|
MARIADB_PYMYSQL_URL_PREFIX = "mariadb+pymysql://"
|
||||||
|
@ -739,8 +739,7 @@ def async_migration_in_progress(hass: HomeAssistant) -> bool:
|
|||||||
"""
|
"""
|
||||||
if DATA_INSTANCE not in hass.data:
|
if DATA_INSTANCE not in hass.data:
|
||||||
return False
|
return False
|
||||||
instance = get_instance(hass)
|
return hass.data[DATA_INSTANCE].migration_in_progress
|
||||||
return instance.migration_in_progress
|
|
||||||
|
|
||||||
|
|
||||||
def async_migration_is_live(hass: HomeAssistant) -> bool:
|
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:
|
if DATA_INSTANCE not in hass.data:
|
||||||
return False
|
return False
|
||||||
instance: Recorder = hass.data[DATA_INSTANCE]
|
return hass.data[DATA_INSTANCE].migration_is_live
|
||||||
return instance.migration_is_live
|
|
||||||
|
|
||||||
|
|
||||||
def second_sunday(year: int, month: int) -> date:
|
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)
|
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:
|
def get_instance(hass: HomeAssistant) -> Recorder:
|
||||||
"""Get the recorder instance."""
|
"""Get the recorder instance."""
|
||||||
instance: Recorder = hass.data[DATA_INSTANCE]
|
return hass.data[DATA_INSTANCE]
|
||||||
return instance
|
|
||||||
|
|
||||||
|
|
||||||
PERIOD_SCHEMA = vol.Schema(
|
PERIOD_SCHEMA = vol.Schema(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user