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

adding last_called #65

Merged
merged 4 commits into from Jan 25, 2019
Merged

adding last_called #65

merged 4 commits into from Jan 25, 2019

Conversation

brianhanifin
Copy link
Contributor

Following these instructions I reset my repository, reapplied git config core.filemode false, then pasted my changes back into .gitignore + alexa.py.

Hopefully this puts the repo in a better state for you to work with.

@brianhanifin
Copy link
Contributor Author

On a related note I experimented with this in Node-RED for a few minutes this morning. It appears I was having trouble identifying the last Alexa device to be called, because every Echo that can hear my voice reports back to the Activity API.

In the screenshot below you see that both my Kitchen Echo Show and my Family Room Echo both reported back. Unfortunately, the order in which these report back to our component is not consistent. Sometimes the last reported device is the one we want, and other times it reports back first.

screen shot 2019-01-23 at 8 54 41 am

For now this feature is not going to do what we want it to. However, there is a lot of data reported back by the Activity API, such as the exact phrase recognized by the Echo (or in the case of a non-command it reports that as well). I am hopeful that I will be able to find something in that stack that will help me make this feature reliable.

I am going to leave it alone for now so @alandtse can work on splitting them off.

@niemyjski
Copy link

@brianhanifin LOL, that's going to be a problem. I have an Alexa in every room and some rooms have 3 devices. Is there any way to see what one took the command. Sometimes I have two respond with one saying cannot complete and the other actually responding.

It would be great to have additional inputs to say last command or last phrase or last action. For example you can say Alexa I'm leaving to put her into listening mode. Would be great to capture this and trigger HA to do something as well.

@brianhanifin
Copy link
Contributor Author

brianhanifin commented Jan 23, 2019

It would be great to have additional inputs to say last command or last phrase or last action. For example you can say Alexa I'm leaving to put her into listening mode. Would be great to capture this and trigger HA to do something as well.

@niemyjski I am hoping to use this feature myself. For example I'd like to say Goodbye to Alexa and have the Echo I spoke to respond with a random line from my goodbye_line() macro.

Here is a link to a couple of quick tests of the Activity API. I think I can code it to ignore "activityStatus":"DISCARDED_NON_DEVICE_DIRECTED_INTENT".
https://gist.github.com/brianhanifin/f5595578015582a4c22f81827b28af80

P.S. Where I sit in my TV Room, there is an Alexa 10 feet in front of me, an Alexa 4 feet away on the other side of the wall in the Kitchen, and 10 feet behind me in the den. Its anyone's guess as to which one will respond. Sometimes our den Echo starts a timer when we ask for one in the kitchen. LOL.

@brianhanifin
Copy link
Contributor Author

brianhanifin commented Jan 23, 2019

OK, so after doing a few experiments... I came to the conclusion that ignoring the DISCARDED_NON_DEVICE_DIRECTED_INTENT activity status devices seem to make it more reliable. It was so easy to add a quick if statement that I did it. I'm going to play with some automations and see if I can get the right Alexa device to play the response. 😀

@niemyjski
Copy link

Awesome work! If you are in discord hit me up. I wonder if this last_called could also be used for room awareness too or if there is any events that are being submitted for audio activity (I usually keep it on mute) but would be good to trigger a binary_sensor for audio detected or audio playing :) If you are close and you say something it may give you a better idea of proximity even if it's DISCARDED_NON_DEVICE_DIRECTED_INTENT

@alandtse
Copy link
Owner

alandtse commented Jan 24, 2019

EDIT: Sorry, have some code review edits. Please review.

Copy link
Owner

@alandtse alandtse left a comment

Choose a reason for hiding this comment

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

Can you review my comments and test them? I took a closer look before to try to rewrite a docstring comment and had some questions. I may have just missed it.

media_player/alexa.py Outdated Show resolved Hide resolved
media_player/alexa.py Outdated Show resolved Hide resolved
media_player/alexa.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@brianhanifin brianhanifin left a comment

Choose a reason for hiding this comment

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

OK. I have removed the orphaned code and made the recommended change to get_last_device_serial(). I have committed those changes.

media_player/alexa.py Outdated Show resolved Hide resolved
@brianhanifin
Copy link
Contributor Author

EDIT: Sorry, have some code review edits. Please review.

Oops, I read this comment earlier on my iPhone on the GitHawk app. Unfortunately, it didn't show the review information so I didn't see them until just now. I apologize for the delay.

@alandtse
Copy link
Owner

No need to apologize. You're helping in your free time.

Copy link
Owner

@alandtse alandtse left a comment

Choose a reason for hiding this comment

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

Changes look good. Can you also update the Wiki with how to use this functionality?

@alandtse alandtse merged commit fa8cfb7 into alandtse:master Jan 25, 2019
@brianhanifin
Copy link
Contributor Author

brianhanifin commented Jan 26, 2019

I have added the following to the wiki page.

Last called device

Each device will report whether it is the last_called or not. This allows us to identify the device that was called according to the Alexa Activities API.

Creating sensor.last_alexa

sensor:
  - platform: template
    sensors:
      last_alexa:
        value_template: >
          {%- for entity in states.media_player -%}
            {%- if state_attr(entity.entity_id, 'last_called') == True -%}
              {{ entity.entity_id }}
            {%- endif -%}
          {%- endfor -%}

@niemyjski
Copy link

Do you mind sharing an export for your node red script above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants