core/homeassistant/components/fritz/services.py

102 lines
3.1 KiB
Python
Raw Normal View History

2021-05-11 20:56:52 +00:00
"""Services for Fritz integration."""
from __future__ import annotations
2021-05-11 20:56:52 +00:00
import logging
import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState
2021-05-11 20:56:52 +00:00
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.service import async_extract_config_entry_ids
from .common import AvmWrapper
from .const import (
DOMAIN,
FRITZ_SERVICES,
SERVICE_CLEANUP,
SERVICE_REBOOT,
SERVICE_RECONNECT,
SERVICE_SET_GUEST_WIFI_PW,
)
2021-05-11 20:56:52 +00:00
_LOGGER = logging.getLogger(__name__)
SERVICE_SCHEMA_SET_GUEST_WIFI_PW = vol.Schema(
{
vol.Required("device_id"): str,
vol.Optional("password"): vol.Length(min=8, max=63),
vol.Optional("length"): vol.Range(min=8, max=63),
}
)
2021-05-11 20:56:52 +00:00
SERVICE_LIST: list[tuple[str, vol.Schema | None]] = [
(SERVICE_CLEANUP, None),
(SERVICE_REBOOT, None),
(SERVICE_RECONNECT, None),
(SERVICE_SET_GUEST_WIFI_PW, SERVICE_SCHEMA_SET_GUEST_WIFI_PW),
]
2021-06-13 14:45:35 +00:00
async def async_setup_services(hass: HomeAssistant) -> None:
2021-05-11 20:56:52 +00:00
"""Set up services for Fritz integration."""
for service, _ in SERVICE_LIST:
if hass.services.has_service(DOMAIN, service):
return
2021-05-11 20:56:52 +00:00
2021-06-13 14:45:35 +00:00
async def async_call_fritz_service(service_call: ServiceCall) -> None:
2021-05-11 20:56:52 +00:00
"""Call correct Fritz service."""
if not (
2022-01-06 11:15:40 +00:00
fritzbox_entry_ids := await _async_get_configured_avm_device(
2021-05-11 20:56:52 +00:00
hass, service_call
)
):
raise HomeAssistantError(
f"Failed to call service '{service_call.service}'. Config entry for target not found"
)
for entry_id in fritzbox_entry_ids:
2021-05-11 20:56:52 +00:00
_LOGGER.debug("Executing service %s", service_call.service)
avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry_id]
if config_entry := hass.config_entries.async_get_entry(entry_id):
await avm_wrapper.service_fritzbox(service_call, config_entry)
else:
_LOGGER.error(
"Executing service %s failed, no config entry found",
service_call.service,
)
for service, schema in SERVICE_LIST:
hass.services.async_register(DOMAIN, service, async_call_fritz_service, schema)
2021-05-11 20:56:52 +00:00
2022-01-06 11:15:40 +00:00
async def _async_get_configured_avm_device(
2021-05-11 20:56:52 +00:00
hass: HomeAssistant, service_call: ServiceCall
2021-06-13 14:45:35 +00:00
) -> list:
2021-05-11 20:56:52 +00:00
"""Get FritzBoxTools class from config entry."""
2021-06-13 14:45:35 +00:00
list_entry_id: list = []
for entry_id in await async_extract_config_entry_ids(hass, service_call):
config_entry = hass.config_entries.async_get_entry(entry_id)
if (
config_entry
and config_entry.domain == DOMAIN
and config_entry.state == ConfigEntryState.LOADED
):
list_entry_id.append(entry_id)
return list_entry_id
2021-05-11 20:56:52 +00:00
2021-06-13 14:45:35 +00:00
async def async_unload_services(hass: HomeAssistant) -> None:
2021-05-11 20:56:52 +00:00
"""Unload services for Fritz integration."""
if not hass.data.get(FRITZ_SERVICES):
return
hass.data[FRITZ_SERVICES] = False
for service, _ in SERVICE_LIST:
hass.services.async_remove(DOMAIN, service)