Skip to content

Commit 2991726

Browse files
authored
Simplify screenlogic service actions (home-assistant#146609)
1 parent c34596e commit 2991726

File tree

2 files changed

+91
-86
lines changed

2 files changed

+91
-86
lines changed

homeassistant/components/screenlogic/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .const import DOMAIN
1919
from .coordinator import ScreenlogicDataUpdateCoordinator, async_get_connect_info
2020
from .data import ENTITY_MIGRATIONS
21-
from .services import async_load_screenlogic_services
21+
from .services import async_setup_services
2222
from .util import generate_unique_id
2323

2424
type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]
@@ -48,7 +48,7 @@
4848
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4949
"""Set up Screenlogic."""
5050

51-
async_load_screenlogic_services(hass)
51+
async_setup_services(hass)
5252

5353
return True
5454

homeassistant/components/screenlogic/services.py

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -54,105 +54,110 @@
5454
)
5555

5656

57-
@callback
58-
def async_load_screenlogic_services(hass: HomeAssistant):
59-
"""Set up services for the ScreenLogic integration."""
60-
61-
async def get_coordinators(
62-
service_call: ServiceCall,
63-
) -> list[ScreenlogicDataUpdateCoordinator]:
64-
entry_ids = {service_call.data[ATTR_CONFIG_ENTRY]}
65-
coordinators: list[ScreenlogicDataUpdateCoordinator] = []
66-
for entry_id in entry_ids:
67-
config_entry = cast(
68-
ScreenLogicConfigEntry | None,
69-
hass.config_entries.async_get_entry(entry_id),
57+
async def _get_coordinators(
58+
service_call: ServiceCall,
59+
) -> list[ScreenlogicDataUpdateCoordinator]:
60+
entry_ids = {service_call.data[ATTR_CONFIG_ENTRY]}
61+
coordinators: list[ScreenlogicDataUpdateCoordinator] = []
62+
for entry_id in entry_ids:
63+
config_entry = cast(
64+
ScreenLogicConfigEntry | None,
65+
service_call.hass.config_entries.async_get_entry(entry_id),
66+
)
67+
if not config_entry:
68+
raise ServiceValidationError(
69+
f"Failed to call service '{service_call.service}'. Config entry "
70+
f"'{entry_id}' not found"
71+
)
72+
if not config_entry.domain == DOMAIN:
73+
raise ServiceValidationError(
74+
f"Failed to call service '{service_call.service}'. Config entry "
75+
f"'{entry_id}' is not a {DOMAIN} config"
7076
)
71-
if not config_entry:
72-
raise ServiceValidationError(
73-
f"Failed to call service '{service_call.service}'. Config entry "
74-
f"'{entry_id}' not found"
75-
)
76-
if not config_entry.domain == DOMAIN:
77-
raise ServiceValidationError(
78-
f"Failed to call service '{service_call.service}'. Config entry "
79-
f"'{entry_id}' is not a {DOMAIN} config"
80-
)
81-
if not config_entry.state == ConfigEntryState.LOADED:
82-
raise ServiceValidationError(
83-
f"Failed to call service '{service_call.service}'. Config entry "
84-
f"'{entry_id}' not loaded"
85-
)
86-
coordinators.append(config_entry.runtime_data)
87-
88-
return coordinators
89-
90-
async def async_set_color_mode(service_call: ServiceCall) -> None:
91-
color_num = SUPPORTED_COLOR_MODES[service_call.data[ATTR_COLOR_MODE]]
92-
coordinator: ScreenlogicDataUpdateCoordinator
93-
for coordinator in await get_coordinators(service_call):
94-
_LOGGER.debug(
95-
"Service %s called on %s with mode %s",
96-
SERVICE_SET_COLOR_MODE,
97-
coordinator.gateway.name,
98-
color_num,
77+
if not config_entry.state == ConfigEntryState.LOADED:
78+
raise ServiceValidationError(
79+
f"Failed to call service '{service_call.service}'. Config entry "
80+
f"'{entry_id}' not loaded"
9981
)
100-
try:
101-
await coordinator.gateway.async_set_color_lights(color_num)
102-
# Debounced refresh to catch any secondary changes in the device
103-
await coordinator.async_request_refresh()
104-
except ScreenLogicError as error:
105-
raise HomeAssistantError(error) from error
106-
107-
async def async_set_super_chlor(
108-
service_call: ServiceCall,
109-
is_on: bool,
110-
runtime: int | None = None,
111-
) -> None:
112-
coordinator: ScreenlogicDataUpdateCoordinator
113-
for coordinator in await get_coordinators(service_call):
114-
if EQUIPMENT_FLAG.CHLORINATOR not in coordinator.gateway.equipment_flags:
115-
raise ServiceValidationError(
116-
f"Equipment configuration for {coordinator.gateway.name} does not"
117-
f" support {service_call.service}"
118-
)
119-
rt_log = f" with runtime {runtime}" if runtime else ""
120-
_LOGGER.debug(
121-
"Service %s called on %s%s",
122-
service_call.service,
123-
coordinator.gateway.name,
124-
rt_log,
82+
coordinators.append(config_entry.runtime_data)
83+
84+
return coordinators
85+
86+
87+
async def _async_set_color_mode(service_call: ServiceCall) -> None:
88+
color_num = SUPPORTED_COLOR_MODES[service_call.data[ATTR_COLOR_MODE]]
89+
coordinator: ScreenlogicDataUpdateCoordinator
90+
for coordinator in await _get_coordinators(service_call):
91+
_LOGGER.debug(
92+
"Service %s called on %s with mode %s",
93+
SERVICE_SET_COLOR_MODE,
94+
coordinator.gateway.name,
95+
color_num,
96+
)
97+
try:
98+
await coordinator.gateway.async_set_color_lights(color_num)
99+
# Debounced refresh to catch any secondary changes in the device
100+
await coordinator.async_request_refresh()
101+
except ScreenLogicError as error:
102+
raise HomeAssistantError(error) from error
103+
104+
105+
async def _async_set_super_chlor(
106+
service_call: ServiceCall,
107+
is_on: bool,
108+
runtime: int | None = None,
109+
) -> None:
110+
coordinator: ScreenlogicDataUpdateCoordinator
111+
for coordinator in await _get_coordinators(service_call):
112+
if EQUIPMENT_FLAG.CHLORINATOR not in coordinator.gateway.equipment_flags:
113+
raise ServiceValidationError(
114+
f"Equipment configuration for {coordinator.gateway.name} does not"
115+
f" support {service_call.service}"
125116
)
126-
try:
127-
await coordinator.gateway.async_set_scg_config(
128-
super_chlor_timer=runtime, super_chlorinate=is_on
129-
)
130-
# Debounced refresh to catch any secondary changes in the device
131-
await coordinator.async_request_refresh()
132-
except ScreenLogicError as error:
133-
raise HomeAssistantError(error) from error
134-
135-
async def async_start_super_chlor(service_call: ServiceCall) -> None:
136-
runtime = service_call.data[ATTR_RUNTIME]
137-
await async_set_super_chlor(service_call, True, runtime)
138-
139-
async def async_stop_super_chlor(service_call: ServiceCall) -> None:
140-
await async_set_super_chlor(service_call, False)
117+
rt_log = f" with runtime {runtime}" if runtime else ""
118+
_LOGGER.debug(
119+
"Service %s called on %s%s",
120+
service_call.service,
121+
coordinator.gateway.name,
122+
rt_log,
123+
)
124+
try:
125+
await coordinator.gateway.async_set_scg_config(
126+
super_chlor_timer=runtime, super_chlorinate=is_on
127+
)
128+
# Debounced refresh to catch any secondary changes in the device
129+
await coordinator.async_request_refresh()
130+
except ScreenLogicError as error:
131+
raise HomeAssistantError(error) from error
132+
133+
134+
async def _async_start_super_chlor(service_call: ServiceCall) -> None:
135+
runtime = service_call.data[ATTR_RUNTIME]
136+
await _async_set_super_chlor(service_call, True, runtime)
137+
138+
139+
async def _async_stop_super_chlor(service_call: ServiceCall) -> None:
140+
await _async_set_super_chlor(service_call, False)
141+
142+
143+
@callback
144+
def async_setup_services(hass: HomeAssistant):
145+
"""Set up services for the ScreenLogic integration."""
141146

142147
hass.services.async_register(
143-
DOMAIN, SERVICE_SET_COLOR_MODE, async_set_color_mode, SET_COLOR_MODE_SCHEMA
148+
DOMAIN, SERVICE_SET_COLOR_MODE, _async_set_color_mode, SET_COLOR_MODE_SCHEMA
144149
)
145150

146151
hass.services.async_register(
147152
DOMAIN,
148153
SERVICE_START_SUPER_CHLORINATION,
149-
async_start_super_chlor,
154+
_async_start_super_chlor,
150155
TURN_ON_SUPER_CHLOR_SCHEMA,
151156
)
152157

153158
hass.services.async_register(
154159
DOMAIN,
155160
SERVICE_STOP_SUPER_CHLORINATION,
156-
async_stop_super_chlor,
161+
_async_stop_super_chlor,
157162
BASE_SERVICE_SCHEMA,
158163
)

0 commit comments

Comments
 (0)