Skip to content

Commit a66e9a1

Browse files
authored
Simplify reolink service actions (home-assistant#146751)
1 parent d880ce6 commit a66e9a1

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

homeassistant/components/reolink/services.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,54 @@
1919
ATTR_RINGTONE = "ringtone"
2020

2121

22-
@callback
23-
def async_setup_services(hass: HomeAssistant) -> None:
24-
"""Set up Reolink services."""
22+
@raise_translated_error
23+
async def _async_play_chime(service_call: ServiceCall) -> None:
24+
"""Play a ringtone."""
25+
service_data = service_call.data
26+
device_registry = dr.async_get(service_call.hass)
2527

26-
@raise_translated_error
27-
async def async_play_chime(service_call: ServiceCall) -> None:
28-
"""Play a ringtone."""
29-
service_data = service_call.data
30-
device_registry = dr.async_get(hass)
31-
32-
for device_id in service_data[ATTR_DEVICE_ID]:
33-
config_entry = None
34-
device = device_registry.async_get(device_id)
35-
if device is not None:
36-
for entry_id in device.config_entries:
37-
config_entry = hass.config_entries.async_get_entry(entry_id)
38-
if config_entry is not None and config_entry.domain == DOMAIN:
39-
break
40-
if (
41-
config_entry is None
42-
or device is None
43-
or config_entry.state != ConfigEntryState.LOADED
44-
):
45-
raise ServiceValidationError(
46-
translation_domain=DOMAIN,
47-
translation_key="service_entry_ex",
48-
translation_placeholders={"service_name": "play_chime"},
49-
)
50-
host: ReolinkHost = config_entry.runtime_data.host
51-
(device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host)
52-
chime: Chime | None = host.api.chime(chime_id)
53-
if not is_chime or chime is None:
54-
raise ServiceValidationError(
55-
translation_domain=DOMAIN,
56-
translation_key="service_not_chime",
57-
translation_placeholders={"device_name": str(device.name)},
28+
for device_id in service_data[ATTR_DEVICE_ID]:
29+
config_entry = None
30+
device = device_registry.async_get(device_id)
31+
if device is not None:
32+
for entry_id in device.config_entries:
33+
config_entry = service_call.hass.config_entries.async_get_entry(
34+
entry_id
5835
)
36+
if config_entry is not None and config_entry.domain == DOMAIN:
37+
break
38+
if (
39+
config_entry is None
40+
or device is None
41+
or config_entry.state != ConfigEntryState.LOADED
42+
):
43+
raise ServiceValidationError(
44+
translation_domain=DOMAIN,
45+
translation_key="service_entry_ex",
46+
translation_placeholders={"service_name": "play_chime"},
47+
)
48+
host: ReolinkHost = config_entry.runtime_data.host
49+
(device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host)
50+
chime: Chime | None = host.api.chime(chime_id)
51+
if not is_chime or chime is None:
52+
raise ServiceValidationError(
53+
translation_domain=DOMAIN,
54+
translation_key="service_not_chime",
55+
translation_placeholders={"device_name": str(device.name)},
56+
)
5957

60-
ringtone = service_data[ATTR_RINGTONE]
61-
await chime.play(ChimeToneEnum[ringtone].value)
58+
ringtone = service_data[ATTR_RINGTONE]
59+
await chime.play(ChimeToneEnum[ringtone].value)
60+
61+
62+
@callback
63+
def async_setup_services(hass: HomeAssistant) -> None:
64+
"""Set up Reolink services."""
6265

6366
hass.services.async_register(
6467
DOMAIN,
6568
"play_chime",
66-
async_play_chime,
69+
_async_play_chime,
6770
schema=vol.Schema(
6871
{
6972
vol.Required(ATTR_DEVICE_ID): list[str],

0 commit comments

Comments
 (0)