Native support for Android TV / Android devices

@QbaF first of all, close the ports that you opened on your router. That’s irrelevant and insecure.

@Amaurgit @paddy0174 I think @QbaF’s issue is that with a tablet, before you can connect via ADB over WiFi, you first have to connect over USB and issue the command adb tcpip 5555 (I think that’s right). And if you restart the tablet, then you’ll need to repeat that step. At least, that was the case for my Kindle Fire tablet when I tried it a year or two ago.

1 Like

Thanks @JeffLIrion. It looks like we are closer. I connected tablet to PC, get key, made adb tcpip 5555 and it is now connected to 192.168.1.71:5555. Also via WiFi

I did try to connect it as well to RPI (is it needed?), get info about charging but this time no RSA key message provided. Is it ok?
And what next steps? Supplement integration with adb server and start ADB add-on?

https://developer.android.com/studio/command-line/adb#wireless

I would try it without the add-on, using the basic config you posted in Native support for Android TV / Android devices.

It used to be the case that the Python implementation was unreliable for newer devices, making it necessary to use an ADB server (such as the add-on). But I rewrote the backend Python library used for sending ADB commands and it’s been solid for a handful of releases. At least, I haven’t heard of any issues with it. The ADB server approach is still supported because

  • I can’t guarantee that the Python ADB implementation is as reliable as using the official ADB binary (running separately and acting as a middleman)
  • Forcing people to use the Python implementation would be a breaking change
  • A small percentage of people might already have an ADB server up and running that they use for reasons outside of HA
1 Like

Working! great! Thanks @JeffLIrion, @Amaurgit and @paddy0174

Is there a chance to have more than one device over WiFi? On USB there is second device listed but when I try to do again adb tcpip 5555 i got error

error: more than one device/emulator

I just started using the Android TV integration with my Fire TV 4K stick and it works well when it works :slight_smile:

I am trying to figure out a problem where my device state goes to Unavailable and logs indicate that the Fire TV can’t be connected to. I tried both the built in python adb implementation as well as running an adb server locally. I can still ping the Fire TV so i know it’s online and I can see that the 5555 adb port is open with nmap. I try to manually connect using the adb command line and although it shows as connected, it is not listed in ‘adb devices’.

I suspect that the Fire TV might kill all adb connections after being off for some time. The only way I can get the integration working again, is to toggle the ADB Debugging setting from On to Off in the Fire TV settings. Has anyone faced a similar issue or have a suggestion here?

I have it working with a ugreen Ethernet adapter on a mibox s, I had issues with the mibox going into wifi sleep mode and only staying connected to my 2.4 wifi and Plex was suffering. The only way to get it off the 2.4 was to disable wifi and re-enable and it would connect to the right band again. The Ethernet adapter works but it’s a little finicky, basically with USB debugging enabled the adapter won’t power on you have to go into developer settings and disable USB debugging then Ethernet will connect but before you re-enable USB debugging you have to select the USB configuration and change it from charging to RNDIS (USB Ethernet) after that is selected you can enable USB debugging again and everything will work. This works perfectly until the mibox reboots and then you have to do it again. It’s not that big of a deal as it doesn’t reboot often but it does do it randomly sometimes which I am sure anyone with a mibox has experienced.

1 Like

Thanks for the reply! I’ll try to see if I can replicate that using your method.
I’ve tried all of the above, but not necessarily in that order. Spose to rain all day tomorrow anyway so maybe I can find some time to play with it.

Let me know how it goes, I’ve tested speed with it and I get a steady 200mbps x 200mbps. Just after I wrote that reply up my Plex was stuttering again even with the Ethernet connected I was only getting 20mbps, I had to reboot and reconfigure but it all worked like a charm after.

I’m facing exactly the same issue where it works fine and then all of a sudden I can’t connect and get the error message:

using Python ADB implementation with adbkey='/config/.storage/androidtv_adbkey'
Failed to call service system_log/clear. undefined

and the only way to get it working again is to turn OFF/ON ADB Debugging. Would love to know why but my python is lacking!

Anyone happen to find a way to recognize which Hdmi input a FireTV (Toshiba) is on? It seems that the “wakelock size” etc is the same for all 3 HDMI inputs… unless I am missing something

