|
54 | 54 | ) |
55 | 55 |
|
56 | 56 |
|
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" |
70 | 76 | ) |
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" |
99 | 81 | ) |
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}" |
125 | 116 | ) |
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.""" |
141 | 146 |
|
142 | 147 | 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 |
144 | 149 | ) |
145 | 150 |
|
146 | 151 | hass.services.async_register( |
147 | 152 | DOMAIN, |
148 | 153 | SERVICE_START_SUPER_CHLORINATION, |
149 | | - async_start_super_chlor, |
| 154 | + _async_start_super_chlor, |
150 | 155 | TURN_ON_SUPER_CHLOR_SCHEMA, |
151 | 156 | ) |
152 | 157 |
|
153 | 158 | hass.services.async_register( |
154 | 159 | DOMAIN, |
155 | 160 | SERVICE_STOP_SUPER_CHLORINATION, |
156 | | - async_stop_super_chlor, |
| 161 | + _async_stop_super_chlor, |
157 | 162 | BASE_SERVICE_SCHEMA, |
158 | 163 | ) |
0 commit comments