1 """Support for Verisure devices."""
3 from __future__
import annotations
5 from contextlib
import suppress
7 from pathlib
import Path
17 from .const
import CONF_LOCK_DEFAULT_CODE, DOMAIN, LOGGER
18 from .coordinator
import VerisureDataUpdateCoordinator
21 Platform.ALARM_CONTROL_PANEL,
22 Platform.BINARY_SENSOR,
31 """Set up Verisure from a config entry."""
32 await hass.async_add_executor_job(migrate_cookie_files, hass, entry)
36 if not await coordinator.async_login():
39 await coordinator.async_config_entry_first_refresh()
41 hass.data.setdefault(DOMAIN, {})
42 hass.data[DOMAIN][entry.entry_id] = coordinator
47 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
50 entry.async_on_unload(entry.add_update_listener(update_listener))
56 """Handle options update."""
58 coordinator = hass.data[DOMAIN][entry.entry_id]
59 coordinator.async_update_listeners()
63 """Unload Verisure config entry."""
64 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
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)
72 del hass.data[DOMAIN][entry.entry_id]
74 if not hass.data[DOMAIN]:
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():
85 hass.config.path(STORAGE_DIR, f
"verisure_{entry.data[CONF_EMAIL]}")
90 """Migrate old entry."""
91 LOGGER.debug(
"Migrating from version %s", entry.version)
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(
102 {CONF_DEFAULT_CODE: config_entry_default_code},
104 new_options = entry.options.copy()
105 del new_options[CONF_LOCK_DEFAULT_CODE]
107 hass.config_entries.async_update_entry(entry, options=new_options)
109 hass.config_entries.async_update_entry(entry, version=2)
111 LOGGER.debug(
"Migration to version %s successful", entry.version)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
def update_listener(HomeAssistant hass, ConfigEntry entry)
None migrate_cookie_files(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)