Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -15,9 +15,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
SOURCE = "Home Assistant Dialogflow"
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: {}
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema({DOMAIN: {}}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
class DialogFlowError(HomeAssistantError):
|
||||
@@ -37,7 +35,7 @@ async def handle_webhook(hass, webhook_id, request):
|
||||
|
||||
try:
|
||||
response = await async_handle_message(hass, message)
|
||||
return b'' if response is None else web.json_response(response)
|
||||
return b"" if response is None else web.json_response(response)
|
||||
|
||||
except DialogFlowError as err:
|
||||
_LOGGER.warning(str(err))
|
||||
@@ -47,8 +45,7 @@ async def handle_webhook(hass, webhook_id, request):
|
||||
_LOGGER.warning(str(err))
|
||||
return web.json_response(
|
||||
dialogflow_error_response(
|
||||
message,
|
||||
"This intent is not yet configured within Home Assistant."
|
||||
message, "This intent is not yet configured within Home Assistant."
|
||||
)
|
||||
)
|
||||
|
||||
@@ -56,21 +53,22 @@ async def handle_webhook(hass, webhook_id, request):
|
||||
_LOGGER.warning(str(err))
|
||||
return web.json_response(
|
||||
dialogflow_error_response(
|
||||
message,
|
||||
"Invalid slot information received for this intent."
|
||||
message, "Invalid slot information received for this intent."
|
||||
)
|
||||
)
|
||||
|
||||
except intent.IntentError as err:
|
||||
_LOGGER.warning(str(err))
|
||||
return web.json_response(
|
||||
dialogflow_error_response(message, "Error handling intent."))
|
||||
dialogflow_error_response(message, "Error handling intent.")
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
"""Configure based on config entry."""
|
||||
hass.components.webhook.async_register(
|
||||
DOMAIN, 'DialogFlow', entry.data[CONF_WEBHOOK_ID], handle_webhook)
|
||||
DOMAIN, "DialogFlow", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
@@ -86,36 +84,38 @@ async_remove_entry = config_entry_flow.webhook_async_remove_entry
|
||||
|
||||
def dialogflow_error_response(message, error):
|
||||
"""Return a response saying the error message."""
|
||||
dialogflow_response = DialogflowResponse(message['result']['parameters'])
|
||||
dialogflow_response = DialogflowResponse(message["result"]["parameters"])
|
||||
dialogflow_response.add_speech(error)
|
||||
return dialogflow_response.as_dict()
|
||||
|
||||
|
||||
async def async_handle_message(hass, message):
|
||||
"""Handle a DialogFlow message."""
|
||||
req = message.get('result')
|
||||
action_incomplete = req['actionIncomplete']
|
||||
req = message.get("result")
|
||||
action_incomplete = req["actionIncomplete"]
|
||||
|
||||
if action_incomplete:
|
||||
return None
|
||||
|
||||
action = req.get('action', '')
|
||||
parameters = req.get('parameters').copy()
|
||||
action = req.get("action", "")
|
||||
parameters = req.get("parameters").copy()
|
||||
parameters["dialogflow_query"] = message
|
||||
dialogflow_response = DialogflowResponse(parameters)
|
||||
|
||||
if action == "":
|
||||
raise DialogFlowError(
|
||||
"You have not defined an action in your Dialogflow intent.")
|
||||
"You have not defined an action in your Dialogflow intent."
|
||||
)
|
||||
|
||||
intent_response = await intent.async_handle(
|
||||
hass, DOMAIN, action,
|
||||
{key: {'value': value} for key, value
|
||||
in parameters.items()})
|
||||
hass,
|
||||
DOMAIN,
|
||||
action,
|
||||
{key: {"value": value} for key, value in parameters.items()},
|
||||
)
|
||||
|
||||
if 'plain' in intent_response.speech:
|
||||
dialogflow_response.add_speech(
|
||||
intent_response.speech['plain']['speech'])
|
||||
if "plain" in intent_response.speech:
|
||||
dialogflow_response.add_speech(intent_response.speech["plain"]["speech"])
|
||||
|
||||
return dialogflow_response.as_dict()
|
||||
|
||||
@@ -129,7 +129,7 @@ class DialogflowResponse:
|
||||
self.parameters = {}
|
||||
# Parameter names replace '.' and '-' for '_'
|
||||
for key, value in parameters.items():
|
||||
underscored_key = key.replace('.', '_').replace('-', '_')
|
||||
underscored_key = key.replace(".", "_").replace("-", "_")
|
||||
self.parameters[underscored_key] = value
|
||||
|
||||
def add_speech(self, text):
|
||||
@@ -143,8 +143,4 @@ class DialogflowResponse:
|
||||
|
||||
def as_dict(self):
|
||||
"""Return response in a Dialogflow valid dictionary."""
|
||||
return {
|
||||
'speech': self.speech,
|
||||
'displayText': self.speech,
|
||||
'source': SOURCE,
|
||||
}
|
||||
return {"speech": self.speech, "displayText": self.speech, "source": SOURCE}
|
||||
Reference in new issue
Block a user