home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.54k stars 30.72k forks source link

Android TV: Currently running app parsing broken #69723

Closed TannerBrunscheon closed 2 years ago

TannerBrunscheon commented 2 years ago

The problem

The new androidtv integration update has made my Nvidia Shield always report back the app playing as "InputMethod}:". It also misses state detection rules.

What version of Home Assistant Core has the issue?

core-2022.4.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

androidtv

Link to integration documentation on our website

https://www.home-assistant.io/integrations/androidtv/

Diagnostics information

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.4.1",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.9.9",
    "docker": true,
    "arch": "x86_64",
    "timezone": "America/Chicago",
    "os_name": "Linux",
    "os_version": "5.10.108",
    "supervisor": "2022.03.5",
    "host_os": "Home Assistant OS 7.6",
    "docker_version": "20.10.9",
    "chassis": "vm",
    "run_as_root": true
  },
  "custom_components": {
    "holidays": {
      "version": "1.7.2",
      "requirements": [
        "python-dateutil>=2.8.2",
        "holidays>=0.13"
      ]
    },
    "pirateweather": {
      "version": "0.1.0",
      "requirements": [
        "python-forecastio==1.4.0"
      ]
    },
    "google_home": {
      "version": "1.9.10",
      "requirements": [
        "glocaltokens==0.6.3",
        "google-api-python-client==2.38.0"
      ]
    },
    "spotcast": {
      "version": "v3.6.27",
      "requirements": [
        "spotify_token==1.0.0"
      ]
    },
    "localtuya": {
      "version": "3.2.1",
      "requirements": []
    },
    "nodered": {
      "version": "1.0.7",
      "requirements": []
    },
    "hacs": {
      "version": "1.24.3",
      "requirements": [
        "aiogithubapi>=21.11.0"
      ]
    },
    "adaptive_lighting": {
      "version": "1.0.14",
      "requirements": []
    }
  },
  "integration_manifest": {
    "domain": "androidtv",
    "name": "Android TV",
    "documentation": "https://www.home-assistant.io/integrations/androidtv",
    "requirements": [
      "adb-shell[async]==0.4.2",
      "androidtv[async]==0.0.66",
      "pure-python-adb[async]==0.3.0.dev0"
    ],
    "codeowners": [
      "@JeffLIrion",
      "@ollo69"
    ],
    "config_flow": true,
    "iot_class": "local_polling",
    "loggers": [
      "adb_shell",
      "androidtv",
      "pure_python_adb"
    ],
    "is_built_in": true
  },
  "data": {
    "entry": {
      "entry_id": "42b4d4fa6c2de81599a71d3558da6ed1",
      "version": 1,
      "domain": "androidtv",
      "title": "192.168.50.124",
      "data": {
        "host": "192.168.50.124",
        "device_class": "auto",
        "port": 5555
      },
      "options": {
        "get_sources": true,
        "exclude_unnamed_apps": false,
        "screencap": true,
        "apps": {
          "com.liskovsoft.smarttubetv.beta": "Youtube",
          "me.efesser.flauncher": "Home"
        },
        "state_detection_rules": {
          "me.efesser.flauncher": [
            "idle"
          ]
        }
      },
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "unique_id": "**REDACTED**",
      "disabled_by": null
    },
    "device_properties": {
      "device_class": "androidtv",
      "manufacturer": "NVIDIA",
      "model": "SHIELD Android TV",
      "serialno": "**REDACTED**",
      "sw_version": "11",
      "ethmac": "**REDACTED**",
      "wifimac": "**REDACTED**"
    },
    "device": {
      "area_id": "living_room",
      "config_entries": [
        "42b4d4fa6c2de81599a71d3558da6ed1"
      ],
      "configuration_url": null,
      "connections": "**REDACTED**",
      "disabled_by": null,
      "entry_type": null,
      "id": "f128690e3afe01dd999ef9f8ce5a0fd9",
      "identifiers": "**REDACTED**",
      "manufacturer": "NVIDIA",
      "model": "SHIELD Android TV (Android TV)",
      "name_by_user": null,
      "name": "Android TV 192.168.50.124",
      "suggested_area": null,
      "sw_version": "11",
      "hw_version": null,
      "via_device_id": null,
      "is_new": false,
      "entities": {
        "media_player.android_tv_192_168_50_124": {
          "unique_id": "**REDACTED**",
          "platform": "androidtv",
          "area_id": null,
          "capabilities": {},
          "config_entry_id": "42b4d4fa6c2de81599a71d3558da6ed1",
          "device_class": null,
          "device_id": "f128690e3afe01dd999ef9f8ce5a0fd9",
          "domain": "media_player",
          "disabled_by": null,
          "entity_category": null,
          "hidden_by": null,
          "icon": null,
          "id": "b07811a73e22341f3809f32cea8c4491",
          "name": null,
          "options": {},
          "original_device_class": null,
          "original_icon": null,
          "original_name": "Android TV 192.168.50.124",
          "supported_features": 23997,
          "unit_of_measurement": null,
          "state": {
            "state": "playing",
            "attributes": {
              "source_list": [
                "com.foxsports.android",
                "com.google.android.gms.persistent",
                "com.android.systemui",
                "Spotify",
                "com.google.android.tv.remote.service",
                "com.nvidia.blakepairing",
                "com.dolby.android.audio.service",
                "com.nvidia.shieldtech.accessoryui",
                "com.nvidia.shield.remote.server",
                "com.nvidia.osc",
                "com.nvidia.app.messaging",
                "com.android.providers.media.module",
                "Youtube",
                "com.google.process.gservices",
                "com.nvidia.tegrazone3",
                "Play Store",
                "com.google.process.gapps",
                "com.android.vending:background",
                "com.nvidia.tegrazone3:telemetry",
                "com.nvidia.tegrazone3:PersonalGridService",
                "com.android.providers.tv",
                "com.google.android.tvrecommendations",
                "Home",
                "com.nvidia.shield.ask",
                "com.google.android.ext.services",
                "com.irishin.buttonsremapper",
                "com.google.android.leanback.ime",
                "com.google.android.katniss:interactor",
                "com.google.android.gms",
                "Google Cast",
                "com.google.android.katniss:search"
              ],
              "volume_level": 1.0,
              "is_volume_muted": false,
              "app_id": "InputMethod}:",
              "app_name": "InputMethod}:",
              "source": "InputMethod}:",
              "adb_response": null,
              "hdmi_input": null,
              "entity_picture": "/api/media_player_proxy/media_player.android_tv_192_168_50_124?token=8e0267e2b1651c1df436956a24ec29934f860217db5943e3889edd58ac60a866&cache=1649479274.991632",
              "friendly_name": "Android TV 192.168.50.124",
              "supported_features": 23997
            },
            "last_changed": "2022-04-09T04:37:05.363656+00:00",
            "last_updated": "2022-04-09T04:41:14.991708+00:00"
          }
        }
      }
    }
  }
}

