Move valve service registration to services module (#182459)

This commit is contained in:
epenet
2026-09-17 18:17:35 +02:00
committed by GitHub
parent 361ae7059b
commit fe9fa47ff2
3 changed files with 68 additions and 40 deletions
+4 -39
View File
@@ -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
+11 -1
View File
@@ -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):
@@ -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],
)