Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Ridwell integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import entity_registry as er
11 
12 from .const import DOMAIN, LOGGER, SENSOR_TYPE_NEXT_PICKUP
13 from .coordinator import RidwellDataUpdateCoordinator
14 
15 PLATFORMS: list[Platform] = [Platform.CALENDAR, Platform.SENSOR, Platform.SWITCH]
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
19  """Set up Ridwell from a config entry."""
20  coordinator = RidwellDataUpdateCoordinator(hass, name=entry.title)
21  await coordinator.async_initialize()
22  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
23 
24  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
25 
26  return True
27 
28 
29 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
30  """Unload a config entry."""
31  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
32  hass.data[DOMAIN].pop(entry.entry_id)
33 
34  return unload_ok
35 
36 
37 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
38  """Migrate an old config entry."""
39  version = entry.version
40 
41  LOGGER.debug("Migrating from version %s", version)
42 
43  # 1 -> 2: Update unique ID of existing, single sensor entity to be consistent with
44  # common format for platforms going forward:
45  if version == 1:
46  version = 2
47  hass.config_entries.async_update_entry(entry, version=version)
48 
49  @callback
50  def migrate_unique_id(entity_entry: er.RegistryEntry) -> dict[str, Any]:
51  """Migrate the unique ID to a new format."""
52  return {
53  "new_unique_id": f"{entity_entry.unique_id}_{SENSOR_TYPE_NEXT_PICKUP}"
54  }
55 
56  await er.async_migrate_entries(hass, entry.entry_id, migrate_unique_id)
57 
58  LOGGER.debug("Migration to version %s successful", version)
59 
60  return True
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:29
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:18
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:37