Native support for Android TV / Android devices

I found and fixed a typo in the code. Please update and let me know how it’s working.

Hi,
I have updated androidtv under custom_components to the newest version and unfortunately I encounter:

  • after clicking three dots volume buttons are responsive but after the entire window freezes
  • history state is not loaded
  • when TV is switched off it still becomes unavailable and cannot restore the connection even if it is switched on (remains still unavailable), below log keeps repeating in the time interval defined for adb add on

2019-09-07 04:22:50 ERROR (SyncWorker_4) [custom_components.androidtv.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ERROR: 'FAIL' 0006closed

Hope that helps

I’m not sure why the window freezes. If the device becomes unavailable after you click the volume up/down button, I suppose that could be the reason.

Do you have the recorder component setup to record the history for this media player?

I’ve seen a previous case of this issue. It’s an ADB-related thing, and I don’t know how to fix it. You could open an issue here: pure-python-adb. You could try restarting the ADB add-on.

Hello. Started using the integration for a Mi Box S on hassio recently and the first few days everything worked great. I kept messing with the HA instance (I can’t remember if I changed anything specific to the Android TV media player but I doubt it) and now I can’t turn on my MI Box from HA anymore. I’m new here but from what o remember, when first installed, the component wouldn’t actually turn off the box, but rather put in in an idle state, where it would still be accessible. Now it simply puts the Mi Box to sleep, which disconnects the wifi, so it’s no longer accessible via adb. Is this a know issue? Does anyone know of any fix for it? It’s kinda driving me crazy. Thanks!

1 Like

It seems there’s a problem with this compoment in the latest Home Assistant releases. On version 0.99.1 and 0.99.2 the state of my Nvidia Shield keeps getting lost and de adb_response constantly goes to “null”. When i switch back to Home Assistant 0.98.5 the problems are gone. (although the states update slowly). Is this a known problem ? Is anybody else having this problem.

I’m running Home Assistant in a docker on Ubuntu. ADB runs locally on Ubuntu. I also downloaded the latest verison of ADB (1.041) from Google but that doesn’t solve the problem.

The only way to get it working stable is switching back to 0.98.5.

The adb_response attribute is supposed to be null until you send an ADB command via the androidtv.adb_command service that returns some text.

Post a log with debug info and I’ll take a look. Run the logger.set_level service with the following parameters:

{"androidtv": "debug", "homeassistant.components.androidtv": "debug"}

I’ve enabled logger and set the level to debug with the command you posted. I’m looking at the log with tail -f but i’m seeing a lot of data. Do you need specific lines ?

Here’s one line after starting the Kodi app :

2019-09-22 08:40:07 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.140136448640912] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=media_player.nvidia_shield_tv, old_state=<state media_player.nvidia_shield_tv=standby; volume_level=1.0, is_volume_muted=False, app_id=org.xbmc.kodi, app_name=Kodi, source=hdmi, adb_response=Starting: Intent { cmp=org.xbmc.kodi/.Splash }, friendly_name=Shield TV Android, supported_features=21945 @ 2019-09-22T08:32:52.185407+02:00>, new_state=<state media_player.nvidia_shield_tv=standby; volume_level=1.0, is_volume_muted=False, source=hdmi, adb_response=Starting: Intent { cmp=org.xbmc.kodi/.Splash }, friendly_name=Shield TV Android, supported_features=21945 @ 2019-09-22T08:32:52.185407+02:00>>}

And when i look at the state it keeps changing from this :

volume_level: 1
is_volume_muted: false
source: hdmi
adb_response: Starting: Intent { cmp=org.xbmc.kodi/.Splash }
friendly_name: Shield TV Android
supported_features: 21945

To this :

volume_level: 1
is_volume_muted: false
app_id: org.xbmc.kodi
app_name: Kodi
source: hdmi
adb_response: Starting: Intent { cmp=org.xbmc.kodi/.Splash }
friendly_name: Shield TV Android
supported_features: 21945

As you can see the app_name attribute disappears. When i switch back to HA 0.98.5 it doesn’t happen.

It looks like you set the log level for everything to debug. I only want the androidtv package and component set to debug. The rest of the log levels should be something like error.

My configuration.yaml only contains logger:
And i ran the command you posted.

I changed to logger settings. Now it only shows critical errors and androidtv is set do debug.

2019-09-22 14:48:24 DEBUG (SyncWorker_3) [androidtv.adb_manager] Sending command to 192.168.2.96:5555 via ADB server 192.168.2.5: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 audio | grep -v 'Buffer Queue' | grep -q paused && echo -e '1\c' || (dumpsys audio | grep -v 'Buffer Queue' | grep -q started && echo '2\c' || echo '0\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) && dumpsys audio | grep '\- STREAM_MUSIC:' -A 12
2019-09-22 14:48:24 DEBUG (SyncWorker_3) [androidtv.androidtv] Android TV 192.168.2.96:5555 update response: 11Failed to write while dumping service audio: Broken pipe
1Wake Locks: size=3
com.google.android.youtube.tv
      state=PlaybackState {state=3, position=90, buffered position=0, speed=1.0, updated=162380181, actions=362, custom actions=[], active item id=-1, error=null}
- STREAM_MUSIC:
   Muted: false
   Min: 0
   Max: 15
   Current: 2 (speaker): 15, 4 (headset): 7, 8 (headphone): 7, 80 (bt_a2dp): 9, 400 (hdmi): 15, 1000 (digital_dock): 9, 4000000 (usb_headset): 3, 40000000 (default): 15
   Devices: hdmi
- STREAM_ALARM:
   Muted: false
   Min: 1
   Max: 7
   Current: 2 (speaker): 7, 4 (headset): 3, 8 (headphone): 3, 80 (bt_a2dp): 4, 400 (hdmi): 7, 1000 (digital_dock): 4, 4000000 (usb_headset): 1, 40000000 (default): 7
   Devices: speaker, hdmi
- STREAM_NOTIFICATION:

Thanks for the log info. This custom component should fix it (HACS compatible): androidtv

1 Like

Yes ! Problems are gone ! Thx for the update.

Thanks, will this fix eventually be rolled out in the regular HA updates so that a custom component is no longer required?

Exact same issue I am having! I am on .99.2

Yes. I submitted the pull request yesterday, it should be in the next release.

https://github.com/home-assistant/home-assistant/pull/26906

You rock! By the way, your code works perfect!

@JeffLIrion Did anything change recently with the androidtv component? I am not able to connect with FireTV. It works fine with Shield.

Nothing that should break it. If it works for an Android TV device, it should work for a Fire TV device. Got a log?

I suggest trying this custom component with the Python ADB approach (HACS compatible): https://github.com/JeffLIrion/ha-androidtv

More info: [TESTERS NEEDED] Android TV / Fire TV key generation

(If you have an existing ADB key and don’t want to test the key generation functionality, you can include adbkey in your config.)