diff --git a/supervisor/addons/addon.py b/supervisor/addons/addon.py index 212267d93..8a3ade57c 100644 --- a/supervisor/addons/addon.py +++ b/supervisor/addons/addon.py @@ -33,8 +33,6 @@ from ..const import ( ATTR_AUDIO_OUTPUT, ATTR_AUTO_UPDATE, ATTR_BOOT, - ATTR_DATA, - ATTR_EVENT, ATTR_IMAGE, ATTR_INGRESS_ENTRY, ATTR_INGRESS_PANEL, @@ -50,7 +48,6 @@ from ..const import ( ATTR_SYSTEM, ATTR_SYSTEM_MANAGED, ATTR_SYSTEM_MANAGED_CONFIG_ENTRY, - ATTR_TYPE, ATTR_USER, ATTR_UUID, ATTR_VERSION, @@ -79,7 +76,7 @@ from ..exceptions import ( HostAppArmorError, ) from ..hardware.data import Device -from ..homeassistant.const import WSEvent, WSType +from ..homeassistant.const import WSEvent from ..jobs.const import JobExecutionLimit from ..jobs.decorator import Job from ..resolution.const import ContextType, IssueType, UnhealthyReason @@ -196,15 +193,12 @@ class Addon(AddonModel): ): self.sys_resolution.dismiss_issue(self.device_access_missing_issue) - self.sys_homeassistant.websocket.send_message( + self.sys_homeassistant.websocket.supervisor_event_custom( + WSEvent.ADDON, { - ATTR_TYPE: WSType.SUPERVISOR_EVENT, - ATTR_DATA: { - ATTR_EVENT: WSEvent.ADDON, - ATTR_SLUG: self.slug, - ATTR_STATE: new_state, - }, - } + ATTR_SLUG: self.slug, + ATTR_STATE: new_state, + }, ) @property diff --git a/supervisor/exceptions.py b/supervisor/exceptions.py index 560a53cf0..7abfa9365 100644 --- a/supervisor/exceptions.py +++ b/supervisor/exceptions.py @@ -84,10 +84,6 @@ class HomeAssistantWSError(HomeAssistantAPIError): """Home Assistant websocket error.""" -class HomeAssistantWSNotSupported(HomeAssistantWSError): - """Raise when WebSockets are not supported.""" - - class HomeAssistantWSConnectionError(HomeAssistantWSError): """Raise when the WebSocket connection has an error.""" diff --git a/supervisor/homeassistant/websocket.py b/supervisor/homeassistant/websocket.py index b2c5e3478..75d2f3527 100644 --- a/supervisor/homeassistant/websocket.py +++ b/supervisor/homeassistant/websocket.py @@ -25,7 +25,6 @@ from ..exceptions import ( HomeAssistantAPIError, HomeAssistantWSConnectionError, HomeAssistantWSError, - HomeAssistantWSNotSupported, ) from ..utils.json import json_dumps from .const import CLOSING_STATES, WSEvent, WSType @@ -254,7 +253,7 @@ class HomeAssistantWebSocket(CoreSysAttributes): ) async def async_send_message(self, message: dict[str, Any]) -> None: - """Send a command with the WS client.""" + """Send a message with the WS client.""" # Only commands allowed during startup as those tell Home Assistant to do something. # Messages may cause clients to make follow-up API calls so those wait. if self.sys_core.state in STARTING_STATES: @@ -288,67 +287,67 @@ class HomeAssistantWebSocket(CoreSysAttributes): raise return None - async def async_supervisor_update_event( - self, - key: str, - data: dict[str, Any] | None = None, - ) -> None: - """Send a supervisor/event command.""" - try: - await self.async_send_message( - { - ATTR_TYPE: WSType.SUPERVISOR_EVENT, - ATTR_DATA: { - ATTR_EVENT: WSEvent.SUPERVISOR_UPDATE, - ATTR_UPDATE_KEY: key, - ATTR_DATA: data or {}, - }, - } - ) - except HomeAssistantWSNotSupported: - pass - except HomeAssistantWSError as err: - _LOGGER.error("Could not send message to Home Assistant due to %s", err) - - def supervisor_update_event( - self, - key: str, - data: dict[str, Any] | None = None, - ) -> None: - """Send a supervisor/event command.""" - if self.sys_core.state in CLOSING_STATES: - return - self.sys_create_task(self.async_supervisor_update_event(key, data)) - def send_message(self, message: dict[str, Any]) -> None: - """Send a supervisor/event command.""" + """Send a supervisor/event message.""" if self.sys_core.state in CLOSING_STATES: return self.sys_create_task(self.async_send_message(message)) - async def async_supervisor_event( - self, event: WSEvent, data: dict[str, Any] | None = None + async def async_supervisor_event_custom( + self, event: WSEvent, extra_data: dict[str, Any] | None = None ) -> None: - """Send a supervisor/event command to Home Assistant.""" + """Send a supervisor/event message to Home Assistant with custom data.""" try: await self.async_send_message( { ATTR_TYPE: WSType.SUPERVISOR_EVENT, ATTR_DATA: { ATTR_EVENT: event, - ATTR_DATA: data or {}, + **(extra_data or {}), }, } ) - except HomeAssistantWSNotSupported: - pass except HomeAssistantWSError as err: _LOGGER.error("Could not send message to Home Assistant due to %s", err) + def supervisor_event_custom( + self, event: WSEvent, extra_data: dict[str, Any] | None = None + ) -> None: + """Send a supervisor/event message to Home Assistant with custom data.""" + if self.sys_core.state in CLOSING_STATES: + return + self.sys_create_task(self.async_supervisor_event_custom(event, extra_data)) + def supervisor_event( self, event: WSEvent, data: dict[str, Any] | None = None ) -> None: - """Send a supervisor/event command to Home Assistant.""" + """Send a supervisor/event message to Home Assistant.""" if self.sys_core.state in CLOSING_STATES: return - self.sys_create_task(self.async_supervisor_event(event, data)) + self.sys_create_task( + self.async_supervisor_event_custom(event, {ATTR_DATA: data or {}}) + ) + + async def async_supervisor_update_event( + self, + key: str, + data: dict[str, Any] | None = None, + ) -> None: + """Send an update supervisor/event message.""" + await self.async_supervisor_event_custom( + WSEvent.SUPERVISOR_UPDATE, + { + ATTR_UPDATE_KEY: key, + ATTR_DATA: data or {}, + }, + ) + + def supervisor_update_event( + self, + key: str, + data: dict[str, Any] | None = None, + ) -> None: + """Send an update supervisor/event message.""" + if self.sys_core.state in CLOSING_STATES: + return + self.sys_create_task(self.async_supervisor_update_event(key, data)) diff --git a/tests/homeassistant/test_websocket.py b/tests/homeassistant/test_websocket.py index ca1699163..8573b9c9c 100644 --- a/tests/homeassistant/test_websocket.py +++ b/tests/homeassistant/test_websocket.py @@ -84,3 +84,11 @@ async def test_send_message_during_startup(coresys: CoreSys, ha_ws_client: Async "data": {"state": "running"}, }, } + + ha_ws_client.reset_mock() + await coresys.core.set_state(CoreState.SHUTDOWN) + + await coresys.homeassistant.websocket.async_supervisor_update_event( + "test", {"lorem": "ipsum"} + ) + ha_ws_client.async_send_command.assert_not_called()