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
|
|
|
|
|
|
|
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
|
|
|
|
2024-05-17 13:42:58 +00:00
|
|
|
type AsusWrtConfigEntry = ConfigEntry[AsusWrtRouter]
|
2020-04-03 13:02:48 +00:00
|
|
|
|
2024-05-06 13:00:15 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> 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
|
|
|
|
2024-05-06 13:00:15 +00:00
|
|
|
entry.runtime_data = router
|
2022-02-06 08:35:49 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2024-05-06 13:00:15 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> 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):
|
2024-05-06 13:00:15 +00:00
|
|
|
router = entry.runtime_data
|
2021-02-13 18:17:06 +00:00
|
|
|
await router.close()
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2024-05-06 13:00:15 +00:00
|
|
|
async def update_listener(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> None:
|
2021-02-13 18:17:06 +00:00
|
|
|
"""Update when config_entry options update."""
|
2024-05-06 13:00:15 +00:00
|
|
|
router = entry.runtime_data
|
2021-02-13 18:17:06 +00:00
|
|
|
|
|
|
|
if router.update_options(entry.options):
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|