Skip to content

Commit 122b732

Browse files
authored
Unify Supervisor event message functions (#5831)
* Unify Supervisor event message functions Unify functions which send WebSocket messages of type "supervisor/event". This deduplicates code and hopefully avoids further diversication in the future. While at it, remove unused HomeAssistantWSNotSupported exception. It seems the only place this exception is used got removed in #3317. * Test message delivery during shutdown states
1 parent 5d07dd2 commit 122b732

File tree

4 files changed

+55
-58
lines changed

4 files changed

+55
-58
lines changed

supervisor/addons/addon.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
ATTR_AUDIO_OUTPUT,
3434
ATTR_AUTO_UPDATE,
3535
ATTR_BOOT,
36-
ATTR_DATA,
37-
ATTR_EVENT,
3836
ATTR_IMAGE,
3937
ATTR_INGRESS_ENTRY,
4038
ATTR_INGRESS_PANEL,
@@ -50,7 +48,6 @@
5048
ATTR_SYSTEM,
5149
ATTR_SYSTEM_MANAGED,
5250
ATTR_SYSTEM_MANAGED_CONFIG_ENTRY,
53-
ATTR_TYPE,
5451
ATTR_USER,
5552
ATTR_UUID,
5653
ATTR_VERSION,
@@ -79,7 +76,7 @@
7976
HostAppArmorError,
8077
)
8178
from ..hardware.data import Device
82-
from ..homeassistant.const import WSEvent, WSType
79+
from ..homeassistant.const import WSEvent
8380
from ..jobs.const import JobExecutionLimit
8481
from ..jobs.decorator import Job
8582
from ..resolution.const import ContextType, IssueType, UnhealthyReason
@@ -196,15 +193,12 @@ def state(self, new_state: AddonState) -> None:
196193
):
197194
self.sys_resolution.dismiss_issue(self.device_access_missing_issue)
198195

199-
self.sys_homeassistant.websocket.send_message(
196+
self.sys_homeassistant.websocket.supervisor_event_custom(
197+
WSEvent.ADDON,
200198
{
201-
ATTR_TYPE: WSType.SUPERVISOR_EVENT,
202-
ATTR_DATA: {
203-
ATTR_EVENT: WSEvent.ADDON,
204-
ATTR_SLUG: self.slug,
205-
ATTR_STATE: new_state,
206-
},
207-
}
199+
ATTR_SLUG: self.slug,
200+
ATTR_STATE: new_state,
201+
},
208202
)
209203

210204
@property

supervisor/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ class HomeAssistantWSError(HomeAssistantAPIError):
8484
"""Home Assistant websocket error."""
8585

8686

87-
class HomeAssistantWSNotSupported(HomeAssistantWSError):
88-
"""Raise when WebSockets are not supported."""
89-
90-
9187
class HomeAssistantWSConnectionError(HomeAssistantWSError):
9288
"""Raise when the WebSocket connection has an error."""
9389

supervisor/homeassistant/websocket.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
HomeAssistantAPIError,
2626
HomeAssistantWSConnectionError,
2727
HomeAssistantWSError,
28-
HomeAssistantWSNotSupported,
2928
)
3029
from ..utils.json import json_dumps
3130
from .const import CLOSING_STATES, WSEvent, WSType
@@ -254,7 +253,7 @@ async def load(self) -> None:
254253
)
255254

256255
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."""
258257
# Only commands allowed during startup as those tell Home Assistant to do something.
259258
# Messages may cause clients to make follow-up API calls so those wait.
260259
if self.sys_core.state in STARTING_STATES:
@@ -288,67 +287,67 @@ async def async_send_command(self, message: dict[str, Any]) -> T | None:
288287
raise
289288
return None
290289

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
295298
) -> None:
296-
"""Send a supervisor/event command."""
299+
"""Send a supervisor/event message to Home Assistant with custom data."""
297300
try:
298301
await self.async_send_message(
299302
{
300303
ATTR_TYPE: WSType.SUPERVISOR_EVENT,
301304
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 {}),
305307
},
306308
}
307309
)
308-
except HomeAssistantWSNotSupported:
309-
pass
310310
except HomeAssistantWSError as err:
311311
_LOGGER.error("Could not send message to Home Assistant due to %s", err)
312312

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
317315
) -> None:
318-
"""Send a supervisor/event command."""
316+
"""Send a supervisor/event message to Home Assistant with custom data."""
319317
if self.sys_core.state in CLOSING_STATES:
320318
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))
322320

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."""
325325
if self.sys_core.state in CLOSING_STATES:
326326
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+
)
328330

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,
331335
) -> 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+
)
347344

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,
350349
) -> None:
351-
"""Send a supervisor/event command to Home Assistant."""
350+
"""Send an update supervisor/event message."""
352351
if self.sys_core.state in CLOSING_STATES:
353352
return
354-
self.sys_create_task(self.async_supervisor_event(event, data))
353+
self.sys_create_task(self.async_supervisor_update_event(key, data))

tests/homeassistant/test_websocket.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ async def test_send_message_during_startup(coresys: CoreSys, ha_ws_client: Async
8484
"data": {"state": "running"},
8585
},
8686
}
87+
88+
ha_ws_client.reset_mock()
89+
await coresys.core.set_state(CoreState.SHUTDOWN)
90+
91+
await coresys.homeassistant.websocket.async_supervisor_update_event(
92+
"test", {"lorem": "ipsum"}
93+
)
94+
ha_ws_client.async_send_command.assert_not_called()

0 commit comments

Comments
 (0)