mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 20:59:22 -04:00
Add service to set Shely RPC cover position and tilt in a single call (#185715)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
98592e86c2
commit
482734ccd1
@@ -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:
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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
|
||||
],
|
||||
)
|
||||
|
||||
@@ -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: "%"
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user