Example YAML snippet

This was set up using Add Integration

Anything in the logs that might be useful for us?

No response

Additional information

No response

rccoleman commented 2 years ago

I agree that the only way to really address the parade of incompatible changes coming from AndroidTV is to make the commands more easily customizable. I dug into this a bit today when it started happening for me and it looks like one of the issues is that the current app is being extracted from Window #1 in the adb output, and the window numbers don't seem to be a reliable way to reference the current running app. I solved it for me in a couple of different ways, with only one required and both with the same result.

CURRENT_APP=$(dumpsys window windows | grep 'mInputMethodTarget') && 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 {'

There may very well be a reason that Window #1 was chosen to search for in the adb output, and I have no idea if the change that I made will continue to be reliable. Just throwing it out there if someone else wants to try it.

The inspiration came from here, where you can see how often this changes and why it's so hard to keep it working.

I'm using an nVidia Shield running AndroidTV 11 and v9.1.0 of the Shield software.

mouth4war commented 2 years ago

So it varies by Android tv version? Maybe it can be hard-coded by version then.

rccoleman commented 2 years ago

It already is, but it's not maintainable to support all the variations.

mouth4war commented 2 years ago

Well we can only try to cover as much as we can. You've managed to make it work for the latest version so that's great. Future versions will be managed as they come out.

I hope this fix and the other fix/feature for listing all installed apps as sources makes it into core eventually. (https://github.com/home-assistant/core/issues/45611) Long story short, PR was rejected 2 years ago when config flow was missing but should be easier now that config flow exists, right?

igloher commented 2 years ago

I have the same problem with a Philips Oled706 recently update to Android 11. app_id: InputMethod}: app_name: InputMethod}: source: InputMethod}: I have Home Assistant 2022.9.2 Is there nothing to solve this problem yet? Thanks!

rccoleman commented 2 years ago

I have the same problem with a Philips Oled706 recently update to Android 11.

app_id: InputMethod}:

app_name: InputMethod}:

source: InputMethod}:

I have Home Assistant 2022.9.2

Is there nothing to solve this problem yet? Thanks!

Aside from the workaround that I described? It's been working fine for me.

igloher commented 2 years ago

