Nerwyn / android-tv-card

A completely customizable universal remote card for Home Assistant. Supports multiple platforms out of the box.
Apache License 2.0
254 stars 13 forks source link

Default entity_id for custom calls #73

Closed kingy444 closed 7 months ago

kingy444 commented 8 months ago

Is your feature request related to a problem? Please describe.

More of a code clean up (yaml) request and easy of changing devices. You have entity definitions for remote_id and slider_id. Could we have a new entity definition field that can be used as "default for custom calls"

Describe the solution you'd like I am thinking at the top of the card we can define something like media_player_id: media_player.lounge_tv

then rather then having to change all tap_action as below

tap_action:
      action: call-service
      service: media_player.play_media
      data:
        entity_id: media_player.lounge_tv
        media_content_type: send_key
        media_content_id: KEY_0

they could be defined as something like the below

tap_action:
      action: call-service
      service: media_player.play_media
      data:
        media_content_type: send_key
        media_content_id: KEY_0

Essentially then when you build the service call do something like

if entity_id:
  # use entity_id in service call
else
  # use media_player_id
Nerwyn commented 7 months ago

So a global value for media_player_id that autopopulates any media player service calls similar to how the key and source work with remote_id. I should probably also make it so that remote_id autopopulates service calls as well. In order to prevent undesirable behavior, I'm going to also add a flag autofill_entity_id which defaults to false which will enable this behavior for service calls.

Nerwyn commented 7 months ago

@kingy444 this remote config is working for me as of the latest 3.5.3 alpha (alpha.015). Can you give it a try?

type: custom:android-tv-card
media_player_id: media_player.spotify
autofill_entity_id: true
rows:
  - - slider
    - previous
    - next
    - play_pause
custom_actions:
  previous:
    tap_action:
      action: call-service
      service: media_player.media_previous_track
  play_pause:
    tap_action:
      action: call-service
      service: media_player.media_play_pause
  next:
    tap_action:
      action: call-service
      service: media_player.media_next_track
  slider:
    style:
      '--tooltip-label': '{{ (100 * VALUE) | int }}%'
Nerwyn commented 7 months ago

Because this release has a bunch of small feature requests I've bumped the version up to 3.6.0 and put a beta out. Let me know if it looks good to you!

Nerwyn commented 7 months ago

@kingy444 if you have no comments on this feature implementation and that for #74 I'm going to release it in the next few days.

kingy444 commented 7 months ago

@kingy444 if you have no comments on this feature implementation and that for #74 I'm going to release it in the next few days.

Sorry for not getting back to you

I've been away and will hopefully be able to check it later tonight/tomorrow

Nerwyn commented 7 months ago

One drawback I just noticed is that I only have it autofilling for services with domain remote or media_player, but here are other services like kodi and denonavr which wouldn't be covered by this. I'm not sure if I should reconsidered how I'm selecting which service calls get autofilled or add kodi and denonavr services to the list of services autofilled using media_player_id.

Edit: Making it so that Kodi and DenonAVR service calls use media_player_id when autofill_entity_id is true is trivial so I'm going to do that.

kingy444 commented 7 months ago

Seems to work well from my testing

Is there a way to access this entity is in jinja2 like the below?


  power:
    style:
      color: |
        {% if is_state("media_player.lounge_tv", "on") %}
          rgb(229,9,20)
        {% else %}
          rgb(68, 163, 69)
        {% endif %}
Nerwyn commented 7 months ago

This card supports templating via nunjucks, which has 99% the same syntax as jinja2 but is made for JavaScript. That template should work as is with this card.

Nerwyn commented 7 months ago

Optional autofilling of service call entity IDs using remote or media player ID added in 3.6.0.

kingy444 commented 7 months ago

This card supports templating via nunjucks, which has 99% the same syntax as jinja2 but is made for JavaScript. That template should work as is with this card.

You are correct in it works as is but I was looking to make this dynamic

What I am asking is how do I replace media_player.lounge_tv with the new global entity id. Is there a way to access the media_player_id value within the nunjucks ?

Nerwyn commented 7 months ago

There isn't, but I did recently do some refactors to how templates are processed internally to include more internal context. If you create another feature request we can discuss more internal fields to be accessible by templates.