You could check here for some ideas: question: determine state of connected tv/display

1 Like

Hi,
replying to the old question, it is possible to use the addon through the ethernet port too,
just need a first connection via wifi for enable the port and to authorize the connection (remember to check the box to authorize permanently) and then in the developer menu “Select USB Configuration” and choose RNDIS (USB Ethernet) and that’s it. Little problem, the box loose this config every time reboot so you’ll see the box doesn’t have connection (Wifi is off), but it is fast to reconfigure it.

I played around with the settings and got all sources to be shown, but I don’t see all apps, for an example netflix and spotify is not shown.

I tried to ad them manually, ie “com.netflix.ninja”, but still it not shown, if I choose an available source and rename them, they are shown with my custom name. Any idea why I don’t see all apps?

Hello not sure if this is the right place but I am trying to get my amazon fire tv set up in to home assistant however, I am almost certain I have done everything right but I there is no device appearing.
Please see below for my settings.
image
image
image

I hit this issue today. Had to toggle ADB Debugging OFF, then ON again. sigh

That seems to happen from time to time, maybe once a week to once a month if I had to guess. Aside from toggling ADB off and then back on, restarting the device should fix it.

If someone posts a debug log, I’ll take a look. But that might just be the way it is.

I’m having a few problems with AndroidTV (in my case a TV-Stick from german provider Telekom) for a few months now, but since a few days ago, I have one more error message, that annoys me very much, as it comes up a lot (meaning yesterday within 15 hours around 6000 messages in the log…).

So here we go, I got a few error messages:

  • This is the “new one”
    Traceback (most recent call last):
    File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 272, in async_update_ha_state
      await self.async_device_update()
    File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 463, in async_device_update
      await self.async_update()  # type: ignore
    File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 357, in _adb_exception_catcher
      return await func(self, *args, **kwargs)
    File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 658, in async_update
      ) = await self.aftv.update(self._get_sources)
    File "/usr/local/lib/python3.8/site-packages/androidtv/androidtv/androidtv_async.py", line 73, in update
      screen_on, awake, audio_state, wake_lock_size, current_app, media_session_state, audio_output_device, is_volume_muted, volume, running_apps = await self.get_properties(get_running_apps=get_running_apps, lazy=lazy)
    File "/usr/local/lib/python3.8/site-packages/androidtv/androidtv/androidtv_async.py", line 125, in get_properties
      output = await self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS)
    File "/usr/local/lib/python3.8/site-packages/androidtv/adb_manager/adb_manager_async.py", line 498, in shell
      return await self._adb_device.shell(cmd)
    File "/usr/local/lib/python3.8/site-packages/androidtv/adb_manager/adb_manager_async.py", line 43, in shell
      return await asyncio.get_running_loop().run_in_executor(None, self._device.shell, cmd)
    AttributeError: 'NoneType' object has no attribute 'shell'
    
  • These are “old ones”
    Traceback (most recent call last):
    File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 418, in start
      resp = await task
    File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 458, in _handle
      resp = await handler(request)
    File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
      return await handler(request)
    File "/usr/src/homeassistant/homeassistant/components/http/real_ip.py", line 39, in real_ip_middleware
      return await handler(request)
    File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 73, in ban_middleware
      return await handler(request)
    File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
      return await handler(request)
    File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 129, in handle
      result = await result
    File "/usr/src/homeassistant/homeassistant/components/media_player/__init__.py", line 882, in get
      data, content_type = await player.async_get_media_image()
    File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 357, in _adb_exception_catcher
      return await func(self, *args, **kwargs)
    File "/usr/src/homeassistant/homeassistant/components/androidtv/media_player.py", line 500, in async_get_media_image
      media_data = await self.aftv.adb_screencap()
    File "/usr/local/lib/python3.8/site-packages/androidtv/basetv/basetv_async.py", line 149, in adb_screencap
      return await self._adb.screencap()
    File "/usr/local/lib/python3.8/site-packages/androidtv/adb_manager/adb_manager_async.py", line 476, in screencap
      return await self._adb_device.screencap()
    File "/usr/local/lib/python3.8/site-packages/androidtv/adb_manager/adb_manager_async.py", line 39, in screencap
      return await asyncio.get_running_loop().run_in_executor(None, self._device.screencap)
    File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
      result = self.fn(*self.args, **self.kwargs)
    File "/usr/local/lib/python3.8/site-packages/ppadb/command/transport/__init__.py", line 48, in screencap
      if result[5] == 0x0d:
    IndexError: bytearray index out of range
    
    and
    Updating androidtv media_player took longer than the scheduled update interval 0:00:10
    
    and
    Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ERROR: 'FAIL' 0006closed
    

