iOS Shortcuts/ Siri/ HomeKit Remote -> HA integrations

To anyone who might be interested. Was bored one afternoon so I thought I’d add basic Siri commands to my Android TV usIng home assistant companion app and iOS shortcuts. Gathered all the info and tools needed and went for it :joy: Might eventually expand to more commands depending on my usage needs.

To sum it up, shortcut no1 by saying hey Siri Android TV will get you 3 commands Turn it off, Turn it on and Set the volume to (number)%, meaning adjusting volume by setting the percentage of it.
Shortcut no2 will get you the ability of playing movies or series on Netflix by just saying Hey Siri Netflix, then saying the name which one you’d like to watch (can’t pick seasons or episodes for now, but u can pick up where you left or start from the beginning if it’s something new) ,
Shortcut no3 by saying hey Siri YouTube and then telling it name of the video, is opening and playing YouTube videos directly ( goes for the first video result in search, channel search can be added as well as opening searched queries in YouTube app,but that I’m planning on adding later on). To test and use them, only two things are needed to be done after downloading them, change tv entity_id in the shortcuts dictionaries to match yours android tv entity id, and set the server in home assistant companion call service shortcut action in these 3 shortcuts( located bellow dictionaries)

Netflix

YouTube

Android TV

1 Like

Thanks for sharing, I am new to these shortcuts with the companion app, so will have to play with it a little and see how to set it up, but sound interesting

If you need any help, if I can, I’ll help out, feel free to ask

Here’s another one, this time using curl with shortcuts and HA REST API to get or post states

Curl REST API Shortcut

To have it work, you need to create a long token and paste it in second action in shortcut instead of word YourLongToken

To have it get a certain state of something, for example what’s playing on your android tv, in states url, add your android tv/ chrome cast entity name ( mine being media_player.android_tv)

http://192.xxx.xxx.xxx:8123/api/states/media_player.android_tv

Once that is done, add these shown in picture.

I’ve probably could of done it a lot cleaner, but it works good enough to get Siri to tell you what’s playing on tv ( as long as chromecast can get the name of current media playing)

If you got a pin on your profile in Netflix app then In the Netflix shortcut add this at the end of the adb command in the dictionary

&& input keyevent 23

This should automatically open the pin part, and then to have it enter your pin, after 23 add && input keyevent with the corresponding key number for your pin code for your first number of your pin, once you’ve entered the first number continue with another number using && input keyevent Once you’ve entered all 4 numbers end with

&& input keyevent 23 && input keyevent 23 && input keyevent 23

This should enter your pin and automatically start your show or movie you’ve picked.

Reference for number key events

7 -->  is 0
8 -->  is 1
9 -->  is 2
10 --> is 3
11 -->  is 4
12 -->  is 5
13 -->  is 6
14 -->  is 7
15 -->  is 8
16 -->  is 9

Example of what you need to add at the end of the adb shell command in shortcut to have it enter 0000 as the pin code

&& input keyevent 23 && input keyevent 7 && input keyevent 7 && input keyevent 7 && input keyevent 7 && input keyevent 23 && input keyevent 23 && input keyevent 23

This assumes you haven’t launched Netflix yet

Seems that using this speeds up the control center remote
(homekit tv remote) (using this since my tv doesn’t support any http api’s by default, and android tv integration adb commands are super slow)

androidTV_keyboard_withRestAPI

You can find it in playstore on your android tv, install, set as default keyboard and then:

Add to configuration.yaml (using keycode= is same as using and adb shell Input key code, but faster)

rest_command:
  center:
    url: "http://192.168.178.28:5000/keycode=23"
  back:
    url: "http://192.168.178.28:5000/keycode=4"
  play:
    url: "http://192.168.178.28:5000/keycode=85"
  left:
    url: "http://192.168.178.28:5000/keycode=21"
  right:
    url: "http://192.168.178.28:5000/keycode=22"
  up:
    url: "http://192.168.178.28:5000/keycode=19"
  down:
    url: "http://192.168.178.28:5000/keycode=20"
  info:
    url: "http://192.168.178.28:5000/keycode=165"
  volumeup:
    url: "http://192.168.178.28:5000/keycode=24" 
  volumedown:
    url: "http://192.168.178.28:5000/keycode=25"
 
 

Automation


