Skip to content

Commit

Permalink
Refactor oauth flow to use get_response to better manage message co…
Browse files Browse the repository at this point in the history
…ntext and debug errors
  • Loading branch information
NeonDaniel committed Jan 3, 2024
1 parent 8628f71 commit cdba998
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions ovos_PHAL_plugin_homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, bus=None, config=None):
self.connector = None
self.registered_devices = [] # Device objects
self.registered_device_names = [] # Device friendly/entity names
self.bus = bus

self.gui = GUIInterface(bus=self.bus, skill_id=self.name,
config=self.config_core.get('gui'),
ui_directories={"qt5": join(dirname(__file__),
Expand Down Expand Up @@ -110,9 +110,8 @@ def __init__(self, bus=None, config=None):
self.bus.on("configuration.patch", self.init_configuration)

# LISTEN FOR OAUTH RESPONSE
self.bus.on("oauth.app.host.info.response", self.handle_oauth_host_info)
self.bus.on("oauth.generate.qr.response", self.handle_qr_oauth_response)
self.bus.on(f"oauth.token.response.{self.munged_id}", self.handle_token_oauth_response)
self.bus.on(f"oauth.token.response.{self.munged_id}",
self.handle_token_oauth_response)

self.init_configuration()

Expand Down Expand Up @@ -393,7 +392,7 @@ def handle_get_device(self, message: Message):

# No device found
LOG.debug(f"No Home Assistant device exists for {device}")
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

def _return_device_response(self, message, device_id) -> None:
"""Return the device representation to the bus
Expand All @@ -406,7 +405,7 @@ def _return_device_response(self, message, device_id) -> None:
if device.device_id == device_id:
return self.bus.emit(message.response(data=device.get_device_display_model()))
LOG.debug(f"No device found with device ID {device_id}")
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

def handle_turn_on(self, message):
""" Handle the turn on message
Expand All @@ -422,7 +421,7 @@ def handle_turn_on(self, message):
return self.bus.emit(message.response(data={"device": spoken_device}))
# No device found
LOG.debug(f"No Home Assistant device exists for {device_id}")
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

def handle_turn_off(self, message):
""" Handle the turn off message
Expand All @@ -438,7 +437,7 @@ def handle_turn_off(self, message):
return self.bus.emit(message.response(data={"device": spoken_device}))
# No device found
LOG.debug(f"No Home Assistant device exists for {device_id}")
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

def _gather_device_id(self, message):
"""Given a bus message, return the device ID and spoken device name for reference
Expand Down Expand Up @@ -612,7 +611,7 @@ def handle_get_device_display_model(self, message):
self.bus.emit(message.response(
data=device.get_device_display_model()))
return
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

def handle_get_device_display_list_model(self, message):
""" Handle the get device display list model message
Expand All @@ -636,7 +635,7 @@ def handle_assist_message(self, message):
if self.connector and type(self.connector) in (HomeAssistantWSConnector, HomeAssistantRESTConnector):
self.bus.emit(message.response(data=self.connector.send_assist_command(command)))
else:
self.bus.emit(message.response(data=None))
self.bus.emit(message.response())

# GUI INTERFACE HANDLERS
def handle_show_dashboard(self, message=None):
Expand Down Expand Up @@ -752,8 +751,13 @@ def handle_set_group_display_settings(self, message):
self.handle_show_dashboard()

# OAuth QR Code Flow Handlers
def request_host_info_from_oauth(self):
self.bus.emit(Message("oauth.get.app.host.info"))
def request_host_info_from_oauth(self, message):
resp = self.bus.wait_for_response(message.forward(
"oauth.get.app.host.info"), "oauth.app.host.info.response", 10)
if not resp:
raise RuntimeError(f"No response from oauth plugin to message: "
f"{message.msg_type}: {message.data}")
self.handle_oauth_host_info(resp)

def handle_oauth_host_info(self, message):
host = message.data.get("host", None)
Expand All @@ -762,18 +766,20 @@ def handle_oauth_host_info(self, message):

if self.temporary_instance:
self.oauth_register()
self.start_oauth_flow()
self.start_oauth_flow(message)

def handle_start_oauth_flow(self, message):
""" Handle the start oauth flow message
Args:
message (Message): The message object
"""
instance = message.data.get("instance", None)
instance = message.data.get("instance")
if instance:
self.temporary_instance = instance.lower()
self.request_host_info_from_oauth()
self.request_host_info_from_oauth(message)
else:
LOG.error(f"`instance` missing from message: {message.msg_type}")

def oauth_register(self):
""" Register the phal plugin with the oauth service """
Expand All @@ -790,14 +796,17 @@ def oauth_register(self):
"refresh_endpoint": "",
}))

def start_oauth_flow(self):
host = self.temporary_instance.replace("ws://", "http://").replace("wss://", "https://")
def start_oauth_flow(self, message):
app_id = "homeassistant-phal-plugin"
skill_id = "ovos-PHAL-plugin-homeassistant"
self.bus.emit(Message("oauth.generate.qr.request", {
"app_id": app_id,
"skill_id": skill_id
}))
resp = self.bus.wait_for_response(
message.forward("oauth.generate.qr.request",
{"app_id": app_id, "skill_id": skill_id}),
"oauth.generate.qr.response", 10)
if not resp:
raise RuntimeError(f"No response from oauth plugin to message: "
f"{message.msg_type}: {message.data}")
self.handle_qr_oauth_response(resp)

def handle_qr_oauth_response(self, message):
qr_code_url = message.data.get("qr", None)
Expand Down

0 comments on commit cdba998

Please sign in to comment.