Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from freebox_api.exceptions import HttpRequestError
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
10 from homeassistant.core import Event, HomeAssistant, ServiceCall
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers.event import async_track_time_interval
13 
14 from .const import DOMAIN, PLATFORMS, SERVICE_REBOOT
15 from .router import FreeboxRouter, get_api
16 
17 SCAN_INTERVAL = timedelta(seconds=30)
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Set up Freebox entry."""
24  api = await get_api(hass, entry.data[CONF_HOST])
25  try:
26  await api.open(entry.data[CONF_HOST], entry.data[CONF_PORT])
27  except HttpRequestError as err:
28  raise ConfigEntryNotReady from err
29 
30  freebox_config = await api.system.get_config()
31 
32  router = FreeboxRouter(hass, entry, api, freebox_config)
33  await router.update_all()
34  entry.async_on_unload(
35  async_track_time_interval(hass, router.update_all, SCAN_INTERVAL)
36  )
37 
38  hass.data.setdefault(DOMAIN, {})
39  hass.data[DOMAIN][entry.unique_id] = router
40 
41  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
42 
43  # Services
44  async def async_reboot(call: ServiceCall) -> None:
45  """Handle reboot service call."""
46  # The Freebox reboot service has been replaced by a
47  # dedicated button entity and marked as deprecated
48  _LOGGER.warning(
49  "The 'freebox.reboot' service is deprecated and "
50  "replaced by a dedicated reboot button entity; please "
51  "use that entity to reboot the freebox instead"
52  )
53  await router.reboot()
54 
55  hass.services.async_register(DOMAIN, SERVICE_REBOOT, async_reboot)
56 
57  async def async_close_connection(event: Event) -> None:
58  """Close Freebox connection on HA Stop."""
59  await router.close()
60 
61  entry.async_on_unload(
62  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
63  )
64 
65  return True
66 
67 
68 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
69  """Unload a config entry."""
70  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
71  if unload_ok:
72  router: FreeboxRouter = hass.data[DOMAIN].pop(entry.unique_id)
73  await router.close()
74  hass.services.async_remove(DOMAIN, SERVICE_REBOOT)
75 
76  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:68
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22
transmission_rpc.Client get_api(HomeAssistant hass, dict[str, Any] entry)
Definition: __init__.py:279
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)
Definition: event.py:1679