Update yolink "play on speaker hub" action to allow optional values (to match YoLink API) (#133099)

This commit is contained in:
Max R 2025-01-17 11:43:47 -05:00 committed by GitHub
parent 44b577cadb
commit 2a514ebc3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 16 deletions

View File

@ -19,6 +19,11 @@ from .const import (
SERVICE_PLAY_ON_SPEAKER_HUB = "play_on_speaker_hub" SERVICE_PLAY_ON_SPEAKER_HUB = "play_on_speaker_hub"
_SPEAKER_HUB_PLAY_CALL_OPTIONAL_ATTRS = (
(ATTR_VOLUME, lambda x: x),
(ATTR_TONE, lambda x: x.capitalize()),
)
def async_register_services(hass: HomeAssistant) -> None: def async_register_services(hass: HomeAssistant) -> None:
"""Register services for YoLink integration.""" """Register services for YoLink integration."""
@ -46,16 +51,16 @@ def async_register_services(hass: HomeAssistant) -> None:
identifier[1] identifier[1]
) )
) is not None: ) is not None:
tone_param = service_data[ATTR_TONE].capitalize() params = {
play_request = ClientRequest(
"playAudio",
{
ATTR_TONE: tone_param,
ATTR_TEXT_MESSAGE: service_data[ATTR_TEXT_MESSAGE], ATTR_TEXT_MESSAGE: service_data[ATTR_TEXT_MESSAGE],
ATTR_VOLUME: service_data[ATTR_VOLUME],
ATTR_REPEAT: service_data[ATTR_REPEAT], ATTR_REPEAT: service_data[ATTR_REPEAT],
}, }
)
for attr, transform in _SPEAKER_HUB_PLAY_CALL_OPTIONAL_ATTRS:
if attr in service_data:
params[attr] = transform(service_data[attr])
play_request = ClientRequest("playAudio", params)
await device_coordinator.device.call_device(play_request) await device_coordinator.device.call_device(play_request)
hass.services.async_register( hass.services.async_register(
@ -64,9 +69,9 @@ def async_register_services(hass: HomeAssistant) -> None:
schema=vol.Schema( schema=vol.Schema(
{ {
vol.Required(ATTR_TARGET_DEVICE): cv.string, vol.Required(ATTR_TARGET_DEVICE): cv.string,
vol.Required(ATTR_TONE): cv.string, vol.Optional(ATTR_TONE): cv.string,
vol.Required(ATTR_TEXT_MESSAGE): cv.string, vol.Required(ATTR_TEXT_MESSAGE): cv.string,
vol.Required(ATTR_VOLUME): vol.All( vol.Optional(ATTR_VOLUME): vol.All(
vol.Coerce(int), vol.Range(min=0, max=15) vol.Coerce(int), vol.Range(min=0, max=15)
), ),
vol.Optional(ATTR_REPEAT, default=0): vol.All( vol.Optional(ATTR_REPEAT, default=0): vol.All(

View File

@ -14,7 +14,6 @@ play_on_speaker_hub:
selector: selector:
text: text:
tone: tone:
required: true
default: "tip" default: "tip"
selector: selector:
select: select:
@ -25,7 +24,6 @@ play_on_speaker_hub:
- "tip" - "tip"
translation_key: speaker_tone translation_key: speaker_tone
volume: volume:
required: true
default: 8 default: 8
selector: selector:
number: number:
@ -33,7 +31,6 @@ play_on_speaker_hub:
max: 15 max: 15
step: 1 step: 1
repeat: repeat:
required: true
default: 0 default: 0
selector: selector:
number: number:

View File

@ -115,7 +115,7 @@
}, },
"volume": { "volume": {
"name": "Volume", "name": "Volume",
"description": "Speaker volume during playback." "description": "Override the speaker volume during playback of this message only."
}, },
"repeat": { "repeat": {
"name": "Repeat", "name": "Repeat",