Native support for Android TV / Android devices

I am also trying this setup, but can’t seem to get it to work. Did you ever resolve this?

Nope. You are the only other one I’ve seen even mention the USB to ethernet adapter.
I have given up using the ethernet adapter.
Not all perfect on WiFi either, but I get something that way.
Right now fighting getting more than one working on my network at the same time.

I cannot imagine everyone just uses WiFi, or there is something pretty obvious we’re missing.

I have even tried the rndis option for the USB networking and nothing.
Let me know if you ever get it. I would love to use them all that way, but I also want to see and control them from HA so I caved and went with what works.

Reading online some more, I don’t think it’s possible. Guess I will try to push commands through Bluetooth or even IR. I won’t be enabling ADB because I also have a mouse, keyboard and external hard drive attached to the adapter

On my Philips Android TV the ADB connect handshake only works over Wifi. Once the handshake was done, connecting to the TV via ethernet was possible.

I believe that is also in the docs.

There is also an Android TV app in the play store, which works quite nice. So there should be some way to control it through network also. Anyone knows a python library that can be used?

What was the android tv app?

The app is Android TV remote control by Google itself

Did you figure this out in the end? I have a miboxs as well… It turns of but it doesn’t turn on again… loses adb connection :S

So I have a Toshiba FireTV which use to be very stable but for the last month or so it started going unavailable after a few/several hours everyday. I was using the adbkey method all along. I have tried a few different methods once it started acting up… auto key generation, etc… So it lost it’s connection again so now I am trying to adbserver method but this is what I got in home assistant…

2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] Connect to adb server - 127.0.0.1:5037
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] b'000Chost:devices'
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] Connection closed...
2019-11-30 13:56:12 WARNING (SyncWorker_3) [custom_components.androidtv.androidtv.adb_manager] Couldn't connect to host 192.168.1.38:5555 via ADB server 127.0.0.1:5037
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.adb_manager] ADB command not sent to 192.168.1.38:5555 via ADB server 127.0.0.1:5037 because pure-python-adb connection is not established: getprop ro.product.manufacturer && getprop ro.product.model && getprop ro.serialno && getprop ro.build.version.release && ip addr show wlan0 | grep -m 1 ether && ip addr show eth0 | grep -m 1 ether
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.basetv] 192.168.1.38:5555 `get_device_properties` response: None
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] Connect to adb server - 127.0.0.1:5037
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] b'000Chost:devices'
2019-11-30 13:56:12 DEBUG (SyncWorker_3) [custom_components.androidtv.androidtv.ppadb.connection] Connection closed...
2019-11-30 13:56:12 WARNING (SyncWorker_3) [custom_components.androidtv.media_player] Could not connect to Fire TV at 192.168.1.38:5555 using ADB server at 127.0.0.1:5037

My tv is wired but the key was originally acquired over Wifi… I didn’t know if it being wired is affecting the “Initial” connection when trying to use adb server… I’m kind of at a loss because this was so stable using the adb key method… also when checking adb server after rebooting my whole system adb server says daemon not running… I created adb in init.d per some instructions here but im not sure if that is correct method.
Any help would be appreciated.

@Bartem It’s difficult to troubleshoot when you’re using the ADB server approach because the issue could be on the ADB server side, and that’s hard to diagnose from HA. With the Python ADB approach, what error do you get in the logs when the device becomes unavailable?

FYI, it doesn’t really matter whether HA generates the key for you or if you use a key that you generated and copied over, as long as you tell the device to always trust connections from that computer. Having the component generate the key for you simplifies the initial setup process, nothing more.

Last night it suddenly couldn’t connect at all with the adb server method so I ended up switching back to the auto-generated key… it’s connected right now and debug on… so if it happens to become unavailable again I’ll see exactly what the message is… transport endpoint something I believe… :grimacing:

