Native support for Android TV / Android devices

I am seeing a lot of the following in the logs:

Log Details (WARNING)
Thu Oct 10 2019 13:30:02 GMT-0400 (Eastern Daylight Time)
Platform androidtv not ready yet. Retrying in 180 seconds.

BTW, I have the hassio addon running on another HA instance and I am accessing the devices through that addon. I will try the custom component

Announcement

The Android TV component in HA 0.100 contains some fixes in the backend package that handles the Python ADB implementation. If you’re having any issues with the ADB server approach, or you’d rather run an ADB server container, I suggest trying the Python ADB implementation approach.

  1. Stop the ADB server
  2. Copy the key(s) from the ADB server to your HA config directory and add adbkey to your config
  3. Restart your Android TV / Fire TV device
  4. Restart HA

If you experience any issues with the HA media player becoming unavailable, you can capture debug logs for the adb_shell package by using the logger.set_level service with the parameters

adb_shell: debug

Posting them here is OK, but it would be ideal if you could open a GitHub issue and post them in there: https://github.com/JeffLIrion/adb_shell

Without these debug logs, there is no way to diagnose and fix issues.

Hello,

somehow my integration of the Fire TV is broken. It worked fine but shortly after the update to 0.100.3 it broke.

It’s neither working via the ADB Server Addon nor via the python implementation.

This is my config:

  - platform: androidtv
    device_class: firetv
    name: Fire TV
    host: 192.168.178.27
    apps:
      com.amazon.tv.launcher: "Fire TV"
      com.netflix.ninja: "Netflix"
    turn_on_command: "input keyevent 3"
    turn_off_command: "input keyevent 223"
    state_detection_rules:
      'com.amazon.tv.launcher':
        - 'standby'
      'com.netflix.ninja':
        - 'media_session_state'

With the python implementation I get the following log:

2019-10-29 10:13:34 DEBUG (SyncWorker_7) [adb_shell.adb_device] bulk_write: b'CNXN\x00\x00\x00\x01\x00\x10\x00\x00"\x00\x00\x00\xd1\x0c\x00\x00\xbc\xb1\xa7\xb1'
2019-10-29 10:13:34 DEBUG (SyncWorker_7) [adb_shell.adb_device] bulk_write: b'host::homeassistant.local.hass.io\x00'
2019-10-29 10:13:34 WARNING (SyncWorker_7) [androidtv.adb_manager] Couldn't connect to host 192.168.178.27:5555, error: Broken pipe
2019-10-29 10:13:35 DEBUG (SyncWorker_7) [androidtv.adb_manager] ADB command not sent to 192.168.178.27:5555 because python-adb connection is not established: getprop ro.product.manufacturer && getprop ro.product.model && getprop ro.serialno && getprop ro.build.version.release && ip addr show wlan0 | grep -m 1 ether && ip addr show eth0 | grep -m 1 ether
2019-10-29 10:13:35 WARNING (SyncWorker_7) [homeassistant.components.androidtv.media_player] Could not connect to Fire TV at 192.168.178.27:5555 using Python ADB implementation without adbkey authentication
2019-10-29 10:14:06 DEBUG (SyncWorker_8) [adb_shell.adb_device] bulk_write: b'CNXN\x00\x00\x00\x01\x00\x10\x00\x00"\x00\x00\x00\xd1\x0c\x00\x00\xbc\xb1\xa7\xb1'
2019-10-29 10:14:06 DEBUG (SyncWorker_8) [adb_shell.adb_device] bulk_write: b'host::homeassistant.local.hass.io\x00'
2019-10-29 10:14:06 WARNING (SyncWorker_8) [androidtv.adb_manager] Couldn't connect to host 192.168.178.27:5555, error: Broken pipe
2019-10-29 10:14:06 DEBUG (SyncWorker_8) [androidtv.adb_manager] ADB command not sent to 192.168.178.27:5555 because python-adb connection is not established: getprop ro.product.manufacturer && getprop ro.product.model && getprop ro.serialno && getprop ro.build.version.release && ip addr show wlan0 | grep -m 1 ether && ip addr show eth0 | grep -m 1 ether
2019-10-29 10:14:06 WARNING (SyncWorker_8) [homeassistant.components.androidtv.media_player] Could not connect to Fire TV at 192.168.178.27:5555 using Python ADB implementation without adbkey authentication

There shows no entity up with the python implementation.

Using the server addon this is the log:

