Native support for Android TV / Android devices

@JeffLIrion
I tried your ha-android ,
I found that when my android tv box is turned off the media player ui turns to dark blue
And when it is on or idle it stays sky blue,

Exactly opposite to regular other media player.

Can you please check or any body confirm it.
Thanks

First of all, I don’t think that custom component was working as expected due to how HA now manages the requirements for custom components. I think I fixed it now, but that custom component is really just for testing. You should use the standard component.

If the media player is dark blue when the device is off, that’s probably because the device is completely off (not just sleeping), and so the ADB connection is broken and the media player is unavailable.

If it’s sky blue when the device is on, that’s probably because the state is Idle. State detection is a known issue that requires help from the community in order to improve it. In the forthcoming version of the component, there is a fix in the androidtv package that improves how the media session state property is determined, and that could help with the state detection.

Aside from that, there is an open issue regarding state detection and pull requests are welcome!

Hi, I’ve got the androidtv component working well with my Nvidia Shield.

Just got one question though. Is there a way to increase the poll delay to the device in order to retrieve its state in near real time? Reason is that I want to be able to dim lights on playback and increase their brightness to full on pause, but the time it takes to effect this can be from 1 to around 9 or so seconds. I haven’t timed it multiple times to come up with figures. I would imagine that HA polls the device for its state every few seconds or so. Ideally, it would be great if the API allowed the component to receive state updates in real time. :slight_smile:

You can try this: https://www.home-assistant.io/docs/configuration/platform_options#scan-interval

My android TV integration does’t work after restart. I didn’t change anything TV related just some MQTT sensors, TV is connected to ADB.

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 126, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 106, in setup_platform
    device_class=config[CONF_DEVICE_CLASS])
  File "/usr/local/lib/python3.7/site-packages/androidtv/__init__.py", line 34, in setup
    return AndroidTV(host, adbkey, adb_server_ip, adb_server_port)
  File "/usr/local/lib/python3.7/site-packages/androidtv/androidtv.py", line 31, in __init__
    BaseTV.__init__(self, host, adbkey, adb_server_ip, adb_server_port)
  File "/usr/local/lib/python3.7/site-packages/androidtv/basetv.py", line 75, in __init__
    self.device_properties = self.get_device_properties()
  File "/usr/local/lib/python3.7/site-packages/androidtv/basetv.py", line 232, in get_device_properties
    constants.CMD_MAC_ETH0)
  File "/usr/local/lib/python3.7/site-packages/androidtv/basetv.py", line 126, in _adb_shell_pure_python_adb
    return self._adb_device.shell(cmd)
  File "/usr/local/lib/python3.7/site-packages/adb_messenger/command/transport/__init__.py", line 23, in shell
    conn.send(cmd)
  File "/usr/local/lib/python3.7/site-packages/adb_messenger/connection.py", line 77, in send
    return self._check_status()
  File "/usr/local/lib/python3.7/site-packages/adb_messenger/connection.py", line 83, in _check_status
    raise RuntimeError("ERROR: {} {}".format(repr(recv), error))
RuntimeError: ERROR: 'FAIL' 0006closed

This person had the same error, although his occurred after it had been successfully setup and working for a few hours: Home Assistant Community Add-on: ADB - Android Debug Bridge

It’s probably a bug with the pure-python-adb package. You could submit an issue there.

1 Like

I fixed the issue by removing the integration, restart, adding and restart and now it works.

As @max5962 mentioned, the usb remote gets deactivated while the usb debugging is turned on which his is very annoying. Is there a workaround for this? Or do you know a different Android TV OS box that doesn’t have this issue? I am using a Mi Box.
And also, the box is somehow disconnecting from wifi while in sleep mode. I tried all the possible solutions to avoid this and none of them worked.

So what is the best way to map all my Nvidia Shield buttons? I used the androidtv custom_component before but now I keep getting: Failed to call service androidtv/adb_command. Service not found.

Hi,

