|
1 | 1 | """The Vivint integration."""
|
| 2 | + |
2 | 3 | import logging
|
| 4 | +import os |
3 | 5 |
|
4 | 6 | from aiohttp import ClientResponseError
|
5 | 7 | from aiohttp.client_exceptions import ClientConnectorError
|
|
15 | 17 | )
|
16 | 18 |
|
17 | 19 | from homeassistant.config_entries import ConfigEntry
|
18 |
| -from homeassistant.const import ATTR_DEVICE_ID, ATTR_DOMAIN, Platform |
19 |
| -from homeassistant.core import HomeAssistant, callback |
| 20 | +from homeassistant.const import ( |
| 21 | + ATTR_DEVICE_ID, |
| 22 | + ATTR_DOMAIN, |
| 23 | + EVENT_HOMEASSISTANT_STOP, |
| 24 | + Platform, |
| 25 | +) |
| 26 | +from homeassistant.core import Event, HomeAssistant, callback |
20 | 27 | from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
21 | 28 | from homeassistant.helpers import device_registry
|
22 | 29 | from homeassistant.helpers.dispatcher import async_dispatcher_send
|
23 | 30 |
|
24 |
| -from .const import DOMAIN, EVENT_TYPE |
| 31 | +from .const import CONF_REFRESH_TOKEN, DOMAIN, EVENT_TYPE |
25 | 32 | from .hub import VivintHub, get_device_id
|
26 | 33 |
|
| 34 | +type VivintConfigEntry = ConfigEntry[VivintHub] |
| 35 | + |
| 36 | + |
27 | 37 | _LOGGER = logging.getLogger(__name__)
|
28 | 38 |
|
29 | 39 | PLATFORMS = [
|
|
43 | 53 | ATTR_TYPE = "type"
|
44 | 54 |
|
45 | 55 |
|
46 |
| -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 56 | +async def async_setup_entry(hass: HomeAssistant, entry: VivintConfigEntry) -> bool: |
47 | 57 | """Set up Vivint from a config entry."""
|
48 | 58 | undo_listener = entry.add_update_listener(update_listener)
|
49 | 59 |
|
50 | 60 | hub = VivintHub(hass, entry.data, undo_listener)
|
51 |
| - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = hub |
| 61 | + entry.runtime_data = hub |
52 | 62 |
|
53 | 63 | try:
|
54 | 64 | await hub.login(load_devices=True, subscribe_for_realtime_updates=True)
|
@@ -144,26 +154,54 @@ def async_on_device_event(event_type: str, viv_device: VivintDevice) -> None:
|
144 | 154 | if device not in known_devices:
|
145 | 155 | dev_reg.async_remove_device(device.id)
|
146 | 156 |
|
| 157 | + @callback |
| 158 | + def _async_save_tokens(ev: Event) -> None: |
| 159 | + """Save tokens to the config entry data.""" |
| 160 | + undo_listener() |
| 161 | + hass.config_entries.async_update_entry( |
| 162 | + entry, data=entry.data | {CONF_REFRESH_TOKEN: hub.account.refresh_token} |
| 163 | + ) |
| 164 | + |
| 165 | + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_save_tokens) |
| 166 | + |
147 | 167 | return True
|
148 | 168 |
|
149 | 169 |
|
150 |
| -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 170 | +async def async_unload_entry(hass: HomeAssistant, entry: VivintConfigEntry) -> bool: |
151 | 171 | """Unload config entry."""
|
152 |
| - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
| 172 | + await entry.runtime_data.disconnect() |
| 173 | + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
| 174 | + |
| 175 | + |
| 176 | +async def async_migrate_entry(hass: HomeAssistant, entry: VivintConfigEntry) -> bool: |
| 177 | + """Migrate old entry.""" |
| 178 | + _LOGGER.debug( |
| 179 | + "Migrating configuration from version %s.%s", entry.version, entry.minor_version |
| 180 | + ) |
153 | 181 |
|
154 |
| - hub: VivintHub = hass.data[DOMAIN][entry.entry_id] |
155 |
| - await hub.disconnect() |
| 182 | + if entry.version > 1: |
| 183 | + # This means the user has downgraded from a future version |
| 184 | + return False |
156 | 185 |
|
157 |
| - return unload_ok |
| 186 | + if entry.version == 1: |
| 187 | + if entry.minor_version < 2: |
| 188 | + for filename in (".vivintpy_cache.pickle", ".vivintpy_cache_1.pickle"): |
| 189 | + try: |
| 190 | + os.remove(hass.config.path(filename)) |
| 191 | + except Exception: |
| 192 | + pass |
158 | 193 |
|
| 194 | + hass.config_entries.async_update_entry(entry, minor_version=2) |
159 | 195 |
|
160 |
| -async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: |
161 |
| - """Handle removal of an entry.""" |
162 |
| - hub: VivintHub = hass.data[DOMAIN][entry.entry_id] |
163 |
| - await hub.disconnect(remove_cache=True) |
164 |
| - hass.data[DOMAIN].pop(entry.entry_id) |
| 196 | + _LOGGER.debug( |
| 197 | + "Migration to version %s.%s successful", |
| 198 | + entry.version, |
| 199 | + entry.minor_version, |
| 200 | + ) |
| 201 | + |
| 202 | + return True |
165 | 203 |
|
166 | 204 |
|
167 |
| -async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: |
| 205 | +async def update_listener(hass: HomeAssistant, entry: VivintConfigEntry) -> None: |
168 | 206 | """Handle options update."""
|
169 | 207 | await hass.config_entries.async_reload(entry.entry_id)
|
0 commit comments