Run an adb shell command on firetv / androidtv component

Hi all,
I would like to automatically start a specific netflix tv show on my firetv stick. I have configured the androidtv component and everything works fine. I can send some standard commands through the androidtv.adb_command service with a json formatted like this:

{
    "entity_id": "media_player.fire_tv_as_android_tv",
    "command": "HOME"
}

The documentation says that I can send some adb shell commands, so my question is how can I do that to start e.g. netflix to open a specific tv show?

I have found this adb command which could work:
adb shell am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -d netflix://title/80997085

But I don’t know how to enter it properly into the service json field.

Thank you very much!

Just omit the adb shell part.

Perfect! Thank you, I thought I tried to omit the first part but somehow I probably missed it :see_no_evil:

Just for completeness I had to add some command line arguments to get it working on a firetv stick. Here is my service json body:

{
  "entity_id": "media_player.fire_tv_as_android_tv",
  "command": "am start -n com.netflix.ninja/.MainActivity -a android.intent.action.VIEW -e amzn_deeplink_data 80234484"
}

Where 80234484 is the specific show id, which can be retrieved from the netflix url like this:
https://www.netflix.com/watch/80234484?trackId=Bla-bla-bla

Credits go out to this gist: https://gist.github.com/tobiasviehweger/5a52ba1ae79f1c8e6ba04e3eb4741789

4 Likes

Hi and sorry for hijacking this thread, but I have a very similar question:

I’m trying to issue an adb command to my FireTV and get a response from it as described here:

adb shell pm dump org.xbmc.kodi | grep -A 1 "MAIN" | grep org.xbmc.kodi | awk '{print $2}' | grep org.xbmc.kodi

I couldn’t find a way to do that, after searching for almost a week.

I.e. I’m looking for an activity or intent that starts streaming Hue Sync

At the moment I’m letting Home Assistant start Hue Stream and Kodi at a certain time in the evening and navigate through the app with Harmony. This seems overly complicated and relies on Hue Stream being off, else the automation would be turning it off instead of on.

- id: kodi_21_15_an
  alias: Kodi 21:15 an
  trigger:
  - at: '21:15:00'
    platform: time
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: media_player.kodi
        state: 'off'
      - condition: or
        conditions:
        - condition: state
          entity_id: device_tracker.google_maps_******
          state: home
        - condition: state
          entity_id: device_tracker.google_maps_******
          state: home
  action:
  - data:
      activity: '37467008'
      entity_id: remote.harmony_hub
    service: remote.turn_on
  - delay: 00:00:30
  - data:
      entity_id: media_player.fire_tv
      source: com.bullbash.huestream
    service: media_player.select_source
  - delay: 00:00:05
  - data:
      command: DirectionDown
      device: '59741152'
      entity_id: remote.harmony_hub
    service: remote.send_command
  - data:
      command: OK
      device: '59741152'
      entity_id: remote.harmony_hub
    service: remote.send_command
  - data:
      entity_id: media_player.fire_tv
      source: org.xbmc.kodi
    service: media_player.select_source

Maybe this automation helps someone or someone can help me find a way to achieve what I’m trying to do.

Thanks in advance.

  • To send that ADB command using the androidtv.adb_command service, just drop the “adb shell” prefix.
  • Currently, any output from that command will be recorded in HA’s log. Short of parsing the log, there’s no way to utilize that data. This could be improved, although I’m not sure what the best approach is. Perhaps the component could fire an event with that data?
  • I don’t think the ADB shell supports awk. At least, I’m pretty sure my generation 2 Fire TV sticks don’t.

I think the best approach for you is to mimic the turn_off command in the androidtv package. Instead of using Harmony to send the down and OK commands, use the androidtv.adb_command service with something like this:

command: "YOUR_COMMAND | grep -q REQUIRED_TEXT && input keyevent 20 && input keyevent 23"

See constants.py for more info about the key commands. That command will only send the down and center (OK) commands if “REQUIRED_TEXT” is found in the output of your command.

If you get it working, please share your solution!

1 Like

Thanks a lot!

I have it working now, the way you described.