I configured Sony Bravia TV as android TV using ADB add on. It was working ok and since few weeks I have been encountering issues with disconnecting and very slow loading of Android TV entity in UI. What is happening:

  • from time to time TV becomes unavailable even if there are no connection issues in ADB add on logs (it is always connected)

  • when I want to volume down the tv from the entity window (after clicking the 3 dots button) it is loading very slow and all of the buttons become responsive after ~ 15s when the history state is fully loaded. Sometimes it just freezes and remains completely unresponsive. I got below logs in developer tool from time to time:

Updating androidtv media_player took longer than the scheduled update interval

failed to execute an adb command. adb connection re-establishing attempt in the next update. error: error: 'fail' 0006closed

HA version is 0.98.2
ADB add on version is 0.5.2

Has anyone similar issues and know the possible solutions?

Thanks!

This method works by sending volume up/down commands with a 1 second pause in between. Without this pause, the device will do a quick power cycle. This is the most robust solution I’ve found so far.

Source: androidtv documentation for set_volume_level

If you send too many volume up/down commands in a row, you’ll power cycle the device. At least, that’s what one tester and I both found. I tried to find a better way to set the volume to a given level, but I was not successful.

It sounds like what might be happening is that you send a bunch of volume up/down commands, causing the device to power cycle. During this time, an ADB command (such as the HA update command) fails. And when an ADB command fails, the device gets marked as unavailable and HA will try to reconnect on the next update, which is in 10 seconds by default.

Thanks for explanations. It seems that I mixed two different issues in one topic. Sorry about that.

First I wanted to ask if it is normal behavior that media player window becomes responsive after 15s when you click 3 dots button (window with mute and volume buttons). In my case after clicking 3 dots (I do not touch anything, just waiting) I am waiting 15s and later I can click the available buttons. I noticed that other entities load immediately but in this case it lasts very long.

Second case is that after setup of ADB connection between Sony Bravia and add on everything works fine for few hours. After the TV gets off problems are starting (adb log with failed connection) and entity becomes unavailable. It remains unavailable even if logs in add on show that add on is connected to the TV. Only restarting the add on and reset of adb debugging on TV helps - but just for few hours - it happens again and again. I enabled all of the remote settings in Bravia. I can ping my TV always when it is off.

Below the logs for 2nd issue:

2019-09-04 17:13:48 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.android_tv fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 261, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 441, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 247, in _adb_exception_catcher
    return func(self, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 443, in update
    self.aftv.update()
  File "/usr/local/lib/python3.7/site-packages/androidtv/androidtv.py", line 77, in update
    screen_on, awake, wake_lock_size, current_app, media_session_state, audio_state, device, is_volume_muted, volume = self.get_properties(lazy=True)
  File "/usr/local/lib/python3.7/site-packages/androidtv/androidtv.py", line 234, in get_properties
    wake_lock_size = self._wake_lock_size(lines[0])
  File "/usr/local/lib/python3.7/site-packages/androidtv/basetv.py", line 914, in _wake_lock_size
    return int(wake_lock_size_response.split("=")[1].strip())
IndexError: list index out of range

2019-09-04 17:16:00 ERROR (SyncWorker_11) [homeassistant.components.androidtv.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ERROR: 'FAIL' 0006closed

The second log keeps repeating all the time.

Thanks in advance for any help.

Thanks for the log. This custom component should fix that issue: androidtv custom component (HACS URL)

As long as the device is available, its window in the UI should load the same as any other media player window.

So I checked android TV custom component (installed HACS and copied androidtv custom component files to custom_components). Media window after clicking three dots button reacts randomly - sometimes volume buttons are responsive, sometimes the window freezes and crashes HA page. In addition play button is not changing to pause after starting e.g. Youtube video and history state is not available after clicking three dots.

I don`t know yet if the connection issues are fixed. I will test it for today and share my observations.

Thanks a lot so far!

I think this boils down to the state not being determined correctly. It’s basically impossible to come up with a set of state detection rules that work for every app on every device, so I added a state_detection_rules config parameter that lets you provide your own state detection rules using the properties collected by the androidtv package during the update.

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.