Skip to content

Commit

Permalink
refactor: replace SETUP_DRIVER_ABORT event with callback
Browse files Browse the repository at this point in the history
Use the setup handler with the new AbortDriverSetup class to notify a
driver about an aborted setup flow.
  • Loading branch information
zehnm committed Nov 7, 2023
1 parent 3fb737c commit 9990b4e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions ucapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import logging # isort:skip

from .api_definitions import ( # isort:skip # noqa: F401
AbortDriverSetup,
DeviceStates,
DriverSetupRequest,
Events,
Expand Down
23 changes: 20 additions & 3 deletions ucapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async def _process_ws_message(self, websocket, message) -> None:
else:
await self._handle_ws_request_msg(websocket, msg, req_id, msg_data)
elif kind == "event":
self._handle_ws_event_msg(msg, msg_data)
await self._handle_ws_event_msg(msg, msg_data)

async def _handle_ws_request_msg(
self, websocket, msg: str, req_id: int, msg_data: dict[str, Any] | None
Expand Down Expand Up @@ -365,7 +365,9 @@ async def _handle_ws_request_msg(
await asyncio.sleep(0.5)
await self.driver_setup_error(websocket)

def _handle_ws_event_msg(self, msg: str, _msg_data: dict[str, Any] | None) -> None:
async def _handle_ws_event_msg(
self, msg: str, msg_data: dict[str, Any] | None
) -> None:
if msg == uc.WsMsgEvents.CONNECT:
self._events.emit(uc.Events.CONNECT)
elif msg == uc.WsMsgEvents.DISCONNECT:
Expand All @@ -375,7 +377,22 @@ def _handle_ws_event_msg(self, msg: str, _msg_data: dict[str, Any] | None) -> No
elif msg == uc.WsMsgEvents.EXIT_STANDBY:
self._events.emit(uc.Events.EXIT_STANDBY)
elif msg == uc.WsMsgEvents.ABORT_DRIVER_SETUP:
self._events.emit(uc.Events.SETUP_DRIVER_ABORT)
if not self._setup_handler:
_LOG.warning(
"Received abort_driver_setup event, but no setup handler provided by the driver!" # noqa
)
return

if "error" in msg_data:
try:
error = uc.IntegrationSetupError[msg_data["error"]]
except KeyError:
error = uc.IntegrationSetupError.OTHER
await self._setup_handler(uc.AbortDriverSetup(error))
else:
_LOG.warning(
"Unsupported abort_driver_setup payload received: %s", msg_data
)

async def _authenticate(self, websocket, success: bool) -> None:
await self._send_ws_response(
Expand Down
13 changes: 12 additions & 1 deletion ucapi/api_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class Events(str, Enum):
ENTITY_ATTRIBUTES_UPDATED = "entity_attributes_updated"
SUBSCRIBE_ENTITIES = "subscribe_entities"
UNSUBSCRIBE_ENTITIES = "unsubscribe_entities"
SETUP_DRIVER_ABORT = "setup_driver_abort"
CONNECT = "connect"
DISCONNECT = "disconnect"
ENTER_STANDBY = "enter_standby"
Expand Down Expand Up @@ -147,6 +146,18 @@ class UserConfirmationResponse(SetupDriver):
confirm: bool


@dataclass
class AbortDriverSetup(SetupDriver):
"""
Abort notification.
- ``error == OTHER``: the user cancelled the setup flow.
- ``error == TIMEOUT``: timeout occurred, most likely because of no user input.
"""

error: IntegrationSetupError


class SetupAction:
"""Setup action response base class."""

Expand Down

0 comments on commit 9990b4e

Please sign in to comment.