Store runtime data inside the config entry in Local ToDo (#116818)

store runtime data inside the config entry
This commit is contained in:
Michael 2024-05-05 01:10:34 +02:00 committed by GitHub
parent 0380116ef6
commit ddf9d6c53a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 14 deletions

View File

@ -10,19 +10,18 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.util import slugify
from .const import CONF_STORAGE_KEY, CONF_TODO_LIST_NAME, DOMAIN
from .const import CONF_STORAGE_KEY, CONF_TODO_LIST_NAME
from .store import LocalTodoListStore
PLATFORMS: list[Platform] = [Platform.TODO]
STORAGE_PATH = ".storage/local_todo.{key}.ics"
LocalTodoConfigEntry = ConfigEntry[LocalTodoListStore]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: LocalTodoConfigEntry) -> bool:
"""Set up Local To-do from a config entry."""
hass.data.setdefault(DOMAIN, {})
path = Path(hass.config.path(STORAGE_PATH.format(key=entry.data[CONF_STORAGE_KEY])))
store = LocalTodoListStore(hass, path)
try:
@ -30,7 +29,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except OSError as 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)
@ -39,10 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:

View File

@ -14,14 +14,14 @@ from homeassistant.components.todo import (
TodoListEntity,
TodoListEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.setup import SetupPhases, async_pause_setup
from homeassistant.util import dt as dt_util
from .const import CONF_TODO_LIST_NAME, DOMAIN
from . import LocalTodoConfigEntry
from .const import CONF_TODO_LIST_NAME
from .store import LocalTodoListStore
_LOGGER = logging.getLogger(__name__)
@ -63,12 +63,12 @@ def _migrate_calendar(calendar: Calendar) -> bool:
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: LocalTodoConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the local_todo todo platform."""
store: LocalTodoListStore = hass.data[DOMAIN][config_entry.entry_id]
store = config_entry.runtime_data
ics = await store.async_load()
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):