python-androidtv icon indicating copy to clipboard operation
python-androidtv copied to clipboard

Android TV 11 showing only Off, Idle or Standby, not recognizing running apps

Open blakadder opened this issue 2 years ago • 90 comments

I have a TCL TV with Android TV 11 installed running on latest release HA (2021.9.7) and I the only states HA receives are Off,Idle and Standby with adb response: null in Developer - States tab.

I ran the commands through adb shell and these are the results:

~$ adb shell dumpsys power | grep 'Display Power' | grep 'state=ON' || dumpsys power | grep 'mScreenOn=true'
Display Power: state=ON
~$ adb shell getprop ro.product.manufacturer
TCL
~$ adb shell getprop ro.product.model
Smart TV Pro
~$ adb shell getprop ro.build.version.release
11
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

gives no response

this works

~$ adb shell dumpsys window windows | grep Window\ #1
  Window #1 Window{f978c53 u0 com.google.android.youtube.tv/com.google.android.apps.youtube.tv.activity.MainActivity}:
~$ adb shell ps -A | grep u0_a
u0_a39          410   4071       0      0 0                   0 Z [com.tcl.usercen]
u0_a72         1029    274 1270008  83388 0                   0 S com.android.systemui
u0_a78         1229    274 1237900  48308 0                   0 S com.google.android.ext.services
u0_a22         1279    274 1303284  39828 0                   0 S com.google.android.tv.remote.service
u0_a70         1302    274 1223912  39352 0                   0 S com.google.android.partnersetup
u0_a15         1361    274 1353364  75584 0                   0 S com.google.android.gms.persistent
u0_a37         1433    274 1266488  51476 0                   0 S com.google.android.inputmethod.latin
u0_a15         1566    274 1238860  41872 0                   0 S com.google.process.gservices
u0_a77         1628    274 1240776  39884 0                   0 S com.android.providers.media.module
u0_a23         1751    274 1277568  76200 0                   0 S com.google.android.tvrecommendations
u0_a27         1772    274 1318284  55712 0                   0 S com.google.android.tvlauncher
u0_a12         1912    274 1255744  42656 0                   0 S com.android.providers.tv
u0_a6          2106    274 1219396  37888 0                   0 S com.android.externalstorage
u0_a59         2593    274 1906136  49788 0                   0 S com.tcl.bi:service
u0_a16         2653    274 2034692  39224 0                   0 S com.google.android.katniss:search
u0_a55         2783    274 1900044  55384 0                   0 S com.netflix.ninja
u0_a80         2859    274 1265992  54496 0                   0 S tv.twitch.android.app
u0_a16         3028    274 1991444  38792 0                   0 S com.google.android.katniss:interactor
u0_a19         3090    274 1252588  41664 0                   0 S com.google.android.tungsten.setupwraith
u0_a15         3147    274 1296252 102184 0                   0 S com.google.android.gms
u0_a11         3218    274 1375284 107356 0                   0 S com.google.android.apps.mediashell
u0_a79         3630    274 1807528 341556 0                   0 S org.xbmc.kodi
u0_a39         4071    274 1278092  43744 0                   0 S com.tcl.usercenter
~$ CURRENT_APP=$(adb shell dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
com.netflix.ninja
~$ CURRENT_APP=$(adb shell dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
com.tcl.waterfall.overseas
~$ CURRENT_APP=$(adb shell dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
com.android.vending
~$ CURRENT_APP=$(adb shell dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
tv.twitch.android.app

Here's full output of dumpsys window and dumpsys activity starter in a gist link

blakadder avatar Sep 22 '21 19:09 blakadder

To be honest, I have very little time to work on this package, and so it's mostly up to the community to contribute features and fixes.

This is something that needs to be done to unblock further development: https://github.com/JeffLIrion/python-androidtv/issues/255

With that in place, it would be easier to tailor commands as needed for different devices.

JeffLIrion avatar Sep 23 '21 04:09 JeffLIrion

I just updated my Sony Bravia TV to Android 10 and there is the same issue - the integration stopped working correctly.

konrad300 avatar Oct 15 '21 16:10 konrad300

I just updated my Sony Bravia TV to Android 10 and there is the same issue - the integration stopped working correctly.

Yes, me too. Wasn't expecting that Android 10 will not be compatible because only Android 11 had been mentioned in the issues / Home Assistant alert.

rensenti avatar Oct 16 '21 10:10 rensenti

Hi @blakadder and others, I have started working on Android 11 (maybe 10 as well) support; for starters very basic support is introduced here https://github.com/barmazu/python-androidtv/commit/c7b0a403cd3745f1e671ad63ad005da13f494d5e just by swapping commands that are send to ADB shell. It seems to restored some basic functionality, surely not all, but (some) states and apps are now reported correctly. Please note changes are not backward compatible (Android 9) yet. This is just test flight and I'm doing it on TCL C82 (A11). Further testing on different devices is welcomed, especially Android 10. Thanks!

obraz

source_list:
  - com.android.systemui
  - com.google.android.ext.services
  - com.google.android.tvrecommendations
  - com.google.android.tv.remote.service
  - Play Store
  - com.android.providers.media.module
  - com.google.android.katniss:interactor
  - com.tcl.inputmethod.international
  - Android TV Launcher
  - Spotify
  - com.android.providers.tv
  - com.google.android.katniss:search
  - Google Cast
  - Netflix
  - com.tcl.system.server
  - com.dolby.android.audio.service
  - com.google.android.apps.nbu.smartconnect.tv
  - com.tcl.usercenter
  - '[com.tcl.usercen]'
  - com.google.android.partnersetup
  - com.google.android.apps.mediashell:privileged_process0
  - com.google.android.gms
  - com.google.android.gms.persistent
  - com.google.process.gservices
  - com.android.vending:instant_app_installer
  - com.android.vending:background
  - com.google.android.permissioncontroller
  - com.google.android.gms.unstable
volume_level: 0.1
is_volume_muted: false
app_id: com.netflix.ninja
app_name: Netflix
source: Netflix
adb_response: >-
  {'screen_on': True, 'awake': True, 'audio_state': 'paused', 'wake_lock_size':
  3, 'current_app': 'com.netflix.ninja', 'media_session_state': 3,
  'audio_output_device': 'speaker', 'is_volume_muted': False, 'volume': 10,
  'running_apps': ['com.android.systemui', 'com.google.android.ext.services',
  'com.google.android.tvrecommendations',
  'com.google.android.tv.remote.service', 'com.android.vending',
  'com.android.providers.media.module', 'com.google.android.katniss:interactor',
  'com.tcl.inputmethod.international', 'com.google.android.tvlauncher',
  'com.spotify.tv.android', 'com.android.providers.tv',
  'com.google.android.katniss:search', 'com.google.android.apps.mediashell',
  'com.netflix.ninja', 'com.tcl.system.server',
  'com.dolby.android.audio.service',
  'com.google.android.apps.nbu.smartconnect.tv', 'com.tcl.usercenter',
  '[com.tcl.usercen]', 'com.google.android.partnersetup',
  'com.google.android.apps.mediashell:privileged_process0',
  'com.google.android.gms', 'com.google.android.gms.persistent',
  'com.google.process.gservices', 'com.android.vending:instant_app_installer',
  'com.android.vending:background', 'com.google.android.permissioncontroller',
  'com.google.android.gms.unstable'], 'hdmi_input': None}
hdmi_input: null
friendly_name: TCL Smart TV
supported_features: 23997

barmazu avatar Oct 19 '21 22:10 barmazu

So I've updated constants.py file, it should work on both domains, ie Android 9 and 11, see here: https://github.com/JeffLIrion/python-androidtv/compare/master...barmazu:a11_support?expand=1 You may update this file in your HA installation ( /usr/local/lib/python3.9/site-packages/androidtv/constants.py), test it out, report any issues... A few caveats though

  • HDMI input detection works on my TCL but will it work elsewhere (most likely it breaks Android 9 Sony TV)?
  • Volume slider does not work in Android 11 (it dropped media volume command support), service call does not work on TCL. Volume up / down buttons work.
  • Many other corner cases, depending on Android implementation, not covered - your mileage WILL vary :-)

I'll push PR here once I update the Unit Tests (HDMI Sony TV seems like a major break point now)

barmazu avatar Nov 01 '21 09:11 barmazu

@barmazu my comments in this post still stand true: https://github.com/JeffLIrion/python-androidtv/issues/271#issuecomment-925505211

The get_properties method needs to use multiple ADB commands, rather than one giant ADB command. This would enable using different commands for different devices, OS versions, etc.

IMO, that is the right approach to resolving this issue, whereas modifying the existing commands is merely a band-aid fix that could introduce new issues.

I really don't have time to work on this myself, so I need someone to take on this effort of

  1. splitting theget_properties method into multiple ADB shell commands, each of which is its own method
  2. enabling the use of different ADB commands depending on the device properties

JeffLIrion avatar Nov 01 '21 15:11 JeffLIrion

Hi @JeffLIrion! Agreed, I'm willing to do it but this is not a one day task, and honestly I didn't have much time either (to work around A11 support). With current implementation its awful, does not scale up at all, you don't have to say anything, this is obvious it would be very hard to maintain it; with still growing Android TV base it will be impossible to keep up.

Dirty patch I made is just unblocks me, maybe some other will take advantage and by any means does not replace necessary code refactoring. This is also my first meet with ATV from this side of things so I'm just looking around. Mostly.

barmazu avatar Nov 01 '21 19:11 barmazu

Hey,

I will take a look to this on next days, may be next w.e., to see if I can help to split get_properties method into multiple ADB shell commands as suggested by @JeffLIrion. If you have some additional suggestion on expected implementation report it here, otherwise I will create a PR when I will have something ready to discuss on.

ollo69 avatar Nov 03 '21 13:11 ollo69

I would like to test this on my android TV 10. I just don't know how to get it into my HA. Can anyone advise step by step installation?

jrousek avatar Jan 09 '22 11:01 jrousek

I would like to test this on my android TV 10. I just don't know how to get it into my HA. Can anyone advise step by step installation?

Replace the file /usr/local/lib/python3.9/site-packages/androidtv/constants.py with this one.

Beware, the mentioned file is inside the homeassistant docker container though. I haven't tried it, but it seems that the android11 update for the nvidia shield is right around the corner.

hillbicks avatar Jan 12 '22 14:01 hillbicks

Can confirm fork from @barmazu is working on Nvidia Shield TV 2017 with Android 11

It shows active app and state


source_list:
  - com.android.systemui
  - com.google.android.ext.services
  - com.nvidia.blakepairing
  - com.nvidia.shieldtech.accessoryui
  - com.nvidia.shield.remote.server
  - com.nvidia.osc
  - com.nvidia.app.messaging
  - com.android.providers.media.module
  - flar2.homebutton
  - net.dinglisch.android.taskerm
  - com.appgeneration.itunerfree
  - Spotify
  - com.nvidia.shield.ask
  - com.android.providers.tv
  - Google Cast
  - com.plexapp.mediaserver.smb:sambaserver
  - com.nvidia.tegrazone3:PersonalGridService
  - Kodi
  - com.google.android.gms.persistent
  - com.google.android.tv.remote.service
  - com.google.android.tvrecommendations
  - Android TV Launcher
  - Play Store
  - com.google.android.inputmethod.latin
  - com.google.android.gms
  - com.google.android.katniss:interactor
  - com.google.android.katniss:search
  - Netflix
  - com.plexapp.mediaserver.smb
  - com.google.process.gservices
volume_level: 0
is_volume_muted: false
app_id: com.netflix.ninja
app_name: Netflix
source: Netflix
adb_response: null
hdmi_input: null
friendly_name: Android TV 1
entity_picture: >-
  /api/media_player_proxy/media_player.android_tv_1?token=ac9bcf744cd043f64a3485120bd18537baad3e0b79fea408e726bc17f73a3ca8&cache=1642029753.262722
supported_features: 23997

grafik

deviant-aut avatar Jan 12 '22 23:01 deviant-aut

I would like to test this on my android TV 10. I just don't know how to get it into my HA. Can anyone advise step by step installation?

Replace the file /usr/local/lib/python3.9/site-packages/androidtv/constants.py with this one.

Beware, the mentioned file is inside the homeassistant docker container though. I haven't tried it, but it seems that the android11 update for the nvidia shield is right around the corner.

Do you know where i have to put the file in HassOS? The path u send doesnt exist :(

Mangomc avatar Jan 13 '22 09:01 Mangomc

run "docker exec -it homeassistant bash" to launch terminal in container. There you will find the path.

deviant-aut avatar Jan 13 '22 10:01 deviant-aut

I've logged into hassio through SSH and unfortunately i don't have a folder Pyhton inside lib only perl5 is available : image

Do i have to create it ? (i've already Android TV integration though)

Moustik2002 avatar Jan 13 '22 14:01 Moustik2002

I figured out I needed to use "docker cp" command from outside the container to copy the new file over from the OS to within the docker. I use HA Core.

Iceman248 avatar Jan 13 '22 14:01 Iceman248

This is 90% reliable for me with my Nvidia shield on Android 11. The only issue I'm encountering (for my needs) is that every so often the media player state will randomly change to standby resulting in my lighting increasing in brightness.

tungmeister avatar Jan 13 '22 19:01 tungmeister

@tungmeister wake_lock_size and/or media_session_state change when this happens?

barmazu avatar Jan 13 '22 21:01 barmazu

@tungmeister wake_lock_size and/or media_session_state change when this happens?

next time it happens I'll try and get the details.

tungmeister avatar Jan 13 '22 21:01 tungmeister

@barmazu here's the response when watching youtube, hopefully I timed this correctly.

Looks good, but how this is different when you see "playing" state? How often state shifts from "playing" to "standby"? Does it stay for long in incorrect state? Have you tried to use custom state detection? So far I haven't seen such case on my TCL TV but I'll try to update Shield and see how that goes. Maybe this is specific to Android 11 implementation by Nvidia, who knows.

barmazu avatar Jan 13 '22 22:01 barmazu

@barmazu here's the response when watching youtube, hopefully I timed this correctly.

Looks good, but how this is different when you see "playing" state? How often state shifts from "playing" to "standby"? Does it stay for long in incorrect state? Have you tried to use custom state detection? So far I haven't seen such case on my TCL TV but I'll try to update Shield and see how that goes. Maybe this is specific to Android 11 implementation by Nvidia, who knows.

it lasts for about 10 seconds or so. The problem as as soon as I "GET_PROPERTIES" that returns the right wait lock and session state so the lights dim again. It's happened maybe 8 times this evening. (4-5 hours of playback)

tungmeister avatar Jan 13 '22 22:01 tungmeister

OK, that quite often I'd say. Would you be so kind and check updated audio_state detection cmds? Maybe it help as it also takes part in state detection? Comment out old CMD_AUDIO_STATE and paste below:

CMD_AUDIO_STATE_RAW = "CURRENT_AUDIO_STATE=$(dumpsys audio | sed -r -n '/[0-9]{2}-[0-9]{2}.*player piid:.*state:.*$/h; ${x;p;}') && "
CMD_AUDIO_STATE = CMD_AUDIO_STATE_RAW + r"echo $CURRENT_AUDIO_STATE | grep paused >/dev/null 2>&1 && echo -e '1\c' || { echo $CURRENT_AUDIO_STATE | grep started >/dev/null 2>&1 && echo '2\c' || echo '0\c' ; }"

It will evaluate the very last audio player state entry (as seen by logs) which should be most recent app playing audio... Regarding PROPERTIES catching problem, just add adb_command to your dim automation :D

Thanks!

barmazu avatar Jan 13 '22 23:01 barmazu

Sorry to bother you guys but, could somebody explain me how to be able to see folder Pyhton inside lib ?

Moustik2002 avatar Jan 13 '22 23:01 Moustik2002

Sorry to bother you guys but, could somebody explain me how to be able to see folder Pyhton inside lib ?

Not sure if same for you, but I didn't have a "local" folder with the Core Docker install. I had to skip that directory.

Iceman248 avatar Jan 13 '22 23:01 Iceman248

Thx @Iceman248 indeed skipping local fix it !

Moustik2002 avatar Jan 13 '22 23:01 Moustik2002

@barmazu I've updated the constants file, I'll do some further testing after I finish work today.

tungmeister avatar Jan 14 '22 09:01 tungmeister

root@cupboard-domotica:/srv/homeassistant/lib/python3.9/site-packages/androidtv# ls -l
total 112
-rw-r--r-- 1 homeassistant homeassistant  3979 Jul  9  2021 __init__.py
drwxr-xr-x 2 homeassistant homeassistant  4096 Jan 14 11:26 __pycache__
drwxr-xr-x 3 homeassistant homeassistant  4096 Jul  9  2021 adb_manager
drwxr-xr-x 3 homeassistant homeassistant  4096 Jul  9  2021 androidtv
drwxr-xr-x 3 homeassistant homeassistant  4096 Jul  9  2021 basetv
-rw-r--r-- 1 homeassistant homeassistant 25076 Jan 14 11:25 constants.py
-rw-r--r-- 1 homeassistant homeassistant 23103 Jan 14 11:25 constants.py.backup
-rw-r--r-- 1 homeassistant homeassistant 25076 Jan 14 11:16 constants.py.new
-rw-r--r-- 1 homeassistant homeassistant   139 Jul  9  2021 exceptions.py
drwxr-xr-x 3 homeassistant homeassistant  4096 Jul  9  2021 firetv
-rw-r--r-- 1 homeassistant homeassistant  3017 Jul  9  2021 setup_async.py
root@cupboard-domotica:/srv/homeassistant/lib/python3.9/site-packages/androidtv#
root@cupboard-domotica:/srv/homeassistant/lib/python3.9/site-packages/androidtv# service homeassistant stop
root@cupboard-domotica:/srv/homeassistant/lib/python3.9/site-packages/androidtv# service homeassistant start

This seems to be working for me, using Homeassistant Core (on Debian 10 in venv) with Nvidia Shield on AndroidTV 11. Thanks for the fix!

megapearl avatar Jan 14 '22 10:01 megapearl

This is 90% reliable for me with my Nvidia shield on Android 11. The only issue I'm encountering (for my needs) is that every so often the media player state will randomly change to standby resulting in my lighting increasing in brightness.

Having this 'problem' also

megapearl avatar Jan 14 '22 10:01 megapearl

@megaperl have you tried the different audio state queries above? I can't test until this evening (UK)

tungmeister avatar Jan 14 '22 11:01 tungmeister

@megaperl have you tried the different media state queries above? I can't test until this evening (UK)

Not yet, I can test it when I'm finished working and back home. (NL)

megapearl avatar Jan 14 '22 11:01 megapearl

Thx @Iceman248 indeed skipping local fix it !

How to skip it? ssh

jrousek avatar Jan 14 '22 13:01 jrousek