From 5b6d5e816ea903ebb532f74cc1e5bd100368b15a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 22 Sep 2026 18:50:13 +0100 Subject: [PATCH] Move keba service registration to services module (#182208) --- homeassistant/components/keba/__init__.py | 31 ++++------------------- homeassistant/components/keba/const.py | 3 +++ homeassistant/components/keba/services.py | 30 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 homeassistant/components/keba/const.py create mode 100644 homeassistant/components/keba/services.py diff --git a/homeassistant/components/keba/__init__.py b/homeassistant/components/keba/__init__.py index 6ed70942346a..08cac519f374 100644 --- a/homeassistant/components/keba/__init__.py +++ b/homeassistant/components/keba/__init__.py @@ -7,13 +7,15 @@ from keba_kecontact.connection import KebaKeContact import probatio from homeassistant.const import CONF_HOST, Platform -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.typing import ConfigType +from .const import DOMAIN +from .services import async_setup_services + _LOGGER = logging.getLogger(__name__) -DOMAIN = "keba" PLATFORMS = (Platform.BINARY_SENSOR, Platform.SENSOR, Platform.LOCK, Platform.NOTIFY) CONF_RFID = "rfid" @@ -43,17 +45,6 @@ CONFIG_SCHEMA = probatio.Schema( extra=probatio.ALLOW_EXTRA, ) -_SERVICE_MAP = { - "request_data": "async_request_data", - "set_energy": "async_set_energy", - "set_current": "async_set_current", - "authorize": "async_start", - "deauthorize": "async_stop", - "enable": "async_enable_ev", - "disable": "async_disable_ev", - "set_failsafe": "async_set_failsafe", -} - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Check connectivity and version of KEBA charging station.""" @@ -78,19 +69,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: except ValueError as ex: _LOGGER.warning("Could not set failsafe mode %s", ex) - # Register services to hass - async def execute_service(call: ServiceCall) -> None: - """Execute a service to KEBA charging station. - - This must be a member function as we need access to the keba - object here. - """ - function_name = _SERVICE_MAP[call.service] - function_call = getattr(keba, function_name) - await function_call(call.data) - - for service in _SERVICE_MAP: - hass.services.async_register(DOMAIN, service, execute_service) + async_setup_services(hass) # Load components for platform in PLATFORMS: diff --git a/homeassistant/components/keba/const.py b/homeassistant/components/keba/const.py new file mode 100644 index 000000000000..3e29c4a3c894 --- /dev/null +++ b/homeassistant/components/keba/const.py @@ -0,0 +1,3 @@ +"""Constants for the KEBA charging station integration.""" + +DOMAIN = "keba" diff --git a/homeassistant/components/keba/services.py b/homeassistant/components/keba/services.py new file mode 100644 index 000000000000..12ceb27e94bc --- /dev/null +++ b/homeassistant/components/keba/services.py @@ -0,0 +1,30 @@ +"""Support for KEBA charging station services.""" + +from homeassistant.core import HomeAssistant, ServiceCall, callback + +from .const import DOMAIN + +_SERVICE_MAP = { + "request_data": "async_request_data", + "set_energy": "async_set_energy", + "set_current": "async_set_current", + "authorize": "async_start", + "deauthorize": "async_stop", + "enable": "async_enable_ev", + "disable": "async_disable_ev", + "set_failsafe": "async_set_failsafe", +} + + +async def _async_execute_service(call: ServiceCall) -> None: + """Execute a service to KEBA charging station.""" + keba = call.hass.data[DOMAIN] + function_call = getattr(keba, _SERVICE_MAP[call.service]) + await function_call(call.data) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the KEBA services.""" + for service in _SERVICE_MAP: + hass.services.async_register(DOMAIN, service, _async_execute_service)