1 """Services for ScreenLogic integration."""
4 from typing
import cast
6 from screenlogicpy
import ScreenLogicError
7 from screenlogicpy.device_const.system
import EQUIPMENT_FLAG
8 import voluptuous
as vol
22 SERVICE_SET_COLOR_MODE,
23 SERVICE_START_SUPER_CHLORINATION,
24 SERVICE_STOP_SUPER_CHLORINATION,
25 SUPPORTED_COLOR_MODES,
27 from .coordinator
import ScreenlogicDataUpdateCoordinator
28 from .types
import ScreenLogicConfigEntry
30 _LOGGER = logging.getLogger(__name__)
32 BASE_SERVICE_SCHEMA = vol.Schema(
34 vol.Required(ATTR_CONFIG_ENTRY): selector.ConfigEntrySelector(
36 "integration": DOMAIN,
42 SET_COLOR_MODE_SCHEMA = BASE_SERVICE_SCHEMA.extend(
44 vol.Required(ATTR_COLOR_MODE): vol.In(SUPPORTED_COLOR_MODES),
48 TURN_ON_SUPER_CHLOR_SCHEMA = BASE_SERVICE_SCHEMA.extend(
50 vol.Optional(ATTR_RUNTIME, default=24): vol.All(
51 vol.Coerce(int), vol.Clamp(min=MIN_RUNTIME, max=MAX_RUNTIME)
59 """Set up services for the ScreenLogic integration."""
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:
68 ScreenLogicConfigEntry |
None,
69 hass.config_entries.async_get_entry(entry_id),
73 f
"Failed to call service '{service_call.service}'. Config entry "
74 f
"'{entry_id}' not found"
76 if not config_entry.domain == DOMAIN:
78 f
"Failed to call service '{service_call.service}'. Config entry "
79 f
"'{entry_id}' is not a {DOMAIN} config"
81 if not config_entry.state == ConfigEntryState.LOADED:
83 f
"Failed to call service '{service_call.service}'. Config entry "
84 f
"'{entry_id}' not loaded"
86 coordinators.append(config_entry.runtime_data)
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):
95 "Service %s called on %s with mode %s",
96 SERVICE_SET_COLOR_MODE,
97 coordinator.gateway.name,
101 await coordinator.gateway.async_set_color_lights(color_num)
103 await coordinator.async_request_refresh()
104 except ScreenLogicError
as error:
107 async
def async_set_super_chlor(
108 service_call: ServiceCall,
110 runtime: int |
None =
None,
112 coordinator: ScreenlogicDataUpdateCoordinator
113 for coordinator
in await get_coordinators(service_call):
114 if EQUIPMENT_FLAG.CHLORINATOR
not in coordinator.gateway.equipment_flags:
116 f
"Equipment configuration for {coordinator.gateway.name} does not"
117 f
" support {service_call.service}"
119 rt_log = f
" with runtime {runtime}" if runtime
else ""
121 "Service %s called on %s%s",
122 service_call.service,
123 coordinator.gateway.name,
127 await coordinator.gateway.async_set_scg_config(
128 super_chlor_timer=runtime, super_chlorinate=is_on
131 await coordinator.async_request_refresh()
132 except ScreenLogicError
as error:
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)
139 async
def async_stop_super_chlor(service_call: ServiceCall) ->
None:
140 await async_set_super_chlor(service_call,
False)
142 hass.services.async_register(
143 DOMAIN, SERVICE_SET_COLOR_MODE, async_set_color_mode, SET_COLOR_MODE_SCHEMA
146 hass.services.async_register(
148 SERVICE_START_SUPER_CHLORINATION,
149 async_start_super_chlor,
150 TURN_ON_SUPER_CHLOR_SCHEMA,
153 hass.services.async_register(
155 SERVICE_STOP_SUPER_CHLORINATION,
156 async_stop_super_chlor,
def async_load_screenlogic_services(HomeAssistant hass)