|
25 | 25 | HomeAssistantAPIError, |
26 | 26 | HomeAssistantWSConnectionError, |
27 | 27 | HomeAssistantWSError, |
28 | | - HomeAssistantWSNotSupported, |
29 | 28 | ) |
30 | 29 | from ..utils.json import json_dumps |
31 | 30 | from .const import CLOSING_STATES, WSEvent, WSType |
@@ -254,7 +253,7 @@ async def load(self) -> None: |
254 | 253 | ) |
255 | 254 |
|
256 | 255 | async def async_send_message(self, message: dict[str, Any]) -> None: |
257 | | - """Send a command with the WS client.""" |
| 256 | + """Send a message with the WS client.""" |
258 | 257 | # Only commands allowed during startup as those tell Home Assistant to do something. |
259 | 258 | # Messages may cause clients to make follow-up API calls so those wait. |
260 | 259 | if self.sys_core.state in STARTING_STATES: |
@@ -288,67 +287,67 @@ async def async_send_command(self, message: dict[str, Any]) -> T | None: |
288 | 287 | raise |
289 | 288 | return None |
290 | 289 |
|
291 | | - async def async_supervisor_update_event( |
292 | | - self, |
293 | | - key: str, |
294 | | - data: dict[str, Any] | None = None, |
| 290 | + def send_message(self, message: dict[str, Any]) -> None: |
| 291 | + """Send a supervisor/event message.""" |
| 292 | + if self.sys_core.state in CLOSING_STATES: |
| 293 | + return |
| 294 | + self.sys_create_task(self.async_send_message(message)) |
| 295 | + |
| 296 | + async def async_supervisor_event_custom( |
| 297 | + self, event: WSEvent, extra_data: dict[str, Any] | None = None |
295 | 298 | ) -> None: |
296 | | - """Send a supervisor/event command.""" |
| 299 | + """Send a supervisor/event message to Home Assistant with custom data.""" |
297 | 300 | try: |
298 | 301 | await self.async_send_message( |
299 | 302 | { |
300 | 303 | ATTR_TYPE: WSType.SUPERVISOR_EVENT, |
301 | 304 | ATTR_DATA: { |
302 | | - ATTR_EVENT: WSEvent.SUPERVISOR_UPDATE, |
303 | | - ATTR_UPDATE_KEY: key, |
304 | | - ATTR_DATA: data or {}, |
| 305 | + ATTR_EVENT: event, |
| 306 | + **(extra_data or {}), |
305 | 307 | }, |
306 | 308 | } |
307 | 309 | ) |
308 | | - except HomeAssistantWSNotSupported: |
309 | | - pass |
310 | 310 | except HomeAssistantWSError as err: |
311 | 311 | _LOGGER.error("Could not send message to Home Assistant due to %s", err) |
312 | 312 |
|
313 | | - def supervisor_update_event( |
314 | | - self, |
315 | | - key: str, |
316 | | - data: dict[str, Any] | None = None, |
| 313 | + def supervisor_event_custom( |
| 314 | + self, event: WSEvent, extra_data: dict[str, Any] | None = None |
317 | 315 | ) -> None: |
318 | | - """Send a supervisor/event command.""" |
| 316 | + """Send a supervisor/event message to Home Assistant with custom data.""" |
319 | 317 | if self.sys_core.state in CLOSING_STATES: |
320 | 318 | return |
321 | | - self.sys_create_task(self.async_supervisor_update_event(key, data)) |
| 319 | + self.sys_create_task(self.async_supervisor_event_custom(event, extra_data)) |
322 | 320 |
|
323 | | - def send_message(self, message: dict[str, Any]) -> None: |
324 | | - """Send a supervisor/event command.""" |
| 321 | + def supervisor_event( |
| 322 | + self, event: WSEvent, data: dict[str, Any] | None = None |
| 323 | + ) -> None: |
| 324 | + """Send a supervisor/event message to Home Assistant.""" |
325 | 325 | if self.sys_core.state in CLOSING_STATES: |
326 | 326 | return |
327 | | - self.sys_create_task(self.async_send_message(message)) |
| 327 | + self.sys_create_task( |
| 328 | + self.async_supervisor_event_custom(event, {ATTR_DATA: data or {}}) |
| 329 | + ) |
328 | 330 |
|
329 | | - async def async_supervisor_event( |
330 | | - self, event: WSEvent, data: dict[str, Any] | None = None |
| 331 | + async def async_supervisor_update_event( |
| 332 | + self, |
| 333 | + key: str, |
| 334 | + data: dict[str, Any] | None = None, |
331 | 335 | ) -> None: |
332 | | - """Send a supervisor/event command to Home Assistant.""" |
333 | | - try: |
334 | | - await self.async_send_message( |
335 | | - { |
336 | | - ATTR_TYPE: WSType.SUPERVISOR_EVENT, |
337 | | - ATTR_DATA: { |
338 | | - ATTR_EVENT: event, |
339 | | - ATTR_DATA: data or {}, |
340 | | - }, |
341 | | - } |
342 | | - ) |
343 | | - except HomeAssistantWSNotSupported: |
344 | | - pass |
345 | | - except HomeAssistantWSError as err: |
346 | | - _LOGGER.error("Could not send message to Home Assistant due to %s", err) |
| 336 | + """Send an update supervisor/event message.""" |
| 337 | + await self.async_supervisor_event_custom( |
| 338 | + WSEvent.SUPERVISOR_UPDATE, |
| 339 | + { |
| 340 | + ATTR_UPDATE_KEY: key, |
| 341 | + ATTR_DATA: data or {}, |
| 342 | + }, |
| 343 | + ) |
347 | 344 |
|
348 | | - def supervisor_event( |
349 | | - self, event: WSEvent, data: dict[str, Any] | None = None |
| 345 | + def supervisor_update_event( |
| 346 | + self, |
| 347 | + key: str, |
| 348 | + data: dict[str, Any] | None = None, |
350 | 349 | ) -> None: |
351 | | - """Send a supervisor/event command to Home Assistant.""" |
| 350 | + """Send an update supervisor/event message.""" |
352 | 351 | if self.sys_core.state in CLOSING_STATES: |
353 | 352 | return |
354 | | - self.sys_create_task(self.async_supervisor_event(event, data)) |
| 353 | + self.sys_create_task(self.async_supervisor_update_event(key, data)) |
0 commit comments