Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for AVM Fritz!Box functions."""
2 
3 import logging
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import (
7  CONF_HOST,
8  CONF_PASSWORD,
9  CONF_PORT,
10  CONF_SSL,
11  CONF_USERNAME,
12 )
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
15 
16 from .const import (
17  DATA_FRITZ,
18  DEFAULT_SSL,
19  DOMAIN,
20  FRITZ_AUTH_EXCEPTIONS,
21  FRITZ_EXCEPTIONS,
22  PLATFORMS,
23 )
24 from .coordinator import AvmWrapper, FritzData
25 from .services import async_setup_services, async_unload_services
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Set up fritzboxtools from config entry."""
32  _LOGGER.debug("Setting up FRITZ!Box Tools component")
33  avm_wrapper = AvmWrapper(
34  hass=hass,
35  host=entry.data[CONF_HOST],
36  port=entry.data[CONF_PORT],
37  username=entry.data[CONF_USERNAME],
38  password=entry.data[CONF_PASSWORD],
39  use_tls=entry.data.get(CONF_SSL, DEFAULT_SSL),
40  )
41 
42  try:
43  await avm_wrapper.async_setup(entry.options)
44  except FRITZ_AUTH_EXCEPTIONS as ex:
45  raise ConfigEntryAuthFailed from ex
46  except FRITZ_EXCEPTIONS as ex:
47  raise ConfigEntryNotReady from ex
48 
49  if (
50  "X_AVM-DE_UPnP1" in avm_wrapper.connection.services
51  and not (await avm_wrapper.async_get_upnp_configuration())["NewEnable"]
52  ):
53  raise ConfigEntryAuthFailed("Missing UPnP configuration")
54 
55  await avm_wrapper.async_config_entry_first_refresh()
56 
57  hass.data.setdefault(DOMAIN, {})
58  hass.data[DOMAIN][entry.entry_id] = avm_wrapper
59 
60  if DATA_FRITZ not in hass.data:
61  hass.data[DATA_FRITZ] = FritzData()
62 
63  entry.async_on_unload(entry.add_update_listener(update_listener))
64 
65  # Load the other platforms like switch
66  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
67 
68  await async_setup_services(hass)
69 
70  return True
71 
72 
73 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
74  """Unload FRITZ!Box Tools config entry."""
75  avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id]
76 
77  fritz_data = hass.data[DATA_FRITZ]
78  fritz_data.tracked.pop(avm_wrapper.unique_id)
79 
80  if not bool(fritz_data.tracked):
81  hass.data.pop(DATA_FRITZ)
82 
83  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
84  if unload_ok:
85  hass.data[DOMAIN].pop(entry.entry_id)
86 
87  await async_unload_services(hass)
88 
89  return unload_ok
90 
91 
92 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
93  """Update when config_entry options update."""
94  if entry.options:
95  await hass.config_entries.async_reload(entry.entry_id)
None async_unload_services(HomeAssistant hass)
Definition: services.py:85
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:92
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:73
None async_setup_services(HomeAssistant hass)
Definition: __init__.py:72