thejeffreystone / SlackerLabVideoExamples

37 stars 5 forks source link

2023-ResponseVariables script suggestion #1

Open brianhanifin opened 1 year ago

brianhanifin commented 1 year ago

https://github.com/thejeffreystone/SlackerLabVideoExamples/blob/a5aaf79e587f5922ba7606e2283157295fd0005f/2023-ResponseVariables/scripts.yaml#L26

For Google home speakers, that code worked... as long as you don't have any displays. Since I have two displays I modified that code filter for a list of entity ids from the Cast integration. I suspect you could do the same with Alexa media player. (integration_entities('alexa_media_player') perhaps?)

{% set audio_speaker = states.media_player | 
    selectattr('entity_id', 'in', integration_entities('cast')) |
    selectattr('entity_id', 'in', area_entities(room)) |
    map(attribute='entity_id') |
  list | first %}

I suspect you could do the same with Alexa media player. (integration_entities('alexa_media_player') perhaps?) In fact, you can concatenate lists easily enough so you could select either a Google or an Alexa entity like so.

{% set audio_speaker = states.media_player | 
    selectattr('entity_id', 'in', integration_entities('alexa_media_player') + integration_entities('cast')) |
    selectattr('entity_id', 'in', area_entities(room)) |
    map(attribute='entity_id') |
  list | first %}

I find referencing integration_entities to be very powerful. However, it can take some experimentation to determine the correct way to reference the desired Integration. For reference look at the variety of ways my integrations a referenced my sensor.unavailable_count and sensor.unknown_count template sensors.


On a related note I was recently brave enough to how to use bitwise filters to select media_players which support TURN_ON, TURN_OFF, and VOLUME_SET by decoding their supported_features fields. There are files in the Home Assistant github repo which list the constants values for these features.

https://github.com/brianhanifin/Home-Assistant-Config/blob/3fc08ce174808b0d13d26d94ea7d791f96c7fe03/scripts.yaml#L1503

brianhanifin commented 1 year ago

Here is a clarification on the integration_entities() value to reference by 123 Taras on the Home Assistant community.

https://community.home-assistant.io/t/selectattr-or-rejectattr-based-on-a-integration/510680/6

brianhanifin commented 1 year ago

One more suggestion. You could adjust your variables section to be more readable, and still return the room_services dictionary in the correct format. I came across this method by experimentation.

variables:
  jarvis_speaker: |
    {{
      states.media_player | 
        selectattr('entity_id', 'in', area_entities(room)) |
        rejectattr('attributes.last_called', 'undefined') |
        map(attribute='entity_id') |
        list | first
    }}
  audio_speaker: |
    {{
      states.media_player
        | selectattr('entity_id', 'in', integration_entities('alexa_media_player') + integration_entities('cast'))
        | selectattr("entity_id", "in", area_entities(room))
        | map(attribute="entity_id")
        | list
        | first
    }}
  tts: 'amp'
  room_services:
    area: "{{room}}"
    jarvis_speaker: "{{jarvis_speaker}}"
    jarvis_tts: "{{tts}}"
    audio_speaker: "{{audio_speaker}}"