diff --git a/homeassistant/components/cover/__init__.py b/homeassistant/components/cover/__init__.py index 0dbd36396aab..23625cfa813e 100644 --- a/homeassistant/components/cover/__init__.py +++ b/homeassistant/components/cover/__init__.py @@ -6,11 +6,10 @@ import functools as ft import logging from typing import Any, final, override -import probatio from propcache.api import cached_property from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( +from homeassistant.const import ( # noqa: F401 SERVICE_CLOSE_COVER, SERVICE_CLOSE_COVER_TILT, SERVICE_OPEN_COVER, @@ -29,7 +28,6 @@ from homeassistant.helpers.deprecation import deprecated_function from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType -from homeassistant.util.hass_dict import HassKey from .condition import make_cover_is_closed_condition, make_cover_is_open_condition from .const import ( @@ -39,6 +37,7 @@ from .const import ( ATTR_POSITION, ATTR_SPEED, ATTR_TILT_POSITION, + DATA_COMPONENT, DEVICE_CLASSES_SCHEMA, DOMAIN, INTENT_CLOSE_COVER, @@ -49,11 +48,11 @@ from .const import ( CoverEntityStateAttribute, CoverState, ) +from .services import async_setup_services from .trigger import make_cover_closed_trigger, make_cover_opened_trigger _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[CoverEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE @@ -109,81 +108,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await component.async_setup(config) - component.async_register_entity_service( - SERVICE_OPEN_COVER, - {probatio.Optional(ATTR_SPEED): cv.string}, - "async_handle_open_cover", - [CoverEntityFeature.OPEN], - ) - - component.async_register_entity_service( - SERVICE_CLOSE_COVER, - {probatio.Optional(ATTR_SPEED): cv.string}, - "async_handle_close_cover", - [CoverEntityFeature.CLOSE], - ) - - component.async_register_entity_service( - SERVICE_SET_COVER_POSITION, - { - probatio.Required(ATTR_POSITION): probatio.All( - probatio.Coerce(int), probatio.Range(min=0, max=100) - ), - probatio.Optional(ATTR_SPEED): cv.string, - }, - "async_handle_set_cover_position", - [CoverEntityFeature.SET_POSITION], - ) - - component.async_register_entity_service( - SERVICE_STOP_COVER, None, "async_stop_cover", [CoverEntityFeature.STOP] - ) - - component.async_register_entity_service( - SERVICE_TOGGLE, - None, - "async_toggle", - [CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE], - ) - - component.async_register_entity_service( - SERVICE_OPEN_COVER_TILT, - None, - "async_open_cover_tilt", - [CoverEntityFeature.OPEN_TILT], - ) - - component.async_register_entity_service( - SERVICE_CLOSE_COVER_TILT, - None, - "async_close_cover_tilt", - [CoverEntityFeature.CLOSE_TILT], - ) - - component.async_register_entity_service( - SERVICE_STOP_COVER_TILT, - None, - "async_stop_cover_tilt", - [CoverEntityFeature.STOP_TILT], - ) - - component.async_register_entity_service( - SERVICE_SET_COVER_TILT_POSITION, - { - probatio.Required(ATTR_TILT_POSITION): probatio.All( - probatio.Coerce(int), probatio.Range(min=0, max=100) - ) - }, - "async_set_cover_tilt_position", - [CoverEntityFeature.SET_TILT_POSITION], - ) - - component.async_register_entity_service( - SERVICE_TOGGLE_COVER_TILT, - None, - "async_toggle_tilt", - [CoverEntityFeature.OPEN_TILT | CoverEntityFeature.CLOSE_TILT], - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/cover/const.py b/homeassistant/components/cover/const.py index aadf0b13985d..b33a6caffa79 100644 --- a/homeassistant/components/cover/const.py +++ b/homeassistant/components/cover/const.py @@ -1,11 +1,19 @@ """Constants for cover 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 . import CoverEntity + DOMAIN: Final = "cover" +DATA_COMPONENT: HassKey[EntityComponent[CoverEntity]] = HassKey(DOMAIN) ATTR_CURRENT_POSITION = "current_position" ATTR_CURRENT_TILT_POSITION = "current_tilt_position" diff --git a/homeassistant/components/cover/services.py b/homeassistant/components/cover/services.py new file mode 100644 index 000000000000..74cee492945d --- /dev/null +++ b/homeassistant/components/cover/services.py @@ -0,0 +1,108 @@ +"""Services for the Cover integration.""" + +import probatio + +from homeassistant.const import ( + SERVICE_CLOSE_COVER, + SERVICE_CLOSE_COVER_TILT, + SERVICE_OPEN_COVER, + SERVICE_OPEN_COVER_TILT, + SERVICE_SET_COVER_POSITION, + SERVICE_SET_COVER_TILT_POSITION, + SERVICE_STOP_COVER, + SERVICE_STOP_COVER_TILT, + SERVICE_TOGGLE, + SERVICE_TOGGLE_COVER_TILT, +) +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv + +from .const import ( + ATTR_POSITION, + ATTR_SPEED, + ATTR_TILT_POSITION, + DATA_COMPONENT, + CoverEntityFeature, +) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the cover services.""" + component = hass.data[DATA_COMPONENT] + + component.async_register_entity_service( + SERVICE_OPEN_COVER, + {probatio.Optional(ATTR_SPEED): cv.string}, + "async_handle_open_cover", + [CoverEntityFeature.OPEN], + ) + + component.async_register_entity_service( + SERVICE_CLOSE_COVER, + {probatio.Optional(ATTR_SPEED): cv.string}, + "async_handle_close_cover", + [CoverEntityFeature.CLOSE], + ) + + component.async_register_entity_service( + SERVICE_SET_COVER_POSITION, + { + probatio.Required(ATTR_POSITION): probatio.All( + probatio.Coerce(int), probatio.Range(min=0, max=100) + ), + probatio.Optional(ATTR_SPEED): cv.string, + }, + "async_handle_set_cover_position", + [CoverEntityFeature.SET_POSITION], + ) + + component.async_register_entity_service( + SERVICE_STOP_COVER, None, "async_stop_cover", [CoverEntityFeature.STOP] + ) + + component.async_register_entity_service( + SERVICE_TOGGLE, + None, + "async_toggle", + [CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE], + ) + + component.async_register_entity_service( + SERVICE_OPEN_COVER_TILT, + None, + "async_open_cover_tilt", + [CoverEntityFeature.OPEN_TILT], + ) + + component.async_register_entity_service( + SERVICE_CLOSE_COVER_TILT, + None, + "async_close_cover_tilt", + [CoverEntityFeature.CLOSE_TILT], + ) + + component.async_register_entity_service( + SERVICE_STOP_COVER_TILT, + None, + "async_stop_cover_tilt", + [CoverEntityFeature.STOP_TILT], + ) + + component.async_register_entity_service( + SERVICE_SET_COVER_TILT_POSITION, + { + probatio.Required(ATTR_TILT_POSITION): probatio.All( + probatio.Coerce(int), probatio.Range(min=0, max=100) + ) + }, + "async_set_cover_tilt_position", + [CoverEntityFeature.SET_TILT_POSITION], + ) + + component.async_register_entity_service( + SERVICE_TOGGLE_COVER_TILT, + None, + "async_toggle_tilt", + [CoverEntityFeature.OPEN_TILT | CoverEntityFeature.CLOSE_TILT], + )