mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Migrate local_calendar to use runtime_data (#147481)
This commit is contained in:
parent
909d950b50
commit
69bf79d3bd
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -11,19 +10,18 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .const import CONF_CALENDAR_NAME, CONF_STORAGE_KEY, DOMAIN, STORAGE_PATH
|
from .const import CONF_CALENDAR_NAME, CONF_STORAGE_KEY, STORAGE_PATH
|
||||||
from .store import LocalCalendarStore
|
from .store import LocalCalendarStore
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.CALENDAR]
|
PLATFORMS: list[Platform] = [Platform.CALENDAR]
|
||||||
|
|
||||||
|
type LocalCalendarConfigEntry = ConfigEntry[LocalCalendarStore]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: LocalCalendarConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Set up Local Calendar from a config entry."""
|
"""Set up Local Calendar from a config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
|
|
||||||
if CONF_STORAGE_KEY not in entry.data:
|
if CONF_STORAGE_KEY not in entry.data:
|
||||||
hass.config_entries.async_update_entry(
|
hass.config_entries.async_update_entry(
|
||||||
entry,
|
entry,
|
||||||
@ -40,22 +38,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except OSError as err:
|
except OSError as err:
|
||||||
raise ConfigEntryNotReady("Failed to load file {path}: {err}") from err
|
raise ConfigEntryNotReady("Failed to load file {path}: {err}") from err
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = store
|
entry.runtime_data = store
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(
|
||||||
|
hass: HomeAssistant, entry: LocalCalendarConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_remove_entry(
|
||||||
|
hass: HomeAssistant, entry: LocalCalendarConfigEntry
|
||||||
|
) -> None:
|
||||||
"""Handle removal of an entry."""
|
"""Handle removal of an entry."""
|
||||||
key = slugify(entry.data[CONF_CALENDAR_NAME])
|
key = slugify(entry.data[CONF_CALENDAR_NAME])
|
||||||
path = Path(hass.config.path(STORAGE_PATH.format(key=key)))
|
path = Path(hass.config.path(STORAGE_PATH.format(key=key)))
|
||||||
|
@ -23,13 +23,13 @@ from homeassistant.components.calendar import (
|
|||||||
CalendarEntityFeature,
|
CalendarEntityFeature,
|
||||||
CalendarEvent,
|
CalendarEvent,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import CONF_CALENDAR_NAME, DOMAIN
|
from . import LocalCalendarConfigEntry
|
||||||
|
from .const import CONF_CALENDAR_NAME
|
||||||
from .store import LocalCalendarStore
|
from .store import LocalCalendarStore
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -39,11 +39,11 @@ PRODID = "-//homeassistant.io//local_calendar 1.0//EN"
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: LocalCalendarConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the local calendar platform."""
|
"""Set up the local calendar platform."""
|
||||||
store = hass.data[DOMAIN][config_entry.entry_id]
|
store = config_entry.runtime_data
|
||||||
ics = await store.async_load()
|
ics = await store.async_load()
|
||||||
calendar: Calendar = await hass.async_add_executor_job(
|
calendar: Calendar = await hass.async_add_executor_job(
|
||||||
IcsCalendarStream.calendar_from_ics, ics
|
IcsCalendarStream.calendar_from_ics, ics
|
||||||
|
@ -5,15 +5,14 @@ from typing import Any
|
|||||||
|
|
||||||
from ical.diagnostics import redact_ics
|
from ical.diagnostics import redact_ics
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from . import LocalCalendarConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: LocalCalendarConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
payload: dict[str, Any] = {
|
payload: dict[str, Any] = {
|
||||||
@ -21,7 +20,7 @@ async def async_get_config_entry_diagnostics(
|
|||||||
"timezone": str(dt_util.get_default_time_zone()),
|
"timezone": str(dt_util.get_default_time_zone()),
|
||||||
"system_timezone": str(datetime.datetime.now().astimezone().tzinfo),
|
"system_timezone": str(datetime.datetime.now().astimezone().tzinfo),
|
||||||
}
|
}
|
||||||
store = hass.data[DOMAIN][config_entry.entry_id]
|
store = config_entry.runtime_data
|
||||||
ics = await store.async_load()
|
ics = await store.async_load()
|
||||||
payload["ics"] = "\n".join(redact_ics(ics))
|
payload["ics"] = "\n".join(redact_ics(ics))
|
||||||
return payload
|
return payload
|
||||||
|
Loading…
x
Reference in New Issue
Block a user