From 482734ccd136a3e9dc698246d07bef81d13e287c Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Sun, 11 Oct 2026 09:30:58 +0300 Subject: [PATCH] Add service to set Shely RPC cover position and tilt in a single call (#185715) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/shelly/cover.py | 7 +++ homeassistant/components/shelly/icons.json | 3 ++ homeassistant/components/shelly/services.py | 33 +++++++++++- homeassistant/components/shelly/services.yaml | 24 +++++++++ homeassistant/components/shelly/strings.json | 14 +++++ tests/components/shelly/test_cover.py | 54 ++++++++++++++++++- 6 files changed, 133 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/shelly/cover.py b/homeassistant/components/shelly/cover.py index 6b3389261f33..f32785aa29be 100644 --- a/homeassistant/components/shelly/cover.py +++ b/homeassistant/components/shelly/cover.py @@ -371,6 +371,13 @@ class RpcShellyCover(ShellyRpcAttributeEntity, CoverEntity): self._id, slat_pos=kwargs[ATTR_TILT_POSITION] ) + @rpc_call + async def async_set_cover_position_and_tilt(self, **kwargs: Any) -> None: + """Move the cover and its tilt to a specific position in a single call.""" + await self.coordinator.device.cover_set_position( + self._id, pos=kwargs[ATTR_POSITION], slat_pos=kwargs[ATTR_TILT_POSITION] + ) + @rpc_call @override async def async_stop_cover_tilt(self, **kwargs: Any) -> None: diff --git a/homeassistant/components/shelly/icons.json b/homeassistant/components/shelly/icons.json index 9411a0b6745e..abba23959b83 100644 --- a/homeassistant/components/shelly/icons.json +++ b/homeassistant/components/shelly/icons.json @@ -120,6 +120,9 @@ "get_kvs_value": { "service": "mdi:import" }, + "set_cover_position_and_tilt": { + "service": "mdi:window-shutter-cog" + }, "set_kvs_value": { "service": "mdi:export" } diff --git a/homeassistant/components/shelly/services.py b/homeassistant/components/shelly/services.py index 6a804ca9e04d..03362ae5e2e4 100644 --- a/homeassistant/components/shelly/services.py +++ b/homeassistant/components/shelly/services.py @@ -6,6 +6,12 @@ from aioshelly.const import RPC_GENERATIONS from aioshelly.exceptions import DeviceConnectionError, RpcCallError import probatio +from homeassistant.components.cover import ( + ATTR_POSITION, + ATTR_TILT_POSITION, + DOMAIN as COVER_DOMAIN, + CoverEntityFeature, +) from homeassistant.const import ATTR_DEVICE_ID from homeassistant.core import ( HomeAssistant, @@ -16,7 +22,11 @@ from homeassistant.core import ( ) from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.service import async_get_device_and_config_entry +from homeassistant.helpers.service import ( + async_get_device_and_config_entry, + async_register_platform_entity_service, +) +from homeassistant.helpers.typing import VolDictType from homeassistant.util.json import JsonValueType from .const import ATTR_KEY, ATTR_VALUE, CONF_SLEEP_PERIOD, DOMAIN @@ -25,6 +35,7 @@ from .utils import get_device_entry_gen SERVICE_GET_KVS_VALUE = "get_kvs_value" SERVICE_SET_KVS_VALUE = "set_kvs_value" +SERVICE_SET_COVER_POSITION_AND_TILT = "set_cover_position_and_tilt" SERVICE_GET_KVS_VALUE_SCHEMA = probatio.Schema( { probatio.Required(ATTR_DEVICE_ID): cv.string, @@ -40,6 +51,14 @@ SERVICE_SET_KVS_VALUE_SCHEMA = probatio.Schema( ), } ) +SERVICE_SET_COVER_POSITION_AND_TILT_SCHEMA: VolDictType = { + probatio.Required(ATTR_POSITION): probatio.All( + probatio.Coerce(int), probatio.Range(min=0, max=100) + ), + probatio.Required(ATTR_TILT_POSITION): probatio.All( + probatio.Coerce(int), probatio.Range(min=0, max=100) + ), +} @callback @@ -143,3 +162,15 @@ def async_setup_services(hass: HomeAssistant) -> None: schema=schema, supports_response=response, ) + + async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_COVER_POSITION_AND_TILT, + entity_domain=COVER_DOMAIN, + schema=SERVICE_SET_COVER_POSITION_AND_TILT_SCHEMA, + func="async_set_cover_position_and_tilt", + required_features=[ + CoverEntityFeature.SET_POSITION | CoverEntityFeature.SET_TILT_POSITION + ], + ) diff --git a/homeassistant/components/shelly/services.yaml b/homeassistant/components/shelly/services.yaml index b559e17d9c48..49798b8cf60a 100644 --- a/homeassistant/components/shelly/services.yaml +++ b/homeassistant/components/shelly/services.yaml @@ -25,3 +25,27 @@ set_kvs_value: required: true selector: object: + +set_cover_position_and_tilt: + target: + entity: + integration: shelly + domain: cover + supported_features: + - - cover.CoverEntityFeature.SET_POSITION + - cover.CoverEntityFeature.SET_TILT_POSITION + fields: + position: + required: true + selector: + number: + min: 0 + max: 100 + unit_of_measurement: "%" + tilt_position: + required: true + selector: + number: + min: 0 + max: 100 + unit_of_measurement: "%" diff --git a/homeassistant/components/shelly/strings.json b/homeassistant/components/shelly/strings.json index e3ddf07f6e53..f686f1a7193e 100644 --- a/homeassistant/components/shelly/strings.json +++ b/homeassistant/components/shelly/strings.json @@ -872,6 +872,20 @@ }, "name": "Get Shelly KVS value" }, + "set_cover_position_and_tilt": { + "description": "Moves a cover and its tilt to the target positions with a single command.", + "fields": { + "position": { + "description": "Target position of the cover. 0 means closed, 100 means fully open.", + "name": "Position" + }, + "tilt_position": { + "description": "Target tilt position of the cover. 0 means closed, 100 means fully open.", + "name": "Tilt position" + } + }, + "name": "Set cover position and tilt" + }, "set_kvs_value": { "description": "Sets a value in a Shelly device's Key-Value Storage.", "fields": { diff --git a/tests/components/shelly/test_cover.py b/tests/components/shelly/test_cover.py index 7df70403ae14..45e15a0e99be 100644 --- a/tests/components/shelly/test_cover.py +++ b/tests/components/shelly/test_cover.py @@ -22,7 +22,8 @@ from homeassistant.components.cover import ( SERVICE_STOP_COVER_TILT, CoverState, ) -from homeassistant.components.shelly.const import RPC_COVER_UPDATE_TIME_SEC +from homeassistant.components.shelly.const import DOMAIN, RPC_COVER_UPDATE_TIME_SEC +from homeassistant.components.shelly.services import SERVICE_SET_COVER_POSITION_AND_TILT from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, @@ -31,6 +32,7 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ServiceNotSupported from homeassistant.helpers.entity_registry import EntityRegistry from . import ( @@ -485,3 +487,53 @@ async def test_rpc_not_initialized_update( mock_rpc_device.update_cover_status.assert_not_called() assert (state := hass.states.get(entity_id)) assert state.state == STATE_UNAVAILABLE + + +async def test_rpc_cover_set_position_and_tilt( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Test RPC cover set position and tilt sends a single command.""" + entity_id = "cover.test_name_test_cover_0" + + config = deepcopy(mock_rpc_device.config) + config["cover:0"]["slat"] = {"enable": True} + monkeypatch.setattr(mock_rpc_device, "config", config) + + status = deepcopy(mock_rpc_device.status) + status["cover:0"]["slat_pos"] = 0 + monkeypatch.setattr(mock_rpc_device, "status", status) + + await init_integration(hass, 3) + + await hass.services.async_call( + DOMAIN, + SERVICE_SET_COVER_POSITION_AND_TILT, + {ATTR_ENTITY_ID: entity_id, ATTR_POSITION: 100, ATTR_TILT_POSITION: 100}, + blocking=True, + ) + + mock_rpc_device.cover_set_position.assert_called_once_with(0, pos=100, slat_pos=100) + + +async def test_rpc_cover_set_position_and_tilt_not_supported( + hass: HomeAssistant, + mock_rpc_device: Mock, +) -> None: + """Test RPC cover set position and tilt on a cover without tilt.""" + await init_integration(hass, 3) + + with pytest.raises(ServiceNotSupported): + await hass.services.async_call( + DOMAIN, + SERVICE_SET_COVER_POSITION_AND_TILT, + { + ATTR_ENTITY_ID: "cover.test_name_test_cover_0", + ATTR_POSITION: 100, + ATTR_TILT_POSITION: 100, + }, + blocking=True, + ) + + mock_rpc_device.cover_set_position.assert_not_called()