I have the same problem with a Philips Oled706 recently update to Android 11. app_id: InputMethod}: app_name: InputMethod}: source: InputMethod}: I have Home Assistant 2022.9.2 Is there nothing to solve this problem yet? Thanks!

Aside from the workaround that I described? It's been working fine for me.

I have tried your solution, but it doesn't solve the problem. However, the following command did it. I found this command here: https://community.home-assistant.io/t/new-chromecast-w-android-tv-integration-only-showing-as-off-or-idle/234424/66

CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP

sammyke007 commented 2 years ago

I have the same problem with a Philips Oled706 recently update to Android 11. app_id: InputMethod}: app_name: InputMethod}: source: InputMethod}: I have Home Assistant 2022.9.2 Is there nothing to solve this problem yet? Thanks!

Aside from the workaround that I described? It's been working fine for me.

I have tried your solution, but it doesn't solve the problem. However, the following command did it. I found this command here: https://community.home-assistant.io/t/new-chromecast-w-android-tv-integration-only-showing-as-off-or-idle/234424/66

CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP

Where did you exactly change this please?

igloher commented 2 years ago

I have the same problem with a Philips Oled706 recently update to Android 11. app_id: InputMethod}: app_name: InputMethod}: source: InputMethod}: I have Home Assistant 2022.9.2 Is there nothing to solve this problem yet? Thanks!

Aside from the workaround that I described? It's been working fine for me.

I have tried your solution, but it doesn't solve the problem. However, the following command did it. I found this command here: https://community.home-assistant.io/t/new-chromecast-w-android-tv-integration-only-showing-as-off-or-idle/234424/66 CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP

Where did you exactly change this please?

I ´ve installed AndroidTV Custom from this repo: I ´ve configured the Android TV Custom integration and then i put the command on current_app_media_session_state field. After that I can correctly see app_id, app_name and source in my home assistant

shooto74 commented 2 years ago

The command also allowed me to see the running apps - thanks for sharing Igloher .

My TV is the following - Sony A80J OLED - Android 9. This is the first time I have successfully seen the running apps within HA after many months trying all sorts of possible solutions.

I used the Android TV Custom Integration rather than the "Android TV" integration.

Here are the steps I followed within HA,

Settings devices and services Android Custom Integration chose my media device "Sony OLED" configure configure ADB shell custom commands followed by current_app_media_session_state - click submit In the blank box in the centre of the window, I pasted the command shared by Igloher

CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP

Submit Submit, again Message appeared that options were saved successfully, click on finish

After this I ran Kodi, Disney++, Netflix to test and under developer tools / states it showed the currently running app & also via the media player in my HA dashboard.

The only issue which still does not work correctly for me is the state of the running app - either it is idle / paused when an app is opened and something is playing. This is the main feature I wish to work, like probably many others, to have lights dim/off when a film is playing at night & come back on when the film is paused/stopped.

If anyone has an idea of an addition to the command to display this information, please share, thanks.

sammyke007 commented 2 years ago

Ok, I did it different, but it also works perfectly fine now for my Philips 65OLED806 on Android 11:

With File editor in HASS I've opened the following file: /config/custom_components/androidtv/androidtv11hack/constants.py

I've replaced line 99 "CURRENT_APP=$(dumpsys window windows | grep 'Window #1') && " + CMD_PARSE_CURRENT_APP11 with "CURRENT_APP=$(dumpsys window windows | grep 'mInputMethodTarget') && " + CMD_PARSE_CURRENT_APP11

Restart HASS and voila!

Tnx for the feedback!

MartijnGP commented 2 years ago

Another workaround is to install Tasker on the TV. Several ways to get the APK where it needs to be. Connect a BT keyboard (or you'll hate yourself) and set up Tasker to run a task when the running app changes. Do a HTTP request to the RESTful API with the app name.

Using Tasker to talk to the IPA gives much more options anyway and much more reliable than this integration. I can for example wake my gaming PC with Steam Link when my controller connects to the TV. Get information about what's playing, battery level of the remote, and so on.

toti787 commented 2 years ago

I too don’t have the Android TV Notifications app installed. Not sure what else it could be. I have the typical apps installed. Has anyone found the other apps that break it?

I bought my Shield TV Pro today, set it up with Home Assistant and have the same issue. I installed no applications so probably one of default apps causing it. Home Assistant is running the newest version

i have discovered that Google Assistant is the cause, if you reboot the shield everything works, and after you open assistant it goes to

AssistPreviewPanel}:

It says AssistPreviewPanel}; for me now, not sure what changed though.

and after it closes InputMethod}: stays on forever until you reboot