From fe9fa47ff200fecaacce99ae30279d31f24490e5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 17 Sep 2026 18:17:35 +0200 Subject: [PATCH] Move valve service registration to services module (#182459) --- homeassistant/components/valve/__init__.py | 43 ++---------------- homeassistant/components/valve/const.py | 12 ++++- homeassistant/components/valve/services.py | 53 ++++++++++++++++++++++ 3 files changed, 68 insertions(+), 40 deletions(-) create mode 100644 homeassistant/components/valve/services.py diff --git a/homeassistant/components/valve/__init__.py b/homeassistant/components/valve/__init__.py index 1f619ad2efee..b4fa62620c2e 100644 --- a/homeassistant/components/valve/__init__.py +++ b/homeassistant/components/valve/__init__.py @@ -3,8 +3,6 @@ from datetime import timedelta import logging -import probatio - from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( # noqa: F401 SERVICE_CLOSE_VALVE, @@ -21,9 +19,10 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType -from homeassistant.util.hass_dict import HassKey from .const import ( # noqa: F401 + ATTR_POSITION, + DATA_COMPONENT, DEVICE_CLASSES_SCHEMA, DOMAIN, ValveDeviceClass, @@ -37,19 +36,16 @@ from .entity import ( # noqa: F401 ValveEntity, ValveEntityDescription, ) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[ValveEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE SCAN_INTERVAL = timedelta(seconds=15) -ATTR_POSITION = "position" - - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Track states and offer events for valves.""" component = hass.data[DATA_COMPONENT] = EntityComponent[ValveEntity]( @@ -58,38 +54,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await component.async_setup(config) - component.async_register_entity_service( - SERVICE_OPEN_VALVE, None, "async_handle_open_valve", [ValveEntityFeature.OPEN] - ) - - component.async_register_entity_service( - SERVICE_CLOSE_VALVE, - None, - "async_handle_close_valve", - [ValveEntityFeature.CLOSE], - ) - - component.async_register_entity_service( - SERVICE_SET_VALVE_POSITION, - { - probatio.Required(ATTR_POSITION): probatio.All( - probatio.Coerce(int), probatio.Range(min=0, max=100) - ) - }, - "async_set_valve_position", - [ValveEntityFeature.SET_POSITION], - ) - - component.async_register_entity_service( - SERVICE_STOP_VALVE, None, "async_stop_valve", [ValveEntityFeature.STOP] - ) - - component.async_register_entity_service( - SERVICE_TOGGLE, - None, - "async_toggle", - [ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE], - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/valve/const.py b/homeassistant/components/valve/const.py index 35afe1ac5710..72d128ba7676 100644 --- a/homeassistant/components/valve/const.py +++ b/homeassistant/components/valve/const.py @@ -1,11 +1,21 @@ """Constants for the Valve entity platform.""" from enum import IntFlag, StrEnum -from typing import Final +from typing import TYPE_CHECKING, Final import probatio +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from homeassistant.helpers.entity_component import EntityComponent + + from .entity import ValveEntity + DOMAIN: Final = "valve" +DATA_COMPONENT: HassKey[EntityComponent[ValveEntity]] = HassKey(DOMAIN) + +ATTR_POSITION = "position" class ValveEntityStateAttribute(StrEnum): diff --git a/homeassistant/components/valve/services.py b/homeassistant/components/valve/services.py new file mode 100644 index 000000000000..1c566f3bc4f4 --- /dev/null +++ b/homeassistant/components/valve/services.py @@ -0,0 +1,53 @@ +"""Services for the Valve integration.""" + +import probatio + +from homeassistant.const import ( + SERVICE_CLOSE_VALVE, + SERVICE_OPEN_VALVE, + SERVICE_SET_VALVE_POSITION, + SERVICE_STOP_VALVE, + SERVICE_TOGGLE, +) +from homeassistant.core import HomeAssistant, callback + +from .const import ATTR_POSITION, DATA_COMPONENT, ValveEntityFeature + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the valve services.""" + component = hass.data[DATA_COMPONENT] + + component.async_register_entity_service( + SERVICE_OPEN_VALVE, None, "async_handle_open_valve", [ValveEntityFeature.OPEN] + ) + + component.async_register_entity_service( + SERVICE_CLOSE_VALVE, + None, + "async_handle_close_valve", + [ValveEntityFeature.CLOSE], + ) + + component.async_register_entity_service( + SERVICE_SET_VALVE_POSITION, + { + probatio.Required(ATTR_POSITION): probatio.All( + probatio.Coerce(int), probatio.Range(min=0, max=100) + ) + }, + "async_set_valve_position", + [ValveEntityFeature.SET_POSITION], + ) + + component.async_register_entity_service( + SERVICE_STOP_VALVE, None, "async_stop_valve", [ValveEntityFeature.STOP] + ) + + component.async_register_entity_service( + SERVICE_TOGGLE, + None, + "async_toggle", + [ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE], + )