2019-03-19 06:07:39 +00:00
|
|
|
"""The dsmr component."""
|
2020-09-03 21:19:45 +00:00
|
|
|
from asyncio import CancelledError
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2020-09-03 21:19:45 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
2021-06-27 14:58:08 +00:00
|
|
|
from .const import DATA_TASK, DOMAIN, PLATFORMS
|
2020-09-03 21:19:45 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-09-03 21:19:45 +00:00
|
|
|
"""Set up DSMR from a config entry."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = {}
|
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2021-06-27 14:58:08 +00:00
|
|
|
entry.async_on_unload(entry.add_update_listener(async_update_options))
|
2020-11-11 08:21:07 +00:00
|
|
|
|
2020-09-03 21:19:45 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-06-24 10:53:16 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-09-03 21:19:45 +00:00
|
|
|
"""Unload a config entry."""
|
|
|
|
task = hass.data[DOMAIN][entry.entry_id][DATA_TASK]
|
|
|
|
|
|
|
|
# Cancel the reconnect task
|
|
|
|
task.cancel()
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(CancelledError):
|
2020-09-03 21:19:45 +00:00
|
|
|
await task
|
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-09-03 21:19:45 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
2020-11-11 08:21:07 +00:00
|
|
|
|
|
|
|
|
2021-06-24 10:53:16 +00:00
|
|
|
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2020-11-11 08:21:07 +00:00
|
|
|
"""Update options."""
|
2021-05-27 13:56:20 +00:00
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|