Native support for Android TV / Android devices

hi, so I got the solution. After killing my friend with questions :slight_smile: I bumped to file called core.entity_registry and as a final attempt searched … and found some old entry regarding nvidia_shield. So I stoped HA from ssh / edited the file / started HA … and hahaha new entity was created right after restart. All is great I can move to next step integration -> https://github.com/mcfrojd/hassio_lovelace_multiremote

C U later in this automation adventure :slight_smile:

1 Like

To replace my older wall mounted Lenovo 10 inch tablet I bought one of these cheap China clones (LNMBBS P40). The tablet can be turned on and off with androidtv but no apps are found. Also the currently launched app is not displayed. This is the output of adb_response the calculator app is started but not recognized:

{'screen_on': True, 'awake': True, 'audio_state': 'playing', 'wake_lock_size': 0, 'current_app': None, 'media_session_state': None, 'audio_output_device': None, 'is_volume_muted': None, 'volume': None, 'running_apps': None, 'hdmi_input': None}

In another thread I found the custom query for Google TV Devices,
which is also stored in constants.py:

#: Get the current app
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"

#: Get the current app for a Google TV device
CMD_CURRENT_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"

this GOOGLE_TV query works on my tablet:

P40-EEA:/ $ bash-5.0# adb -s 192.168.1.56 shell
P40-EEA:/ $ CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
com.google.android.calculator

How does androidtv distinguish if it is a Google device or a ‘normal’ Android device?
What can I do, so that my tablet is recognized as a Google TV device?

I updated docker image to the current Homeassistant version 2021.2.3, but unfortunately with the same result. My current androidtv version

androidtv in /usr/local/lib/python3.8/site-packages (0.0.57)

The tablet has Android version 10 installed.
I have several Android phones, tablets and fire tv’s they all work fine, but they all have an Android version lower than 10. Maybe that makes a difference

My media_player entry in configuration.yaml looks like this:

media_player:
  - platform: androidtv
    name: LNM Tablet
    device_class: androidtv
    host: !secret ip_lnm_tablet
    adb_server_ip: !secret ip_localhost
    turn_on_command: !secret lnm_tablet_unlock
    turn_off_command: "input keyevent POWER"

Ok, I have managed it for myself.
I changed the two queries “CMD_CURRENT_APP” and “CMD_LAUNCH_APP” in constants.py.
When the default android “dumpsys window” query fails, the Google TV “dumpsys activity” query is executed. Dirty I know, but it works. :wink: and maybe it helps someone who has the same problem.

#: Get the current app
#CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus || dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"

#: Launch an app if it is not already the current app
#CMD_LAUNCH_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"
CMD_LAUNCH_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus || dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi"

This certainly was a good amount of time ago. However, were you ever able to fix this issue? It seems I am being affected by it as well and have tried repeatedly to no avail.

I’m running HASSIO with 3 NVIDIA Shield boxes in my house. Integration is working fine…I want to be able to build a simple entity card that shows the current status of the boxes: (Playing, Off, Standby, Idle, etc.) and nothing more…When I use the entity card I get that and all the other player controls…Is there a way to just isolate the status?

Try a template sensor and show that with your entity-card. :slight_smile:

1 Like

Thanks for reminding me! I couldn’t think of where to begin! Just created one and it works great!

any solution?

You’re very welcome, sometimes you just can’t see the forest for the trees. :slight_smile:

You might want to take a look just three posts after the question, that’s one place where answers would likely be given… :wink:

But here:

Is there anything wrong with this configuration? I created the following media player in the configuration:
media_player:

  • platform: androidtv
    name: Nvidia S.H.I.E.L.D TV Pro
    host: 192.168.100.116

However, the following was created:
media_player.shield

I’d say change it to NVIDIA SHIELD TV PRO - remove the punctuation

I changed the name again but did not correct the name of the media player.
Is it necessary to edit the cory entry? Never did it…

The name change will take affect after a reboot, and it should replace the old entity name.

This is my own config:

  - platform: androidtv    # uses adb
    name: Sitting Room Android TV
    host: 192.168.1.56
    adbkey: "/config/.storage/androidtv_adbkey_sittingroom"

In my entity list it appears like this: media_player.sitting_room_android_tv

The adbkey is optional, I have it because I have multiple devices and want to keep the keys in specific locations.

You will need to have ADB properly enabled on the TV, and keep an eye out for a permissions popup on the TV when you reboot Home Assistant for the first time after adding an androidtv.

I have already changed the name of the entity several times and restarted the hass, however the name is not changed.

You’ll need to have adb installed in home assistant or on your host before anything will work

My config:

media_player:
  - platform: androidtv
    name: "Shield TV"
    host: 192.168.0.160
    apps:
      com.google.android.tvlauncher: "Home"
      com.google.android.youtube.tv: "Youtube"
      com.amazon.amazonvideo.livingroom: "Amazon Prime Video"
      com.plexapp.android: "Plex"
      com.netflix.ninja: "Netflix"
    adb_server_ip: 127.0.0.1
    adb_server_port: 5037

Try also restart shield / disable / enable DEbug on shield. change IP so it will be discovered as new device.

Also remember - ADB_server is not mandatory, if you do not use it, you will use by default python adb integration. It works in my case with no problems.

Hi I cant seem to understand that from this thread, is it possible to get a state detection for the TV using this component or others?

I control my lights in my living room based on the state, e.g.

Paus/idle lights on 100%

Play lights off

Since moving to android TV (from Kodi) I can no longer get the current state and therefore not able to control lights, any ideas on how to get this working?

Thank you for your help.

EDIT: This works just perfect, as is, just needed to do a custom wake_lock setting, see below

Thank you for this component, it works great, I just want to share some wake_lock settings for svtplay for those who need:

I am not sure about the last “standby”, but it seems to work as I want it now, detecting both play and pause. Since I changed the polling interval I also had to handle the wake_lock_state when you adjust volume.

    state_detection_rules:
      'se.svt.android.svtplay':
        - 'paused':
            'wake_lock_size': 3 #Paus
        - 'playing':
            'wake_lock_size': 4 #Play
        - 'playing':
            'wake_lock_size': 5 #Volume adjust
        - 'standby'

Hi, it seems like they update the SVT play app and there are some major changes…

This is the new settings:

state_detection_rules:
  'se.svt.android.svtplay':
    - 'paused':
        'wake_lock_size': 3 #Paus
    - 'playing':
        'wake_lock_size': 4 #Play

But there is a bug, it seems that the app is randomly switching state between wake lock 3 and 4, even thou the app is streaming the video just fine…

Anyone know how to fix it?