Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The yale_smart_alarm component."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.lock import CONF_DEFAULT_CODE, DOMAIN as LOCK_DOMAIN
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import CONF_CODE
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers import entity_registry as er
10 
11 from .const import LOGGER, PLATFORMS
12 from .coordinator import YaleDataUpdateCoordinator
13 
14 type YaleConfigEntry = ConfigEntry[YaleDataUpdateCoordinator]
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bool:
18  """Set up Yale from a config entry."""
19 
20  coordinator = YaleDataUpdateCoordinator(hass, entry)
21  await coordinator.async_config_entry_first_refresh()
22  entry.runtime_data = coordinator
23 
24  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
25  entry.async_on_unload(entry.add_update_listener(update_listener))
26 
27  return True
28 
29 
30 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
31  """Handle options update."""
32  await hass.config_entries.async_reload(entry.entry_id)
33 
34 
35 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
36  """Unload a config entry."""
37  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
38 
39 
40 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
41  """Migrate old entry."""
42  LOGGER.debug("Migrating from version %s", entry.version)
43 
44  if entry.version == 1:
45  if config_entry_default_code := entry.options.get(CONF_CODE):
46  entity_reg = er.async_get(hass)
47  entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id)
48  for entity in entries:
49  if entity.entity_id.startswith("lock"):
50  entity_reg.async_update_entity_options(
51  entity.entity_id,
52  LOCK_DOMAIN,
53  {CONF_DEFAULT_CODE: config_entry_default_code},
54  )
55  new_options = entry.options.copy()
56  del new_options[CONF_CODE]
57 
58  hass.config_entries.async_update_entry(entry, options=new_options)
59 
60  hass.config_entries.async_update_entry(entry, version=2)
61 
62  LOGGER.debug("Migration to version %s successful", entry.version)
63 
64  return True
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:40
bool async_setup_entry(HomeAssistant hass, YaleConfigEntry entry)
Definition: __init__.py:17
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:35