From 46c9097c145f64ccf5d2612d747fcbdf3dab7ffc Mon Sep 17 00:00:00 2001 From: indevolt-y Date: Tue, 10 Nov 2026 20:55:08 +0800 Subject: [PATCH] Add action enums to Indevolt integration (#191868) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/indevolt/services.py | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/indevolt/services.py b/homeassistant/components/indevolt/services.py index 4af2898bd720..e7068e332f3e 100644 --- a/homeassistant/components/indevolt/services.py +++ b/homeassistant/components/indevolt/services.py @@ -1,6 +1,7 @@ """Services for Indevolt integration.""" import asyncio +from enum import StrEnum from typing import Final, Never from indevolt_api import ( @@ -10,6 +11,7 @@ from indevolt_api import ( ) import probatio +from homeassistant.const import CommonServiceArgument from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError import homeassistant.helpers.config_validation as cv @@ -18,17 +20,32 @@ from homeassistant.helpers.service import async_extract_config_entry_ids from .const import DOMAIN from .coordinator import IndevoltCoordinator + +class IndevoltService(StrEnum): + """Store keys for Indevolt services.""" + + CHARGE = "charge" + DISCHARGE = "discharge" + + +class IndevoltServiceArgument(StrEnum): + """Store keys for Indevolt service arguments.""" + + POWER = "power" + TARGET_SOC = "target_soc" + + RT_ACTION_SERVICE_SCHEMA: Final = probatio.Schema( { - probatio.Required("device_id"): probatio.All( + probatio.Required(CommonServiceArgument.DEVICE_ID): probatio.All( probatio.EnsureList(), [cv.string], ), - probatio.Required("target_soc"): probatio.All( + probatio.Required(IndevoltServiceArgument.TARGET_SOC): probatio.All( probatio.Coerce(int), probatio.Range(min=0, max=100), ), - probatio.Required("power"): probatio.All( + probatio.Required(IndevoltServiceArgument.POWER): probatio.All( probatio.Coerce(int), probatio.Range(min=0, max=10800), ), @@ -51,10 +68,10 @@ def async_setup_services(hass: HomeAssistant) -> None: ) hass.services.async_register( - DOMAIN, "charge", charge, schema=RT_ACTION_SERVICE_SCHEMA + DOMAIN, IndevoltService.CHARGE, charge, schema=RT_ACTION_SERVICE_SCHEMA ) hass.services.async_register( - DOMAIN, "discharge", discharge, schema=RT_ACTION_SERVICE_SCHEMA + DOMAIN, IndevoltService.DISCHARGE, discharge, schema=RT_ACTION_SERVICE_SCHEMA ) @@ -66,8 +83,8 @@ async def _async_handle_realtime_action( """Validate and execute a realtime action for one or more coordinators.""" coordinators = await _async_get_coordinators_from_call(hass, call) - power: int = call.data["power"] - target_soc: int = call.data["target_soc"] + power: int = call.data[IndevoltServiceArgument.POWER] + target_soc: int = call.data[IndevoltServiceArgument.TARGET_SOC] _validate_realtime_action(coordinators, action, power, target_soc) await _execute_realtime_action(coordinators, action, power, target_soc)