Skip to content

Commit 4a8faad

Browse files
authored
Simplify fully_kiosk service actions (home-assistant#146509)
1 parent ba69301 commit 4a8faad

File tree

2 files changed

+66
-64
lines changed

2 files changed

+66
-64
lines changed

homeassistant/components/fully_kiosk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2828
"""Set up Fully Kiosk Browser."""
2929

30-
await async_setup_services(hass)
30+
async_setup_services(hass)
3131

3232
return True
3333

homeassistant/components/fully_kiosk/services.py

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
88
from homeassistant.const import ATTR_DEVICE_ID
9-
from homeassistant.core import HomeAssistant, ServiceCall
9+
from homeassistant.core import HomeAssistant, ServiceCall, callback
1010
from homeassistant.exceptions import HomeAssistantError
1111
from homeassistant.helpers import config_validation as cv, device_registry as dr
1212

@@ -23,71 +23,73 @@
2323
from .coordinator import FullyKioskDataUpdateCoordinator
2424

2525

26-
async def async_setup_services(hass: HomeAssistant) -> None:
27-
"""Set up the services for the Fully Kiosk Browser integration."""
26+
async def _collect_coordinators(
27+
call: ServiceCall,
28+
) -> list[FullyKioskDataUpdateCoordinator]:
29+
device_ids: list[str] = call.data[ATTR_DEVICE_ID]
30+
config_entries = list[ConfigEntry]()
31+
registry = dr.async_get(call.hass)
32+
for target in device_ids:
33+
device = registry.async_get(target)
34+
if device:
35+
device_entries = list[ConfigEntry]()
36+
for entry_id in device.config_entries:
37+
entry = call.hass.config_entries.async_get_entry(entry_id)
38+
if entry and entry.domain == DOMAIN:
39+
device_entries.append(entry)
40+
if not device_entries:
41+
raise HomeAssistantError(f"Device '{target}' is not a {DOMAIN} device")
42+
config_entries.extend(device_entries)
43+
else:
44+
raise HomeAssistantError(f"Device '{target}' not found in device registry")
45+
coordinators = list[FullyKioskDataUpdateCoordinator]()
46+
for config_entry in config_entries:
47+
if config_entry.state != ConfigEntryState.LOADED:
48+
raise HomeAssistantError(f"{config_entry.title} is not loaded")
49+
coordinators.append(config_entry.runtime_data)
50+
return coordinators
2851

29-
async def collect_coordinators(
30-
device_ids: list[str],
31-
) -> list[FullyKioskDataUpdateCoordinator]:
32-
config_entries = list[ConfigEntry]()
33-
registry = dr.async_get(hass)
34-
for target in device_ids:
35-
device = registry.async_get(target)
36-
if device:
37-
device_entries = list[ConfigEntry]()
38-
for entry_id in device.config_entries:
39-
entry = hass.config_entries.async_get_entry(entry_id)
40-
if entry and entry.domain == DOMAIN:
41-
device_entries.append(entry)
42-
if not device_entries:
43-
raise HomeAssistantError(
44-
f"Device '{target}' is not a {DOMAIN} device"
45-
)
46-
config_entries.extend(device_entries)
47-
else:
48-
raise HomeAssistantError(
49-
f"Device '{target}' not found in device registry"
50-
)
51-
coordinators = list[FullyKioskDataUpdateCoordinator]()
52-
for config_entry in config_entries:
53-
if config_entry.state != ConfigEntryState.LOADED:
54-
raise HomeAssistantError(f"{config_entry.title} is not loaded")
55-
coordinators.append(config_entry.runtime_data)
56-
return coordinators
57-
58-
async def async_load_url(call: ServiceCall) -> None:
59-
"""Load a URL on the Fully Kiosk Browser."""
60-
for coordinator in await collect_coordinators(call.data[ATTR_DEVICE_ID]):
61-
await coordinator.fully.loadUrl(call.data[ATTR_URL])
62-
63-
async def async_start_app(call: ServiceCall) -> None:
64-
"""Start an app on the device."""
65-
for coordinator in await collect_coordinators(call.data[ATTR_DEVICE_ID]):
66-
await coordinator.fully.startApplication(call.data[ATTR_APPLICATION])
67-
68-
async def async_set_config(call: ServiceCall) -> None:
69-
"""Set a Fully Kiosk Browser config value on the device."""
70-
for coordinator in await collect_coordinators(call.data[ATTR_DEVICE_ID]):
71-
key = call.data[ATTR_KEY]
72-
value = call.data[ATTR_VALUE]
73-
74-
# Fully API has different methods for setting string and bool values.
75-
# check if call.data[ATTR_VALUE] is a bool
76-
if isinstance(value, bool) or (
77-
isinstance(value, str) and value.lower() in ("true", "false")
78-
):
79-
await coordinator.fully.setConfigurationBool(key, value)
80-
else:
81-
# Convert any int values to string
82-
if isinstance(value, int):
83-
value = str(value)
84-
85-
await coordinator.fully.setConfigurationString(key, value)
52+
53+
async def _async_load_url(call: ServiceCall) -> None:
54+
"""Load a URL on the Fully Kiosk Browser."""
55+
for coordinator in await _collect_coordinators(call):
56+
await coordinator.fully.loadUrl(call.data[ATTR_URL])
57+
58+
59+
async def _async_start_app(call: ServiceCall) -> None:
60+
"""Start an app on the device."""
61+
for coordinator in await _collect_coordinators(call):
62+
await coordinator.fully.startApplication(call.data[ATTR_APPLICATION])
63+
64+
65+
async def _async_set_config(call: ServiceCall) -> None:
66+
"""Set a Fully Kiosk Browser config value on the device."""
67+
for coordinator in await _collect_coordinators(call):
68+
key = call.data[ATTR_KEY]
69+
value = call.data[ATTR_VALUE]
70+
71+
# Fully API has different methods for setting string and bool values.
72+
# check if call.data[ATTR_VALUE] is a bool
73+
if isinstance(value, bool) or (
74+
isinstance(value, str) and value.lower() in ("true", "false")
75+
):
76+
await coordinator.fully.setConfigurationBool(key, value)
77+
else:
78+
# Convert any int values to string
79+
if isinstance(value, int):
80+
value = str(value)
81+
82+
await coordinator.fully.setConfigurationString(key, value)
83+
84+
85+
@callback
86+
def async_setup_services(hass: HomeAssistant) -> None:
87+
"""Set up the services for the Fully Kiosk Browser integration."""
8688

8789
# Register all the above services
8890
service_mapping = [
89-
(async_load_url, SERVICE_LOAD_URL, ATTR_URL),
90-
(async_start_app, SERVICE_START_APPLICATION, ATTR_APPLICATION),
91+
(_async_load_url, SERVICE_LOAD_URL, ATTR_URL),
92+
(_async_start_app, SERVICE_START_APPLICATION, ATTR_APPLICATION),
9193
]
9294
for service_handler, service_name, attrib in service_mapping:
9395
hass.services.async_register(
@@ -107,7 +109,7 @@ async def async_set_config(call: ServiceCall) -> None:
107109
hass.services.async_register(
108110
DOMAIN,
109111
SERVICE_SET_CONFIG,
110-
async_set_config,
112+
_async_set_config,
111113
schema=vol.Schema(
112114
vol.All(
113115
{

0 commit comments

Comments
 (0)