From a66e9a1a2cc7abdb6b5b24962149e15b1531db01 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 13 Jun 2025 18:08:59 +0200 Subject: [PATCH] Simplify reolink service actions (#146751) --- homeassistant/components/reolink/services.py | 79 ++++++++++---------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/reolink/services.py b/homeassistant/components/reolink/services.py index d170aa32379..352ebb4ef19 100644 --- a/homeassistant/components/reolink/services.py +++ b/homeassistant/components/reolink/services.py @@ -19,51 +19,54 @@ from .util import get_device_uid_and_ch, raise_translated_error ATTR_RINGTONE = "ringtone" +@raise_translated_error +async def _async_play_chime(service_call: ServiceCall) -> None: + """Play a ringtone.""" + service_data = service_call.data + device_registry = dr.async_get(service_call.hass) + + for device_id in service_data[ATTR_DEVICE_ID]: + config_entry = None + device = device_registry.async_get(device_id) + if device is not None: + for entry_id in device.config_entries: + config_entry = service_call.hass.config_entries.async_get_entry( + entry_id + ) + if config_entry is not None and config_entry.domain == DOMAIN: + break + if ( + config_entry is None + or device is None + or config_entry.state != ConfigEntryState.LOADED + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="service_entry_ex", + translation_placeholders={"service_name": "play_chime"}, + ) + host: ReolinkHost = config_entry.runtime_data.host + (device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host) + chime: Chime | None = host.api.chime(chime_id) + if not is_chime or chime is None: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="service_not_chime", + translation_placeholders={"device_name": str(device.name)}, + ) + + ringtone = service_data[ATTR_RINGTONE] + await chime.play(ChimeToneEnum[ringtone].value) + + @callback def async_setup_services(hass: HomeAssistant) -> None: """Set up Reolink services.""" - @raise_translated_error - async def async_play_chime(service_call: ServiceCall) -> None: - """Play a ringtone.""" - service_data = service_call.data - device_registry = dr.async_get(hass) - - for device_id in service_data[ATTR_DEVICE_ID]: - config_entry = None - device = device_registry.async_get(device_id) - if device is not None: - for entry_id in device.config_entries: - config_entry = hass.config_entries.async_get_entry(entry_id) - if config_entry is not None and config_entry.domain == DOMAIN: - break - if ( - config_entry is None - or device is None - or config_entry.state != ConfigEntryState.LOADED - ): - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="service_entry_ex", - translation_placeholders={"service_name": "play_chime"}, - ) - host: ReolinkHost = config_entry.runtime_data.host - (device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host) - chime: Chime | None = host.api.chime(chime_id) - if not is_chime or chime is None: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="service_not_chime", - translation_placeholders={"device_name": str(device.name)}, - ) - - ringtone = service_data[ATTR_RINGTONE] - await chime.play(ChimeToneEnum[ringtone].value) - hass.services.async_register( DOMAIN, "play_chime", - async_play_chime, + _async_play_chime, schema=vol.Schema( { vol.Required(ATTR_DEVICE_ID): list[str],