-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__init__.py
104 lines (79 loc) · 3.39 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
from datetime import timedelta
import logging
from freebox_api.exceptions import HttpRequestError
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, PLATFORMS, SERVICE_REBOOT
from .router import FreeboxRouter, get_api
FREEBOX_SCHEMA = vol.Schema(
{vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT): cv.port}
)
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [FREEBOX_SCHEMA]))},
),
extra=vol.ALLOW_EXTRA,
)
SCAN_INTERVAL = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Freebox integration."""
if DOMAIN in config:
for entry_config in config[DOMAIN]:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=entry_config
)
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Freebox entry."""
api = await get_api(hass, entry.data[CONF_HOST])
try:
await api.open(entry.data[CONF_HOST], entry.data[CONF_PORT])
except HttpRequestError as err:
raise ConfigEntryNotReady from err
freebox_config = await api.system.get_config()
router = FreeboxRouter(hass, entry, api, freebox_config)
await router.update_all()
entry.async_on_unload(
async_track_time_interval(hass, router.update_all, SCAN_INTERVAL)
)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = router
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# Services
async def async_reboot(call: ServiceCall) -> None:
"""Handle reboot service call."""
# The Freebox reboot service has been replaced by a
# dedicated button entity and marked as deprecated
_LOGGER.warning(
"The 'freebox.reboot' service is deprecated and "
"replaced by a dedicated reboot button entity; please "
"use that entity to reboot the freebox instead"
)
await router.reboot()
hass.services.async_register(DOMAIN, SERVICE_REBOOT, async_reboot)
async def async_close_connection(event: Event) -> None:
"""Close Freebox connection on HA Stop."""
await router.close()
entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
router: FreeboxRouter = hass.data[DOMAIN].pop(entry.unique_id)
await router.close()
hass.services.async_remove(DOMAIN, SERVICE_REBOOT)
return unload_ok