2021-04-25 10:10:33 +00:00
|
|
|
"""Support for AVM Fritz!Box functions."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from fritzconnection.core.exceptions import FritzConnectionException, FritzSecurityError
|
2021-11-27 15:56:53 +00:00
|
|
|
from fritzconnection.core.logger import fritzlogger
|
2021-04-25 10:10:33 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
)
|
2021-06-13 14:45:35 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant, callback
|
2021-04-25 10:10:33 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
|
|
|
|
2021-04-29 18:10:36 +00:00
|
|
|
from .common import FritzBoxTools, FritzData
|
|
|
|
from .const import DATA_FRITZ, DOMAIN, PLATFORMS
|
2021-05-11 20:56:52 +00:00
|
|
|
from .services import async_setup_services, async_unload_services
|
2021-04-25 10:10:33 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-11-27 15:56:53 +00:00
|
|
|
level = _LOGGER.getEffectiveLevel()
|
|
|
|
_LOGGER.info(
|
|
|
|
"Setting logging level of fritzconnection: %s", logging.getLevelName(level)
|
|
|
|
)
|
|
|
|
fritzlogger.set_level(level)
|
|
|
|
fritzlogger.enable()
|
|
|
|
|
2021-04-25 10:10:33 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up fritzboxtools from config entry."""
|
|
|
|
_LOGGER.debug("Setting up FRITZ!Box Tools component")
|
|
|
|
fritz_tools = FritzBoxTools(
|
|
|
|
hass=hass,
|
|
|
|
host=entry.data[CONF_HOST],
|
|
|
|
port=entry.data[CONF_PORT],
|
|
|
|
username=entry.data[CONF_USERNAME],
|
|
|
|
password=entry.data[CONF_PASSWORD],
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await fritz_tools.async_setup()
|
2021-05-24 14:54:57 +00:00
|
|
|
await fritz_tools.async_start(entry.options)
|
2021-04-25 10:10:33 +00:00
|
|
|
except FritzSecurityError as ex:
|
|
|
|
raise ConfigEntryAuthFailed from ex
|
|
|
|
except FritzConnectionException as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = fritz_tools
|
|
|
|
|
2021-04-29 18:10:36 +00:00
|
|
|
if DATA_FRITZ not in hass.data:
|
|
|
|
hass.data[DATA_FRITZ] = FritzData()
|
|
|
|
|
2021-04-25 10:10:33 +00:00
|
|
|
@callback
|
2021-06-13 14:45:35 +00:00
|
|
|
def _async_unload(event: Event) -> None:
|
2021-04-25 10:10:33 +00:00
|
|
|
fritz_tools.async_unload()
|
|
|
|
|
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_unload)
|
|
|
|
)
|
2021-05-24 14:54:57 +00:00
|
|
|
entry.async_on_unload(entry.add_update_listener(update_listener))
|
|
|
|
|
2021-04-25 10:10:33 +00:00
|
|
|
# Load the other platforms like switch
|
2021-04-27 06:46:49 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2021-04-25 10:10:33 +00:00
|
|
|
|
2021-05-11 20:56:52 +00:00
|
|
|
await async_setup_services(hass)
|
|
|
|
|
2021-04-25 10:10:33 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-05-04 03:11:21 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-04-25 10:10:33 +00:00
|
|
|
"""Unload FRITZ!Box Tools config entry."""
|
|
|
|
fritzbox: FritzBoxTools = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
fritzbox.async_unload()
|
|
|
|
|
2021-04-29 18:10:36 +00:00
|
|
|
fritz_data = hass.data[DATA_FRITZ]
|
|
|
|
fritz_data.tracked.pop(fritzbox.unique_id)
|
|
|
|
|
|
|
|
if not bool(fritz_data.tracked):
|
|
|
|
hass.data.pop(DATA_FRITZ)
|
|
|
|
|
2021-04-27 06:46:49 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-04-25 10:10:33 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
2021-05-11 20:56:52 +00:00
|
|
|
await async_unload_services(hass)
|
|
|
|
|
2021-04-25 10:10:33 +00:00
|
|
|
return unload_ok
|
2021-05-24 14:54:57 +00:00
|
|
|
|
|
|
|
|
2021-06-13 14:45:35 +00:00
|
|
|
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2021-05-24 14:54:57 +00:00
|
|
|
"""Update when config_entry options update."""
|
|
|
|
if entry.options:
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|