Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for ASUSWRT devices."""
2 
3 from homeassistant.config_entries import ConfigEntry
4 from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform
5 from homeassistant.core import Event, HomeAssistant
6 
7 from .router import AsusWrtRouter
8 
9 PLATFORMS = [Platform.DEVICE_TRACKER, Platform.SENSOR]
10 
11 type AsusWrtConfigEntry = ConfigEntry[AsusWrtRouter]
12 
13 
14 async def async_setup_entry(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> bool:
15  """Set up AsusWrt platform."""
16 
17  router = AsusWrtRouter(hass, entry)
18  await router.setup()
19 
20  router.async_on_close(entry.add_update_listener(update_listener))
21 
22  async def async_close_connection(event: Event) -> None:
23  """Close AsusWrt connection on HA Stop."""
24  await router.close()
25 
26  entry.async_on_unload(
27  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
28  )
29 
30  entry.runtime_data = router
31 
32  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
33 
34  return True
35 
36 
37 async def async_unload_entry(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> bool:
38  """Unload a config entry."""
39  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
40  router = entry.runtime_data
41  await router.close()
42 
43  return unload_ok
44 
45 
46 async def update_listener(hass: HomeAssistant, entry: AsusWrtConfigEntry) -> None:
47  """Update when config_entry options update."""
48  router = entry.runtime_data
49 
50  if router.update_options(entry.options):
51  await hass.config_entries.async_reload(entry.entry_id)
None update_listener(HomeAssistant hass, AsusWrtConfigEntry entry)
Definition: __init__.py:46
bool async_unload_entry(HomeAssistant hass, AsusWrtConfigEntry entry)
Definition: __init__.py:37
bool async_setup_entry(HomeAssistant hass, AsusWrtConfigEntry entry)
Definition: __init__.py:14