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

How to utilize the new alexa_media_last_called_event in AMP 4.13.x #2535

Open
danielbrunt57 opened this issue Sep 17, 2024 · 1 comment
Open

Comments

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Sep 17, 2024

Describe the implementation

AMP 4.13.x will now fire an event when last_called is updated.

"alexa_media_last_called_event",
                    {
                        "last_called": self.device_serial_number,
                        "timestamp": self._last_called_timestamp,
                        "summary": self._last_called_summary,
                    },

This can be used in automations/scripts to wait for that event before continuing. I've implemented a timeout in the wait_for_trigger of 30 seconds but that might be excessive. You just want to avoid too short of a timeout in which the update last_called can succeed. This wait_for_trigger avoids manually calling alexa_media.update_last_called and then delaying a fixed period of time before proceeding and also can ensure that last_called was truly updated. It can also avoid the case where the integration is not working and your scripts/automations fail to execute as the Alexa service/entity does not exist.

wait_for_trigger:
  - platform: event
    event_type: alexa_media_last_called_event
    event_data:
      summary: good night
timeout:
  hours: 0
  minutes: 0
  seconds: 30
  milliseconds: 0
continue_on_timeout: true

The event_data: summary: good night is the last thing in the history records that was said to Alexa and is included in the event so you can include that to ensure the last_called is truly correct for this script, or you can elect to not include that check and just proceed.

Following that, I have:

variables:
  alexa_last_called_updated: "{{ wait.remaining > 0 }}"

which is then used in if-then's to conditionally call actionable notifications or notify.alexa_media_last_called or...

if:
  - alias: Test if alexa_last_called_updated renders true
    condition: template
    value_template: "{{ alexa_last_called_updated }}"
  - condition: state
    entity_id: input_boolean.coffee_prepared
    state: "off"
then:
  - data:
      text: Did you prepare your coffee?
      event_id: actionable_notification_coffee_prepared
      alexa_device: "{{ states('sensor.last_alexa') }}"
    action: script.activate_alexa_actionable_notification

or

if:
  - condition: template
    value_template: "{{ alexa_last_called_updated }}"
then:
  - data:
      message: Good morning
    action: script.notify_alexa_media

Another nifty thing I figured out was to create a new Home Assistant user that is only used by the Alexa Skill to integrate HA with Alexa. The Alexa skill then authenticates with HA using that username and you can use that to watch for state_changed events that were triggered by that user to automatically update last_called when Alexa has been asked to do something in HA like turn this or that on|off or when you say something that turns on an Alexa scene (which is an HA script which then waits for the new AMP event to fire).

alias: Alexa - Triggered by Smart Home Skill user account
description: >-
  Perform action `Alexa Media Player: Update Last Called Sensor` when entity
  states are changed by Alexa Smart Home Skill user account
trigger:
  - platform: event
    event_type: state_changed
    context:
      user_id: 0df463c1efc4490f900b09b143cae9c4
condition: []
action:
  - action: alexa_media.update_last_called
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 6
      milliseconds: 0
trace:
  stored_traces: 30
mode: single
max_exceeded: silent

System details

  • Home Assistant version: 2024.9.2
  • alexa_media version (from const.py or HA startup log): 4.13.1
  • alexapy version (from pip show alexapy in homeasssistant container or HA startup log): 1.29.2
  • Is Amazon 2FA/2SV enabled <!---We will not debug login issues if unanswered---> (y/n): Y
  • Amazon Domain: amazon.ca

Debug Logs (alexa_media & alexapy)

2024-09-17 04:20:13.397 DEBUG (MainThread) [custom_components.alexa_media.services] Service update_last_called for: []
2024-09-17 04:20:13.827 DEBUG (MainThread) [alexapy.alexaapi] d****l@b******a: static GET: https://www.amazon.ca/alexa-privacy/apd/rvh/customer-history-records?startTime=1726485613397&endTime=1726658413397&recordType=VOICE_HISTORY&maxRecordSize=10 returned 200:OK:application/json
2024-09-17 04:20:13.828 DEBUG (MainThread) [custom_components.alexa_media] d****l@b******a: Updated last_called: {'serialNumber': 'G************S0N', 'timestamp': 1726572010837, 'summary': 'turn on kitchen light'}
2024-09-17 04:20:13.828 DEBUG (MainThread) [custom_components.alexa_media] d****l@b******a: last_called changed: {'serialNumber': 'G************6SA', 'timestamp': 1726567732307, 'summary': ','} to {'serialNumber': 'G************S0N', 'timestamp': 1726572010837, 'summary': 'turn on kitchen light'}
2024-09-17 04:20:13.830 DEBUG (MainThread) [custom_components.alexa_media.media_player] d****l@b******a: <entity media_player.office_echo_dot_right=standby> is last_called: G************S0N
2024-09-17 04:20:13.831 DEBUG (MainThread) [custom_components.alexa_media.media_player] d****l@b******a: Refreshing notify targets
2024-09-17 04:20:13.831 DEBUG (MainThread) [custom_components.alexa_media.notify] d****l@b******a: Found last_called <entity media_player.office_echo_dot_right=standby> called at 1726572010837
2024-09-17 04:20:13.831 DEBUG (MainThread) [custom_components.alexa_media.notify] d****l@b******a: Creating last_called target last_called using <entity media_player.office_echo_dot_right=standby> called at 1726572010837
2024-09-17 04:20:13.831 DEBUG (MainThread) [custom_components.alexa_media.notify] d****l@b******a: Found last_called <entity media_player.office_echo_dot_right=standby> called at 1726572010837
2024-09-17 04:20:13.831 DEBUG (MainThread) [custom_components.alexa_media.notify] d****l@b******a: Creating last_called target last_called using <entity media_player.office_echo_dot_right=standby> called at 1726572010837
@danielbrunt57 danielbrunt57 reopened this Sep 17, 2024
Repository owner deleted a comment from github-actions bot Sep 17, 2024
@danielbrunt57 danielbrunt57 changed the title How to utilize the new alexa_media_last_called_event in AMP 4.13.1 How to utilize the new alexa_media_last_called_event in AMP 4.13.x Sep 17, 2024
@Marcelo-Y
Copy link

Marcelo-Y commented Sep 22, 2024

I have tried the code but the event never fires.
Can't see the event firing in "Developer tools/events" either.

Home assistant info:

Core 2024.9.2
Supervisor 2024.09.1
Operating System 13.1
Frontend 20240909.1

AMP info:
{
"domain": "alexa_media",
"name": "Alexa Media Player",
"codeowners": ["@alandtse", "@keatontaylor"],
"config_flow": true,
"dependencies": ["persistent_notification", "http"],
"documentation": "https://github.com/alandtse/alexa_media_player/wiki",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/alandtse/alexa_media_player/issues",
"loggers": ["alexapy", "authcaptureproxy"],
"requirements": ["alexapy==1.29.2", "packaging>=20.3", "wrapt>=1.14.0"],
"version": "4.13.2"
}

Thanks!

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

No branches or pull requests

2 participants