All things seem to work as expected, beside the possibility to turn on/off the Magenta-TV-Stick. I can circumvent this by turning on/off the TV itself (another AndroidTV-SonyBravia) and CEC does the rest. Correction, since the “new” error message I can’t control the MagentaTV-Stick in any way, it simply doesn’t work (what I’d have expected after the shell error).

And here we go with the config:

- platform: braviatv_psk
  host: !secret host_sony-bravia
  name: Sony Bravia TV
  psk: sony
  mac: !secret mac_sony-bravia
  amp: True
  android: True
- platform: androidtv
  name: MagentaTV Stick
  host: !secret host_magentatv-stick
  adb_server_ip: 127.0.0.1
  adb_server_port: 5037
  apps:
    [...skipped...]
  exclude_unnamed_apps: true
  state_detection_rules:
    [...skipped...]

Is there anything I can do to debug this further, or does someone have other ideas how to solve this, I would appreciate any input.

Thanks! :slight_smile:

I fixed that here, it will be in 0.113.1: https://github.com/home-assistant/core/pull/38090

You should file an issue here: https://github.com/Swind/pure-python-adb/issues

Updating androidtv media_player took longer than the scheduled update interval 0:00:10

I suspect that is due to the fact that you have the screencap option enabled.

Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ERROR: ‘FAIL’ 0006closed

I’m not sure about that one, but it could be a bug with pure-python-adb

Just a guess, but the last two issues could be related: the device could be unresponsive, causing the update command to hang forever, causing the next update command to fail. I use the Python ADB implementation, so maybe this doesn’t apply, but every few weeks or so my Fire TV stick stops responding to ADB commands sent by HA and I have to restart the Fire TV, after which HA successfully reconnects.

1 Like

You are a genius! :slight_smile: Thanks a lot, my total respect, you know your code base! :+1: :slight_smile:

I disabled the screencapture (must have overlooked that setting), and all errors are gone. It’s now running for a good 45 minutes without any error message, even the “new one” seems to be gone. All is responsible and working as expected. I can switch, play, pause, turn on or off, all good… :+1:

Thanks a lot and have a nice weekend! :slight_smile:

EDIT: After around one and a half hours the error that you already have fixed is back, but that’s not a problem, I’ll wait for 0.113.1 and report back. :slight_smile:

Having used Apple TV’s in the past and recently switched to Android TV I do miss one thing. On the ATV (and the HA component) it was able to determine whether the device was playing or paused. I find that with the SHIELD TV and the Xiaomi MiBox S they both show a playing state when browsing apps like Disney+ or Netflix, the state would simply change to playing after a few seconds even if you were just browsing these apps. This makes automating anything based on play/paused states quite difficult which was not the case for my ATV’s. Also updating the state takes over 5 seconds which makes the automations feel kinda clunky. I wonder there is a way to change the polling speed or something so that at least the states update in realtime.

For my movies I use the Plex integration to automate on (which surprisingly does update the state in real-time. Even more surprisingly, my ATV had correct states and real-time play/pause state changes, but the Plex integration had that same 5-8 seconds delay which my Android TV has on all other apps XD).

I wonder if this is a limitation of the ADB implementation and if there is a way to work around it (which I am thinking it isn’t possible). Too bad if it can’t get better than this, I actually like the Android TV experience in conjunction with other Google services as opposed to the Apple TV. Though currently I think the ATV HA component works slightly better in this regard.

I am fairly new to the Android ecosystem and thus also fairly new to this component. If there is anything I should know or something I have missed please correct me XD.