2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ASUSWRT devices."""
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2022-02-06 15:03:04 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform
|
2022-04-22 07:14:13 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant
|
2021-02-13 18:17:06 +00:00
|
|
|
|
2022-02-06 15:03:04 +00:00
|
|
|
from .const import DATA_ASUSWRT, DOMAIN
|
2021-02-13 18:17:06 +00:00
|
|
|
from .router import AsusWrtRouter
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2021-12-03 16:51:30 +00:00
|
|
|
PLATFORMS = [Platform.DEVICE_TRACKER, Platform.SENSOR]
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2020-04-03 13:02:48 +00:00
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-13 18:17:06 +00:00
|
|
|
"""Set up AsusWrt platform."""
|
2020-04-03 13:02:48 +00:00
|
|
|
|
2021-02-13 18:17:06 +00:00
|
|
|
router = AsusWrtRouter(hass, entry)
|
|
|
|
await router.setup()
|
2018-11-07 17:32:13 +00:00
|
|
|
|
2021-02-13 18:17:06 +00:00
|
|
|
router.async_on_close(entry.add_update_listener(update_listener))
|
|
|
|
|
2022-04-22 07:14:13 +00:00
|
|
|
async def async_close_connection(event: Event) -> None:
|
2021-02-13 18:17:06 +00:00
|
|
|
"""Close AsusWrt connection on HA Stop."""
|
|
|
|
await router.close()
|
|
|
|
|
2022-02-06 08:35:49 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-02-13 18:17:06 +00:00
|
|
|
|
2022-02-06 08:35:49 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_ASUSWRT: router}
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2019-01-08 14:14:16 +00:00
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
return True
|
2021-02-13 18:17:06 +00:00
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-13 18:17:06 +00:00
|
|
|
"""Unload a config entry."""
|
2022-02-06 08:35:49 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2021-02-13 18:17:06 +00:00
|
|
|
router = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]
|
|
|
|
await router.close()
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2022-02-02 09:36:04 +00:00
|
|
|
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2021-02-13 18:17:06 +00:00
|
|
|
"""Update when config_entry options update."""
|
|
|
|
router = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]
|
|
|
|
|
|
|
|
if router.update_options(entry.options):
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|