From 22de2f1bfcea100166f165d42673bfa0af8e5dab Mon Sep 17 00:00:00 2001 From: mib1185 Date: Sat, 25 Jan 2025 10:59:08 +0000 Subject: [PATCH] move back to own module --- homeassistant/components/fritz/__init__.py | 55 ++------------------ homeassistant/components/fritz/services.py | 59 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 homeassistant/components/fritz/services.py diff --git a/homeassistant/components/fritz/__init__.py b/homeassistant/components/fritz/__init__.py index b7194d9cabd..1ffcb21c442 100644 --- a/homeassistant/components/fritz/__init__.py +++ b/homeassistant/components/fritz/__init__.py @@ -2,8 +2,6 @@ import logging -import voluptuous as vol - from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_HOST, @@ -12,14 +10,9 @@ from homeassistant.const import ( CONF_SSL, CONF_USERNAME, ) -from homeassistant.core import HomeAssistant, ServiceCall -from homeassistant.exceptions import ( - ConfigEntryAuthFailed, - ConfigEntryNotReady, - HomeAssistantError, -) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.service import async_extract_config_entry_ids from homeassistant.helpers.typing import ConfigType from .const import ( @@ -29,58 +22,18 @@ from .const import ( FRITZ_AUTH_EXCEPTIONS, FRITZ_EXCEPTIONS, PLATFORMS, - SERVICE_SET_GUEST_WIFI_PW, ) from .coordinator import AvmWrapper, FritzData +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) -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), - } -) - -SERVICE_LIST: list[tuple[str, vol.Schema | None]] = [ - (SERVICE_SET_GUEST_WIFI_PW, SERVICE_SCHEMA_SET_GUEST_WIFI_PW), -] - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up fritzboxtools services.""" - for service, _ in SERVICE_LIST: - if hass.services.has_service(DOMAIN, service): - return True - - async def async_call_fritz_service(service_call: ServiceCall) -> None: - """Call correct Fritz service.""" - - target_entry_ids = await async_extract_config_entry_ids(hass, service_call) - target_entries = [ - loaded_entry - for loaded_entry in hass.config_entries.async_loaded_entries(DOMAIN) - if loaded_entry.entry_id in target_entry_ids - ] - - if not target_entries: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="config_entry_not_found", - translation_placeholders={"service": service_call.service}, - ) - - for target_entry in target_entries: - _LOGGER.debug("Executing service %s", service_call.service) - avm_wrapper: AvmWrapper = hass.data[DOMAIN][target_entry.entry_id] - await avm_wrapper.service_fritzbox(service_call, target_entry) - - for service, schema in SERVICE_LIST: - hass.services.async_register(DOMAIN, service, async_call_fritz_service, schema) - + await async_setup_services(hass) return True diff --git a/homeassistant/components/fritz/services.py b/homeassistant/components/fritz/services.py new file mode 100644 index 00000000000..8372c06fce6 --- /dev/null +++ b/homeassistant/components/fritz/services.py @@ -0,0 +1,59 @@ +"""Services for Fritz integration.""" + +import logging + +import voluptuous as vol + +from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.service import async_extract_config_entry_ids + +from .const import DOMAIN, SERVICE_SET_GUEST_WIFI_PW +from .coordinator import AvmWrapper + +_LOGGER = logging.getLogger(__name__) + +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + +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), + } +) + +SERVICE_LIST: list[tuple[str, vol.Schema | None]] = [ + (SERVICE_SET_GUEST_WIFI_PW, SERVICE_SCHEMA_SET_GUEST_WIFI_PW), +] + + +async def _async_call_fritz_service(service_call: ServiceCall) -> None: + """Call correct Fritz service.""" + hass = service_call.hass + target_entry_ids = await async_extract_config_entry_ids(hass, service_call) + target_entries = [ + loaded_entry + for loaded_entry in hass.config_entries.async_loaded_entries(DOMAIN) + if loaded_entry.entry_id in target_entry_ids + ] + + if not target_entries: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="config_entry_not_found", + translation_placeholders={"service": service_call.service}, + ) + + for target_entry in target_entries: + _LOGGER.debug("Executing service %s", service_call.service) + avm_wrapper: AvmWrapper = hass.data[DOMAIN][target_entry.entry_id] + await avm_wrapper.service_fritzbox(service_call, target_entry) + + +async def async_setup_services(hass: HomeAssistant) -> None: + """Set up services for Fritz integration.""" + + for service, schema in SERVICE_LIST: + hass.services.async_register(DOMAIN, service, _async_call_fritz_service, schema)