Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Verisure devices."""
2 
3 from __future__ import annotations
4 
5 from contextlib import suppress
6 import os
7 from pathlib import Path
8 
9 from homeassistant.components.lock import CONF_DEFAULT_CODE, DOMAIN as LOCK_DOMAIN
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_EMAIL, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 from homeassistant.helpers import entity_registry as er
15 from homeassistant.helpers.storage import STORAGE_DIR
16 
17 from .const import CONF_LOCK_DEFAULT_CODE, DOMAIN, LOGGER
18 from .coordinator import VerisureDataUpdateCoordinator
19 
20 PLATFORMS = [
21  Platform.ALARM_CONTROL_PANEL,
22  Platform.BINARY_SENSOR,
23  Platform.CAMERA,
24  Platform.LOCK,
25  Platform.SENSOR,
26  Platform.SWITCH,
27 ]
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Set up Verisure from a config entry."""
32  await hass.async_add_executor_job(migrate_cookie_files, hass, entry)
33 
34  coordinator = VerisureDataUpdateCoordinator(hass, entry=entry)
35 
36  if not await coordinator.async_login():
37  raise ConfigEntryNotReady("Could not log in to verisure.")
38 
39  await coordinator.async_config_entry_first_refresh()
40 
41  hass.data.setdefault(DOMAIN, {})
42  hass.data[DOMAIN][entry.entry_id] = coordinator
43 
44  # Migrate lock default code from config entry to lock entity
45 
46  # Set up all platforms for this device/entry.
47  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
48 
49  # Update options
50  entry.async_on_unload(entry.add_update_listener(update_listener))
51 
52  return True
53 
54 
55 async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
56  """Handle options update."""
57  # Propagate configuration change.
58  coordinator = hass.data[DOMAIN][entry.entry_id]
59  coordinator.async_update_listeners()
60 
61 
62 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
63  """Unload Verisure config entry."""
64  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
65  if not unload_ok:
66  return False
67 
68  cookie_file = hass.config.path(STORAGE_DIR, f"verisure_{entry.entry_id}")
69  with suppress(FileNotFoundError):
70  await hass.async_add_executor_job(os.unlink, cookie_file)
71 
72  del hass.data[DOMAIN][entry.entry_id]
73 
74  if not hass.data[DOMAIN]:
75  del hass.data[DOMAIN]
76 
77  return True
78 
79 
80 def migrate_cookie_files(hass: HomeAssistant, entry: ConfigEntry) -> None:
81  """Migrate old cookie file to new location."""
82  cookie_file = Path(hass.config.path(STORAGE_DIR, f"verisure_{entry.unique_id}"))
83  if cookie_file.exists():
84  cookie_file.rename(
85  hass.config.path(STORAGE_DIR, f"verisure_{entry.data[CONF_EMAIL]}")
86  )
87 
88 
89 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
90  """Migrate old entry."""
91  LOGGER.debug("Migrating from version %s", entry.version)
92 
93  if entry.version == 1:
94  if config_entry_default_code := entry.options.get(CONF_LOCK_DEFAULT_CODE):
95  entity_reg = er.async_get(hass)
96  entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id)
97  for entity in entries:
98  if entity.entity_id.startswith("lock"):
99  entity_reg.async_update_entity_options(
100  entity.entity_id,
101  LOCK_DOMAIN,
102  {CONF_DEFAULT_CODE: config_entry_default_code},
103  )
104  new_options = entry.options.copy()
105  del new_options[CONF_LOCK_DEFAULT_CODE]
106 
107  hass.config_entries.async_update_entry(entry, options=new_options)
108 
109  hass.config_entries.async_update_entry(entry, version=2)
110 
111  LOGGER.debug("Migration to version %s successful", entry.version)
112 
113  return True
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:89
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30
def update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:55
None migrate_cookie_files(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:80
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:62