So after what was the longest it has stayed connected in weeks (over 24 hours) it became unavailable again… currently using the saved adbkey… (the auto-generated didn’t seem to last longer, but I’ll gladly try again if you think it would help…)
my TV is connected to wired ethernet, the key was originally generated on wireless back at the time you had the first FireTV Custom component
here is my config

  - platform: androidtv
    name: Fire TV
    host: 192.168.1.38
    device_class: firetv
    adbkey: "/home/homeassistant/.android/adbkey"
#    adb_server_ip: 127.0.0.1
    get_sources: false
    apps:
      com.amazon.tv.launcher: "Fire TV"
      com.amazon.tv.inputpreference.service: "Cable/Cameras"
      com.cbs.ott: "CBS All Access"
      com.netflix.ninja: "Netflix"
      com.amazon.firetv.youtube: "YouTube"

And here is the logs from the time it became unavailable…

2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_manager] Sending command to 192.168.1.38:5555 via adb-shell: (dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\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)
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'OPEN\x01\x00\x00\x00\x00\x00\x00\x00\r\x02\x00\x00>\xa8\x00\x00\xb0\xaf\xba\xb1'
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b"shell:(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\\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)\x00"
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_read(24): b'OKAYp\t\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\xb4\xbe\xa6'
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_read(24): b'CLSEp\t\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbc\xb3\xac\xba'
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'CLSE\x01\x00\x00\x00p\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbc\xb3\xac\xba'
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b''
2019-12-04 12:02:42 DEBUG (SyncWorker_7) [custom_components.androidtv.androidtv.firetv] Fire TV 192.168.1.38:5555 `get_properties` response: 
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_manager] Sending command to 192.168.1.38:5555 via adb-shell: (dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\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)
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'OPEN\x01\x00\x00\x00\x00\x00\x00\x00\r\x02\x00\x00>\xa8\x00\x00\xb0\xaf\xba\xb1'
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b"shell:(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\\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)\x00"
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_read(24): b'OKAYq\t\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb0\xb4\xbe\xa6'
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_read(24): b'CLSEq\t\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbc\xb3\xac\xba'
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'CLSE\x01\x00\x00\x00q\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbc\xb3\xac\xba'
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b''
2019-12-04 12:02:53 DEBUG (SyncWorker_2) [custom_components.androidtv.androidtv.firetv] Fire TV 192.168.1.38:5555 `get_properties` response: 
2019-12-04 12:03:04 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_manager] Sending command to 192.168.1.38:5555 via adb-shell: (dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\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)
2019-12-04 12:03:04 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'OPEN\x01\x00\x00\x00\x00\x00\x00\x00\r\x02\x00\x00>\xa8\x00\x00\xb0\xaf\xba\xb1'
2019-12-04 12:03:04 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b"shell:(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true') && echo -e '1\\c' && dumpsys power | grep mWakefulness | grep -q Awake && echo -e '1\\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)\x00"
2019-12-04 12:03:12 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_read(24): b''
2019-12-04 12:03:12 ERROR (SyncWorker_0) [custom_components.androidtv.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ('Unable to unpack ADB command. (length=0)', b'<6I', b'', error('unpack requires a buffer of 24 bytes'))
2019-12-04 12:03:15 DEBUG (SyncWorker_8) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b"CNXN\x00\x00\x00\x01\x00\x10\x00\x00\x0e\x00\x00\x00'\x05\x00\x00\xbc\xb1\xa7\xb1"
2019-12-04 12:03:15 DEBUG (SyncWorker_8) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'host::hassnuc\x00'
2019-12-04 12:03:24 WARNING (SyncWorker_8) [custom_components.androidtv.androidtv.adb_manager] Couldn't connect to host 192.168.1.38:5555.  TcpTimeoutException: Reading from 192.168.1.38:5555 timed out (9.0 seconds)
2019-12-04 12:03:37 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b"CNXN\x00\x00\x00\x01\x00\x10\x00\x00\x0e\x00\x00\x00'\x05\x00\x00\xbc\xb1\xa7\xb1"
2019-12-04 12:03:37 DEBUG (SyncWorker_0) [custom_components.androidtv.androidtv.adb_shell.adb_device] bulk_write: b'host::hassnuc\x00'
2019-12-04 12:03:37 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.fire_tv fails
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 104, in connect
    self._adb.connect(rsa_keys=[signer], auth_timeout_s=auth_timeout_s)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/adb_device.py", line 317, in connect
    cmd, arg0, arg1, banner = self._read([constants.AUTH, constants.CNXN], adb_info)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/adb_device.py", line 739, in _read
    msg = self._handle.bulk_read(constants.MESSAGE_SIZE, adb_info.timeout_s)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/tcp_handle.py", line 127, in bulk_read
    return self._connection.recv(numbytes)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 270, in async_update_ha_state
    await self.async_device_update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 450, 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 "/home/homeassistant/.homeassistant/custom_components/androidtv/media_player.py", line 271, in _adb_exception_catcher
    return func(self, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/media_player.py", line 536, in update
    self._available = self.aftv.adb_connect(always_log_errors=False)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/basetv.py", line 172, in adb_connect
    return self._adb.connect(always_log_errors, auth_timeout_s)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 122, in connect
    self.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 66, in close
    self._adb.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/adb_device.py", line 261, in close
    self._handle.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/tcp_handle.py", line 83, in close
    self._connection.shutdown(socket.SHUT_RDWR)
OSError: [Errno 107] Transport endpoint is not connected
2019-12-04 12:03:48 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.fire_tv fails
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 104, in connect
    self._adb.connect(rsa_keys=[signer], auth_timeout_s=auth_timeout_s)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/adb_device.py", line 308, in connect
    self._handle.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/tcp_handle.py", line 83, in close
    self._connection.shutdown(socket.SHUT_RDWR)
OSError: [Errno 107] Transport endpoint is not connected

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 270, in async_update_ha_state
    await self.async_device_update()
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 450, 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 "/home/homeassistant/.homeassistant/custom_components/androidtv/media_player.py", line 271, in _adb_exception_catcher
    return func(self, *args, **kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/media_player.py", line 536, in update
    self._available = self.aftv.adb_connect(always_log_errors=False)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/basetv.py", line 172, in adb_connect
    return self._adb.connect(always_log_errors, auth_timeout_s)
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 122, in connect
    self.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_manager.py", line 66, in close
    self._adb.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/adb_device.py", line 261, in close
    self._handle.close()
  File "/home/homeassistant/.homeassistant/custom_components/androidtv/androidtv/adb_shell/tcp_handle.py", line 83, in close
    self._connection.shutdown(socket.SHUT_RDWR)
OSError: [Errno 107] Transport endpoint is not connected

Any ideas would be greatly appreciated… I was thinking about leaving it off of the wired connection and trying wireless only for a bit, but as I had changed other things I didn’t want to introduce too many variables at once…

Thanks for posting the log! I opened an issue here: https://github.com/JeffLIrion/adb_shell/issues/53

I added a fix in the custom component. If that solves the issue for you, I’ll submit it to HA.

Ok I updated I’ll let you know how it goes. Thank you!

Has anyone successfully got media player controls (volume and play/pause/stop) to show up in homekit? I’ve tried entity_config for supported features, device_class to tv and re-setting/re-pairing with homekit. No bueno.

media_player:
  - platform: androidtv
    name: Television
    host: 192.168.0.1

media_player.television:
  device_class: tv

homekit:
  auto_start: False
  ip_address: 192.168.0.1
  entity_config:
    media_player.television:
      feature_list:
        - feature: on_off
        - feature: play_pause
        - feature: play_stop
        - feature: toggle_mute

Is the updated component still working? Has it been long enough to conclude that the issue was fixed?

Yes, I just checked again and it is still good I’m pretty sure it would have been unavailable by now previously. Thanks again!

EDIT: Just to check back in… 6 days and hasn’t gone unavailable once! Thanks again!