Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Local To-do integration."""
2 
3 from __future__ import annotations
4 
5 from pathlib import Path
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryNotReady
11 from homeassistant.util import slugify
12 
13 from .const import CONF_STORAGE_KEY, CONF_TODO_LIST_NAME
14 from .store import LocalTodoListStore
15 
16 PLATFORMS: list[Platform] = [Platform.TODO]
17 
18 STORAGE_PATH = ".storage/local_todo.{key}.ics"
19 
20 type LocalTodoConfigEntry = ConfigEntry[LocalTodoListStore]
21 
22 
23 async def async_setup_entry(hass: HomeAssistant, entry: LocalTodoConfigEntry) -> bool:
24  """Set up Local To-do from a config entry."""
25  path = Path(hass.config.path(STORAGE_PATH.format(key=entry.data[CONF_STORAGE_KEY])))
26  store = LocalTodoListStore(hass, path)
27  try:
28  await store.async_load()
29  except OSError as err:
30  raise ConfigEntryNotReady("Failed to load file {path}: {err}") from err
31 
32  entry.runtime_data = store
33 
34  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
35 
36  return True
37 
38 
39 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
40  """Unload a config entry."""
41  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
42 
43 
44 async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
45  """Handle removal of an entry."""
46  key = slugify(entry.data[CONF_TODO_LIST_NAME])
47  path = Path(hass.config.path(STORAGE_PATH.format(key=key)))
48 
49  def unlink(path: Path) -> None:
50  path.unlink(missing_ok=True)
51 
52  await hass.async_add_executor_job(unlink, path)
bool async_setup_entry(HomeAssistant hass, LocalTodoConfigEntry entry)
Definition: __init__.py:23
None async_remove_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:39
str slugify(str|None text, *str separator="_")
Definition: __init__.py:41