Move keba service registration to services module (#182208)

This commit is contained in:
epenet
2026-09-22 19:50:13 +02:00
committed by GitHub
parent 67fc612109
commit 5b6d5e816e
3 changed files with 38 additions and 26 deletions
+5 -26
View File
@@ -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:
+3
View File
@@ -0,0 +1,3 @@
"""Constants for the KEBA charging station integration."""
DOMAIN = "keba"
+30
View File
@@ -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)