- id: kodi_21_15_an
  alias: Kodi 21:15 an
  trigger:
  - at: '21:15:00'
    platform: time
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: media_player.kodi
        state: 'off'
      - condition: or
        conditions:
        - condition: state
          entity_id: device_tracker.google_maps_***********1
          state: home
        - condition: state
          entity_id: device_tracker.google_maps_***********2
          state: home
  action:
  - data:
      activity: '37467008'
      entity_id: remote.harmony_hub
    service: remote.turn_on
  - delay: 00:00:30
  - data:
      entity_id: media_player.fire_tv
      source: com.bullbash.huestream
    service: media_player.select_source
  - delay: 00:00:05
  - data:
      command: dumpsys activity service com.bullbash.huestream/com.bullbash.entertainment.screenstatebroadcaster.ScreenBroadcastService | grep -q  'No services match' && input keyevent 20 && input keyevent 23
      entity_id: media_player.fire_tv
    service: androidtv.adb_command
  - data:
      entity_id: media_player.fire_tv
      source: org.xbmc.kodi
    service: media_player.select_source

Most of the time this should do the job.

Now I’m trying to figure out, how to only start the entire automation, if HueStream isn’t streaming and no one is watching anything else, like Netflix.
I don’t really understand how to do this, as there doesn’t seem to be an easy way to pass that data to a sensor, but if I ever find out, I’m gonna update my post. :slight_smile:

You could check whether the current app (i.e., source) is Netflix.

- condition: template
  value_template: "{{ state_attr('androidtv.fire_tv', 'source') != com.netflix.ninja }}"
2 Likes

In the next HA version, I added an adb_response attribute that will store the output, if any, of the last ADB command. So you could use that in a template:

{{ is_state_attr('media_player.fire_tv', 'adb_response', 'SOMETHING') }}
1 Like

@JeffLIrion How to know which is the correct syntax to launch Youtube TV on a fire TV?
Really cannot find the correct name of the youtube apk and the syntax.

Did you ever figure this out? I am also trying to launch a youtube video on my firetv cube.

adb command to call from HA, should be:

"am start -a android.intent.action.VIEW -d -n com.google.android.youtube.tv/com.google.android.apps.youtube.tv.activity.ShellActivity"

as described here and already mentioned here.

How do I actually launch a video instead of just opening the app. Ex. When we wake up in the morning I would like to launch a youtube livestream link for ambiance.

I found it! This command worked on my new FireTV Cube to launch a youtube video. I used this with the ADB bridge addon. I will try next with the python method to see if I have the same results. Hope this helps!

entity_id: media_player.family_room_firetv_atv
command: am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://www.youtube.com/watch?v=HGg77PwGJbY"

This does NOT work on a firetv cube. For one, the package name is different.

I have a Sony Bravia Android TV, have the below working well and changing to the channel:

am start -a android.intent.action.VIEW -d watch/{{channel_code}} -n com.google.android.youtube.tvunplugged/com.google.android.apps.youtube.tvunplugged.activity.MainActivity

I’ve been trying to do the same using the FireTV Cube we have in the bedroom. This will launch Youtube TV if it’s not running, but will NOT change to the channel given by {{channel_code}}.

am start -a android.intent.action.VIEW -d watch/{{channel_code}} -n com.amazon.firetv.youtube.tv/dev.cobalt.app.MainActivity

It does return the below in the adb attributes, so I’m guessing either the -d parameter isn’t correct or is getting ignored for some reason.

Warning: Activity not started, its current task has been brought to the front

Anybody know of any way to get this to work?

Well, its over year since the start of the thread, but I was having a similar issue on my Sony Android TV and found a couple solutions I thought I should share.

I found this thread that has a command that worked for me:

I used this in my script that calls the “androidtv.adb_command” service.
command: >-
‘am start -c android.intent.category.LEANBACK_LAUNCHER -a
android.intent.action.VIEW -d http://www.netflix.com/watch/80992048 -f
0x10808000 -e source 30 com.netflix.ninja/.MainActivity’

Just swap the 80992048 for your show ID.

As well as I have an android TV, and it might be slightly different for a FireTV, my original inspiration came from this thread on Reddit that is about a FireTV.

Originally I dropped the last part as it seemed to be specific to amazon, but turned out it was the key to my Sony Android TV as well.

1 Like

Hi,

Any way to get this working whit the actual tv show/movie name instead the title id?

Did you know the command for Amazon Video? I am trying to do the same for Amazon Prime Videos with the specific show. Whenever I start the script, It should start the Specific Show.