3 import voluptuous
as vol
4 from yolink.client_request
import ClientRequest
20 SERVICE_PLAY_ON_SPEAKER_HUB =
"play_on_speaker_hub"
24 """Register services for YoLink integration."""
26 async
def handle_speaker_hub_play_call(service_call: ServiceCall) ->
None:
27 """Handle Speaker Hub audio play call."""
28 service_data = service_call.data
29 device_registry = dr.async_get(hass)
30 device_entry = device_registry.async_get(service_data[ATTR_TARGET_DEVICE])
31 if device_entry
is not None:
32 for entry_id
in device_entry.config_entries:
33 if (entry := hass.config_entries.async_get_entry(entry_id))
is None:
35 if entry.domain == DOMAIN:
37 if entry
is None or entry.state == ConfigEntryState.NOT_LOADED:
39 translation_domain=DOMAIN,
40 translation_key=
"invalid_config_entry",
42 home_store = hass.data[DOMAIN][entry.entry_id]
43 for identifier
in device_entry.identifiers:
45 device_coordinator := home_store.device_coordinators.get(
49 tone_param = service_data[ATTR_TONE].capitalize()
50 play_request = ClientRequest(
53 ATTR_TONE: tone_param,
54 ATTR_TEXT_MESSAGE: service_data[ATTR_TEXT_MESSAGE],
55 ATTR_VOLUME: service_data[ATTR_VOLUME],
56 ATTR_REPEAT: service_data[ATTR_REPEAT],
59 await device_coordinator.device.call_device(play_request)
61 hass.services.async_register(
63 service=SERVICE_PLAY_ON_SPEAKER_HUB,
66 vol.Required(ATTR_TARGET_DEVICE): cv.string,
67 vol.Required(ATTR_TONE): cv.string,
68 vol.Required(ATTR_TEXT_MESSAGE): cv.string,
69 vol.Required(ATTR_VOLUME): vol.All(
70 vol.Coerce(int), vol.Range(min=0, max=15)
72 vol.Optional(ATTR_REPEAT, default=0): vol.All(
73 vol.Coerce(int), vol.Range(min=0, max=10)
77 service_func=handle_speaker_hub_play_call,
None async_register_services(HomeAssistant hass)