alias: Call Android TV on HomeKit Remote key presses
description: ''
trigger:
  - platform: event
    event_type: homekit_tv_remote_key_pressed
    event_data: {}
condition: []
action:
  - service: |
      {% set key_map = {
        'arrow_right': 'rest_command.right',
        'arrow_down' : 'rest_command.down',
        'arrow_left': 'rest_command.left',
        'arrow_up': 'rest_command.up',
        'select': 'rest_command.center',
        'back': 'rest_command.back',
        'information': 'rest_command.info',
        'volume_down': 'rest_command.volumedown',
        'volume_up': 'rest_command.volumeup',
        'media_play_pause': 'rest_command.play'     
      } %}  {{key_map[trigger.event.data['key_name']] }}

Due to volume up and down, play pause being linked directly to the media player ( android in my case and being slow to respond) to control it I had to make a universal media player that controls my android tv media player as its child. ( media_player.android_tv_192_168_178_28 being the entity name of my android tv )

media_player:
  - platform: universal
    name: AndroidTV
    children:
    - media_player.android_tv_192_168_178_28
    commands:
      turn_on:
        service: media_player.turn_on
        data:
          entity_id: 
          - media_player.android_tv_192_168_178_28
      turn_off:
        service: media_player.turn_off
        data:
          entity_id: 
          - media_player.android_tv_192_168_178_28
      volume_up:
        service: rest_command.volumeup
        data:
          entity_id: 
          - media_player.android_tv_192_168_178_28
      volume_down:
        service: rest_command.volumedown
        data:
          entity_id: 
          - media_player.android_tv_192_168_178_28
      media_play_pause:
        service: rest_command.play
        data:
          entity_id: 
          - media_player.android_tv_192_168_178_28    
    device_class: tv

Big thanks to @petro for opening my eyes templates wise :heart:

you can drop the map if you want by doing the following

rest_command:
   select:
      url: "http://TVIP:5000/center"
   back:
      url: "http://TVIP:5000/back"
   media_play_pause:
      url: "http://TVIP:5000/play"
   arrow_left:
      url: "http://TVIP:5000/left"
   arrow_right:
      url: "http://TVIP:5000/right"
   arrow_up:
      url: "http://TVIP:5000/up"
   arrow_down:
      url: "http://TVIP:5000/down"
   information:
      url: "http://TVIP:5000/keycode=165"
   volume_up:
      url: "http://TVIP:5000/volumeup"
   volume_down:
      url: "http://TVIP:5000/volumedown"
alias: Call Android TV on HomeKit Remote key presses
description: ''
trigger:
  - platform: event
    event_type: homekit_tv_remote_key_pressed
    event_data: {}
condition: []
action:
  - service: rest_command.{{ trigger.event.data['key_name'] }}
1 Like

Thanks again. By the way, using this “method” with the keyboard app installed on android tv makes the button press and tv reaction time basically instantaneous, like using the normal IR remote

Hello I just want to create a virtual button that when is pressed the action in the home assistant is to execute a Home Kit Shortcut that I already have, is this possible?
If yes can you please let me know how to proceed ?

Thank you

What do you mean by this? iOS shortcut app or what exactly?

Siri shortcut (home kit shortcut)

Not possible, iOS shortcuts are action only, they can’t receive any info unless they are running and you are grabbing that info from somewhere. What are you trying to achieve anyways, maybe there is a way to work it out with HA

1 Like

Ok thank you!!

I still going search for this probably there has a way

Probably i will try to use api to execute some url that is going invoke the siri shortcuts … something like that …

Once again thank you for your support and best regards

You can do it, but still got to make an action on your iOS device. Not automated shortcut run tho so not sure if that’s what you are going for. One way is by making That HA button send out an actionable notification to your iOS device, once your device receives the notification you’ll need to press the button in the notification on your device and that notification button opens a shortcut. All info is here

Another way is having ha button send an email with a specific subject, then have an automation in iOS shortcuts to check for mail with a specific subject and once it gets it, it runs your shortcut, this also will result in iOS shortcuts automation asking for your permission to run

Info on sending email with ha is here

1 Like

Thank you for the information :grinning:

I will Try, I also verify that I always need to perform the action on the phone to execute the automations and this is not me idea, but I will try and continue searching

1 Like