-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Alexa slot synonym fix #10614
Merged
balloob
merged 6 commits into
home-assistant:dev
from
devspacenine:alexa-slot-synonym-fix
Nov 17, 2017
Merged
Alexa slot synonym fix #10614
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1036443
Added logic to the alexa component for handling slot synonyms
devspacenine bd07f60
Merge branch 'dev' into alexa-slot-synonym-fix
devspacenine 5cfd22b
Moved note with long url to the top of the file
devspacenine 65c532a
Just made a tiny url instead of messing with Flake8
devspacenine 891b8c7
Refactored to be more Pythonic
devspacenine 2742144
Put trailing comma back
devspacenine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,22 @@ | |
|
||
For more details about this component, please refer to the documentation at | ||
https://home-assistant.io/components/alexa/ | ||
""" | ||
|
||
Notes: | ||
[1] - https://developer.amazon.com/docs/custom-skills/define-synonyms-and-ids-for-slot-type-values-entity-resolution.html#intentrequest-changes | ||
|
||
""" # noqa | ||
import asyncio | ||
import enum | ||
import logging | ||
import itertools | ||
|
||
from homeassistant.core import callback | ||
from homeassistant.const import HTTP_BAD_REQUEST | ||
from homeassistant.helpers import intent | ||
from homeassistant.components import http | ||
|
||
from .const import DOMAIN | ||
from .const import DOMAIN, SYN_RESOLUTION_MATCH | ||
|
||
INTENTS_API_ENDPOINT = '/api/alexa' | ||
|
||
|
@@ -123,6 +128,46 @@ def post(self, request): | |
return self.json(alexa_response) | ||
|
||
|
||
def resolve_slot_synonyms(key, request): | ||
"""Check slot request for synonym resolutions.""" | ||
# Default to the spoken slot value if more than one or none are found. For | ||
# reference to the request object structure, see the Alexa docs [1] | ||
resolved_value = request['value'] | ||
|
||
if ('resolutions' in request and | ||
'resolutionsPerAuthority' in request['resolutions'] and | ||
len(request['resolutions']['resolutionsPerAuthority']) >= 1): | ||
|
||
# Only consider the authorities with a successful match | ||
matches = filter( | ||
lambda x: x['status']['code'] == SYN_RESOLUTION_MATCH, | ||
request['resolutions']['resolutionsPerAuthority'] | ||
) | ||
|
||
# Extract all of the possible values from the object. | ||
possible_values = list( | ||
itertools.chain.from_iterable([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write idiomatic Python using for loops and appending to lists. |
||
map( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not use |
||
lambda val: val['value']['name'], | ||
x['values'] | ||
) for x in matches | ||
]) | ||
) | ||
|
||
# If there is only one match use the resolved value, otherwise the | ||
# resolution cannot be determined, so use the spoken slot value | ||
if len(possible_values) == 1: | ||
resolved_value = possible_values[0] | ||
else: | ||
_LOGGER.debug( | ||
'Found multiple synonym resolutions for slot value: {%s: %s}', | ||
key, | ||
request['value'] | ||
) | ||
|
||
return resolved_value | ||
|
||
|
||
class AlexaResponse(object): | ||
"""Help generating the response for Alexa.""" | ||
|
||
|
@@ -135,28 +180,17 @@ def __init__(self, hass, intent_info): | |
self.session_attributes = {} | ||
self.should_end_session = True | ||
self.variables = {} | ||
|
||
# Intent is None if request was a LaunchRequest or SessionEndedRequest | ||
if intent_info is not None: | ||
for key, value in intent_info.get('slots', {}).items(): | ||
underscored_key = key.replace('.', '_') | ||
|
||
if 'value' in value: | ||
self.variables[underscored_key] = value['value'] | ||
|
||
if 'resolutions' in value: | ||
self._populate_resolved_values(underscored_key, value) | ||
|
||
def _populate_resolved_values(self, underscored_key, value): | ||
for resolution in value['resolutions']['resolutionsPerAuthority']: | ||
if 'values' not in resolution: | ||
continue | ||
|
||
for resolved in resolution['values']: | ||
if 'value' not in resolved: | ||
# Only include slots with values | ||
if 'value' not in value: | ||
continue | ||
|
||
if 'name' in resolved['value']: | ||
self.variables[underscored_key] = resolved['value']['name'] | ||
_key = key.replace('.', '_') | ||
|
||
self.variables[_key] = resolve_slot_synonyms(key, value) | ||
|
||
def add_card(self, card_type, title, content): | ||
"""Add a card to the response.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not use
filter
but instead use list comprehensionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.