Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Dialogflow to work with V2 API #25975

Merged
merged 2 commits into from Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 38 additions & 8 deletions homeassistant/components/dialogflow/__init__.py
Expand Up @@ -17,6 +17,9 @@

CONFIG_SCHEMA = vol.Schema({DOMAIN: {}}, extra=vol.ALLOW_EXTRA)

V1 = 1
V2 = 2


class DialogFlowError(HomeAssistantError):
"""Raised when a DialogFlow error happens."""
Expand Down Expand Up @@ -84,23 +87,45 @@ async def async_unload_entry(hass, entry):

def dialogflow_error_response(message, error):
"""Return a response saying the error message."""
dialogflow_response = DialogflowResponse(message["result"]["parameters"])
_api_version = get_api_version(message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't prefix parameters with _ if you're using them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed - that was bad styling on my part. It's fixed now.

if _api_version is V1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use == for comparison, not is. is is for pointer equivalency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended pointer equivalency here since it's comparing integers, but I'm happy to change it to ==.

parameters = message["result"]["parameters"]
elif _api_version is V2:
parameters = message["queryResult"]["parameters"]
dialogflow_response = DialogflowResponse(parameters, get_api_version(message))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use _api_version here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye! Should be fixed now.

dialogflow_response.add_speech(error)
return dialogflow_response.as_dict()


def get_api_version(message):
"""Get API version of Dialogflow message."""
if message.get("id") is not None:
return V1
if message.get("responseId") is not None:
return V2


async def async_handle_message(hass, message):
"""Handle a DialogFlow message."""
req = message.get("result")
action_incomplete = req["actionIncomplete"]
_api_version = get_api_version(message)
if _api_version is V1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem.

_LOGGER.warning(
"Dialogflow V1 API will be removed on October 23, 2019. Please change your DialogFlow settings to use the V2 api"
)
req = message.get("result")
action_incomplete = req.get("actionIncomplete", True)
if action_incomplete is True:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if action_incomplete is True:
if action_incomplete:

return

if action_incomplete:
return None
elif _api_version is V2:
req = message.get("queryResult")
if req.get("allRequiredParamsPresent", False) is False:
return

action = req.get("action", "")
parameters = req.get("parameters").copy()
parameters["dialogflow_query"] = message
dialogflow_response = DialogflowResponse(parameters)
dialogflow_response = DialogflowResponse(parameters, _api_version)

if action == "":
raise DialogFlowError(
Expand All @@ -123,10 +148,11 @@ async def async_handle_message(hass, message):
class DialogflowResponse:
"""Help generating the response for Dialogflow."""

def __init__(self, parameters):
def __init__(self, parameters, api_version):
"""Initialize the Dialogflow response."""
self.speech = None
self.parameters = {}
self.api_version = api_version
# Parameter names replace '.' and '-' for '_'
for key, value in parameters.items():
underscored_key = key.replace(".", "_").replace("-", "_")
Expand All @@ -143,4 +169,8 @@ def add_speech(self, text):

def as_dict(self):
"""Return response in a Dialogflow valid dictionary."""
return {"speech": self.speech, "displayText": self.speech, "source": SOURCE}
if self.api_version is V1:
return {"speech": self.speech, "displayText": self.speech, "source": SOURCE}

if self.api_version is V2:
return {"fulfillmentText": self.speech, "source": SOURCE}