2019-10-29 10:10:46 DEBUG (SyncWorker_12) [androidtv.adb_manager] ADB connection to 192.168.178.27:5555 via ADB server 127.0.0.1:5037 successfully established
2019-10-29 10:10:46 DEBUG (SyncWorker_12) [androidtv.adb_manager] Sending command to 192.168.178.27:5555 via ADB server 127.0.0.1:5037: getprop ro.product.manufacturer && getprop ro.product.model && getprop ro.serialno && getprop ro.build.version.release && ip addr show wlan0 | grep -m 1 ether && ip addr show eth0 | grep -m 1 ether
2019-10-29 10:10:47 DEBUG (SyncWorker_12) [homeassistant.components.androidtv.media_player] Setup Fire TV at 192.168.178.27:5555 using ADB server at 127.0.0.1:5037
2019-10-29 10:10:59 DEBUG (SyncWorker_6) [androidtv.adb_manager] Sending command to 192.168.178.27:5555 via ADB server 127.0.0.1:5037: dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && ps | grep u0_a
2019-10-29 10:11:00 DEBUG (SyncWorker_6) [androidtv.firetv] Fire TV 192.168.178.27:5555 update response: 
2019-10-29 10:11:10 DEBUG (SyncWorker_8) [androidtv.adb_manager] Sending command to 192.168.178.27:5555 via ADB server 127.0.0.1:5037: dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && ps | grep u0_a
2019-10-29 10:11:10 DEBUG (SyncWorker_8) [androidtv.firetv] Fire TV 192.168.178.27:5555 update response: 
2019-10-29 10:11:21 DEBUG (SyncWorker_5) [androidtv.adb_manager] Sending command to 192.168.178.27:5555 via ADB server 127.0.0.1:5037: dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && ps | grep u0_a
2019-10-29 10:11:21 DEBUG (SyncWorker_5) [androidtv.firetv] Fire TV 192.168.178.27:5555 update response: 
2019-10-29 10:11:32 DEBUG (SyncWorker_1) [androidtv.adb_manager] Sending command to 192.168.178.27:5555 via ADB server 127.0.0.1:5037: dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\c' && dumpsys power | grep Locks | grep 'size=' && CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP && (dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {' || echo) && ps | grep u0_a
2019-10-29 10:11:32 DEBUG (SyncWorker_1) [androidtv.firetv] Fire TV 192.168.178.27:5555 update response: 

In this case there is an entity for the media_player.fire_tv with the following state:

adb_response: null
friendly_name: Fire TV
supported_features: 22961

But the entity is not working and not showing up in the lovelace UI.

I would be very happy if someone here is able to help me. Thanks a lot!

The Python ADB implementation fails because you didn’t provide a key; in the next HA version (released tomorrow), HA will generate a key for you if you do not provide one and so your config should work.

It looks like the ADB server approach worked. If the entity isn’t showing up in the UI, then you need to add it to the UI. Based on your logs, it should be in the “off” state.

Thanks for your help.

You are right, it was not showing up in the UI because the automatic control of Lovelace was disabled and I didn’t know that. Now it shows up again as it did when I startet investigating this issue… The card has the following content using the ADB server approach:

No state available for media_player.fire_tv
{
  "type": "media-control",
  "entity": "media_player.fire_tv"
}

And there is no entity showing up anymore in the developer options. Config is still the same as above with the added server ip.

If there’s no entity on the States page, then it failed to setup the component. The log should confirm this.

I suggest waiting until later today when HA 0.101 is released and using the Python ADB approach. Read the documentation (which will be updated when 0.101 is released), especially the ADB Troubleshooting section. In particular, you’ll need to stop your ADB server and restart your Fire TV device, otherwise the Python ADB connection won’t work.

1 Like

Yesterday evening it worked suddenly again with the adb server approach. I didn’t change anything.

But after the update I switched to the new python approach and it works great so far. But I still have a question: Is there some possibility to list custom apps with custom names in the Sources? The sources sometimes show all apps, sometimes only some and I’d like to rename them.

Thanks!

The sources list shows the running apps.

This pull request will use the friendly names instead of the app IDs: https://github.com/home-assistant/home-assistant/pull/28417

1 Like

Hi there all,
I just want to ask you all if it’s possible to link the Google Home Assistant to my
Android TV… I have a Philips Android TV.

The Google Home Assistens allready work on my Hassio with other commands.

This thread it’s about a lot of procedure and a lot of problems.
Is there any simple plugin in order to just have “Hey Google… turn on the TV”
Or maybe a step-by-step link on some web page.

Thanks in advance
Denis

I’ve got a problem retrieving the current state of my FireTV Stick. Getting states (playing, paused) for Netflix works very reliable (just with a little delay sometimes). But for prime video it’s going to paused very often though I’m still playing.

image
The reddish color is playing, green is paused. I’ve been playing a movie without pausing, though. the last part (saying “Spielt”) is played using netflix. As you can see, there are no false states.

Did anyone else experience this?

I also experience this with Prime Video on my Nvidia Shield. From what I saw it’s reporting different values for the wake_lock_size. Sometimes it’s 4 when it’s playing and then 2 when it’s paused then other times it’s 3 when it’s playing an 1 when it’s paused. Not sure why that app in particular reports different values. Those are used to detect the state of the app. I haven’t looked closely at the custom state detection to see if you can specify 2 different values for play/pause or not.

1 Like

You can use multiple values.

EDIT

This configuration is incorrect. See here: Native support for Android TV / Android devices

media_player:
  - platform: androidtv
    name: Android TV
    host: 192.168.0.222
    state_detection_rules:
      'com.amazon.tv.launcher':
        - 'playing':
            'wake_lock_size': 4  # this indentation is important!
            'wake_lock_size': 3  # this indentation is important!
        - 'paused':
            'wake_lock_size': 2  # this indentation is important!
            'wake_lock_size': 1  # this indentation is important!
        - 'standby'
2 Likes

Thanks @JeffLIrion! Will give this a try tonight to see if I can get Prime Video to report state better.

It’s giving me an error ‘duplicate entry for wake_lock_size’. So I’m trying

  - platform: androidtv
    host: 192.168.178.7
    name: "FireTV"
    adb_server_ip: 127.0.0.1
    state_detection_rules:
      'com.amazon.tv.launcher':
        - 'wake_lock_size':
            4: 'playing'  # this indentation is important!
            3: 'playing'   # this indentation is important!
            2: 'paused'   # this indentation is important!
            1: 'paused'   # this indentation is important!
        - 'standby'

as described in your thread here
Let’s see how this works out

2 Likes

You’re right. I edited my post to link to yours.

1 Like

I’m having an issue trying to find the correct name for the BBC app - iPlayer, when I pull the app list from my shieldTV it doesn’t show though:

com.nvidia.gfn_100030011/.Launch
com.nvidia.gfn_100182111/.Launch
com.ted.android.tv/.view.MainActivity
com.nvidia.gs_10534111/.Launch
com.plexapp.android/com.plexapp.plex.activities.SplashActivity
com.nvidia.remotelocator/.RemoteLocatorActivity
com.nvidia.tegrazone3/com.nvidia.tegrazone.MainActivity
com.android.documentsui/.LauncherActivity
de.cyberdream.androidtv.notifications.google/de.cyberdream.androidtv.notifications.StartActivity
com.silicondust.view/.App
com.plexapp.mediaserver.smb/com.plexapp.mediaserver.ui.main.MainActivity
com.android.vending/com.google.android.finsky.tvmainactivity.TvMainActivity
com.flipps.fitetv/com.bianor.ams.MainActivity
com.vevo/.MainActivity
com.google.android.tv/com.android.tv.TvActivity
com.nvidia.inputviewer/.AccessorySelectionActivity
com.nvidia.gfn_7785111/.Launch
com.nvidia.gs_17932211/.Launch
com.nvidia.gs_17939611/.Launch
com.nvidia.gs_17941411/.Launch
com.android.gallery3d/.app.GalleryActivity
tv.twitch.android.app/tv.twitch.android.apps.TwitchActivity
com.nvidia.ota/.ui.LauncherActivity
org.xbmc.kodi/.Splash
com.nvidia.gs_100005511/.Launch
com.nvidia.gs_100010811/.Launch
com.nvidia.gs_100030011/.Launch
com.nvidia.gs_100061111/.Launch
com.nvidia.gs_100075911/.Launch
com.nvidia.gfn_11154111/.Launch
com.limelight/.PcView
com.blade.shadowcloudgaming/com.blade.shadow.register.LoginActivity
com.google.android.youtube.tv/com.google.android.apps.youtube.tv.activity.ShellActivity
com.google.android.tv.remote.service/.settings.SettingsActivity
com.netflix.ninja/.MainActivity

Any UK users here have any idea?

“GET_PROPERTIES” seems to fail when I try…probably a formatting issue as I wanted to see if I could pull data when the actual BBC app was playing but not sure how to achieve this.

@JeffLIrion
This also doesn’t work, as the validator only allows one of these states as the key if the value is a dict. https://github.com/JeffLIrion/python-androidtv/blob/2c2e14021fc1bbea5e452e661bb57eaab78114bc/androidtv/basetv.py#L1160

Which causes a validation error on startup

Invalid config for [media_player.androidtv]: 'wake_lock_size' is not a valid state for the 'state_detection_rules' parameter for dictionary value @ data['state_detection_rules']['com.amazon.amazonvideo.livingroom']. Got [OrderedDict([('wake_lock_size', OrderedDict([(4, 'playing'), (3, 'playing'), (2, 'paused'), (1, 'paused')]))]), 'standby']. (See /config/configuration.yaml, line 164). Please check the docs at https://home-assistant.io/integrations/media_player.androidtv/
    state_detection_rules:
      "com.amazon.amazonvideo.livingroom":
        - "wake_lock_size":
            4: "playing"
            3: "playing"
            2: "paused"
            1: "paused"
        - "standby"

I think this is the way to do it.

media_player:
  - platform: androidtv
    name: Android TV
    host: 192.168.0.222
    state_detection_rules:
      'com.amazon.tv.launcher':
        - 'playing':
            'wake_lock_size': 4  # this indentation is important!
        - 'playing':
            'wake_lock_size': 3  # this indentation is important!
        - 'paused':
            'wake_lock_size': 2  # this indentation is important!
        - 'paused':
            'wake_lock_size': 1  # this indentation is important!
        - 'standby'
2 Likes

Look at the app_id attribute on the States page when the app is open.

I don’t know why "GET_PROPERTIES" would fail…

Thanks! That did the trick.