Native support for Android TV / Android devices

Excellent project ! very excited about it !
I use the client version androidtv.py file. But when restarting HA, I have an error when setting up platform.
line 129 “from adb.client import Client as AdbClient”

I have adb installed.

Am I missing something ?

Great job man !!! :slight_smile:

1 Like

Thanks for the kind words!
Ok the original post is a bit outdated, I’ve been working on newer version of the component that handle both implementations (client and standalone) that should work way better :wink:
I’ve opened a pull request to hopefully get this component into Home Assistant, meanwhile you can find the latest version here.
You should probably uninstall any adb related Python package before trying this out, a lot of changes were made.

:slight_smile:
Thank you for that update !
I’ve downloaded the right package from your link. First restart and seems that I missed the androidtv-0.0.4 pypi package. Will try with ot installed.
If my Shield TV is already paired with my raspberry I can omit the adb server and ports parameters ?

Thank you for your work (and sorry for my english… I’m french :stuck_out_tongue: )

French here too! I will continue in english for the other users though :stuck_out_tongue:
Ok so you should install androitv, with pip3 install androidtv, if you already connected the Shield with the ADB server then all you need in your config is:

media_player:
  - platform: androidtv
    host: ip.of.the.shield
    name: Shield TV

and that should work! This will use the “standalone” implementation by default. Please report here how it works with an RPi 3 and a Shield :slight_smile:

Hi from Toulouse so :slight_smile:

So, I have installed androidtv, configured the shield as described.
Command /usr/bin/adb connect 192.168.0.49 returns “already connected to 192.168.0.49:5555”
BUT I Have this error in home-assistant.log after restart.

File "/home/homeassistant/.homeassistant/custom_components/media_player/androidtv.py", line 181, in 
setup_platform
atv = AndroidTV(host, adbkey)

Of course the component does not work.
Missing something ?

@jncanches I think your problem is that your pi is already connected to your Android TV via ADB, and this component is trying to make another connection. I think you can do one of two things:

  1. Use the “pure-python-adb” version of the component that makes calls to the adb binaries by adding adb_server_ip: '127.0.0.1' to your config.
  2. Close your ADB connection and retry. To be safe, follow the “ADB Troubleshooting” steps from the Fire TV component documentation: https://www.home-assistant.io/components/media_player.firetv/.

Thanks for the reply @JeffLIrion
I already solved my adbkey issue with the help of @a1ex4 :slight_smile:

Now I’m beta testing the component using an nvidia shield.

Didn’t about your FireTV component. Really good documentation. It helped me to understand adb permissions.

Great job thank you !

This does not work at all for me…

I did pip3 install androidtv (for the homeassistant user, since I use Hassbian).

When I use the component without any additional parameters (i. e. standalone), I get

Could not connect to Android TV at 192.168.1.8:5555 using adbkey='/home/homeassistant/.android/adbkey'

which is kind of clear because I did not install the adb server for homeassistant user and there are no adb keys. But installing android-tools-adb does not work when switched to the homeassistant user… :rage:

When I try to use adb_server_ip: 127.0.0.1, I get this error, no matter if I define the adb_server_port or not:

2018-12-16 12:43:19 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform androidtv
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/media_player/androidtv.py", line 191, in setup_platform
    adb_server_port=config[CONF_ADB_SERVER_PORT])
  File "/srv/homeassistant/lib/python3.5/site-packages/androidtv/__init__.py", line 230, in __init__
    self.properties = self.device_info()
  File "/srv/homeassistant/lib/python3.5/site-packages/androidtv/__init__.py", line 327, in device_info
    btmac = re.findall(BTMAC_REGEX_PATTERN, properties)[0]
IndexError: list index out of range

You should either copy the adbkeys somewhere, then use them in your configuration file, or simply run the server and use adb_server_ip: 127.0.0.1. The error you are seeing came from a recent addition that we made, and that’s because your Android TV device behave differently than mine. But hey that’s what a development thread is for.

In the terminal, run
adb connect 192.168.1.8
adb shell getprop

And paste the results here.

EDIT: ok you have a Shield, for now installing a newer version and using the server should work:
cd /srv/homeassistant/lib/python3.5/site-packages/androidtv/
mv __init__.py __init__.py.bak
wget https://raw.githubusercontent.com/JeffLIrion/python-androidtv/fe6e9ffadfd86ef4c45ce75c3a87e0495bfa45c1/androidtv/__init__.py

This will backup and update the androitv python package, running and using the ADB server should work for you.

Hello,

Thank you for your work on this component.

I installed the new version as you suggested in your last post but still get an error. I’ve saved my adbkey in my config file like below :

media_player:
  - platform: androidtv
    host: 192.168.10.20
    name: Shield
    adbkey: /data/data/com.termux/files/home/.homeassistant/.android/adbkey 

error message :

Error while setting up platform androidtv
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/data/data/com.termux/files/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/data/data/com.termux/files/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/data/data/com.termux/files/home/.homeassistant/custom_components/media_player/androidtv.py", line 181, in setup_platform
    atv = AndroidTV(host, adbkey)
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py", line 230, in __init__
    self.properties = self.device_info()
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py", line 330, in device_info
    wifi_out = self._adb_shell('ip addr show wlan0')
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py", line 474, in _adb_shell_python_adb
    return self._adb.Shell(cmd)
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/adb/adb_protocol.py", line 439, in StreamingCommand
    for data in connection.ReadUntilClose():
AttributeError: 'NoneType' object has no attribute 'ReadUntilClose'

Can you try this quick fix and let me know if it works. In the androidtv/__init__.py, change line 330 from

wifi_out = self._adb_shell('ip addr show wlan0')

to

wifi_out = ''.join(list(self._adb.StreamingShell('ip addr show wlan0')))

The following command should do this for you:

sed -i "s/self._adb_shell('ip addr show wlan0')/''.join(list(self._adb.StreamingShell('ip addr show wlan0')))/g" /data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py

Unfortunately your quick fix didn’t solve the issue, the error is just slightly different now :

Error while setting up platform androidtv
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.7/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/data/data/com.termux/files/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/data/data/com.termux/files/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/data/data/com.termux/files/home/.homeassistant/custom_components/media_player/androidtv.py", line 181, in setup_platform
    atv = AndroidTV(host, adbkey)
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py", line 230, in __init__
    self.properties = self.device_info()
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/androidtv/__init__.py", line 330, in device_info
    wifi_out = ''.join(list(self._adb.StreamingShell('ip addr show wlan0')))
  File "/data/data/com.termux/files/home/.homeassistant/deps/lib/python3.7/site-packages/adb/adb_protocol.py", line 439, in StreamingCommand
    for data in connection.ReadUntilClose():
AttributeError: 'NoneType' object has no attribute 'ReadUntilClose'  

If i use the other configuration with adb_server_ip: 127.0.0.1 . and connect the adb server manually, the component is working fine.

Works on Mi Box S with the adb server ip and adb server running. The standalone version connects but shows Mi Box S as unavailable most of the time.

@MomoB @blakadder Thank you both for your feedback! :+1:

If anyone knows how to make a Hass.io addon that can install the ADB binaries and start the ADB server, that would be very helpful!

2 Likes

I am using hassio latest version installed through docker on my raspberrypi3b+.
i am using Mini m8s2 android tv box.
i am getting adb connections by adb connect xxx.xxx.xxx.xxx

this is the error which i am receiving
2019-01-03 13:39:40 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform androidtv
Traceback (most recent call last):
File “/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py”, line 128, in _async_setup_platform
SLOW_SETUP_MAX_WAIT, loop=hass.loop)
File “/usr/local/lib/python3.6/asyncio/tasks.py”, line 358, in wait_for
return fut.result()
File “/usr/local/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/media_player/androidtv.py”, line 181, in setup_platform
atv = AndroidTV(host, adbkey)
File “/config/deps/lib/python3.6/site-packages/androidtv/init.py”, line 230, in init
self.properties = self.device_info()
File “/config/deps/lib/python3.6/site-packages/androidtv/init.py”, line 327, in device_info
btmac = re.findall(BTMAC_REGEX_PATTERN, properties)[0]
IndexError: list index out of range

Please guide me to solve my problem.

Can someone,who got Android TV working,make a video of working and how feature works with Android TV/box.
Please someone,
So that all features can be fully used by all.

Hi a1ex4,

Thanks for developing a component for androidtv.
Unfortunately I cant get my Philips (android) TV to trust my Raspberry pi (running my HA instance). (see below)

pi@rhea:~$ adb devices
List of devices attached
192.168.1.114:5555      unauthorized

I enabled USB debugging but as far I understand I would be supposed to connect the TV to my raspi by USB to enable wireless ADB afterwards. Unfortunately I seems very cumbersome to move my TV or my raspi - do you have an idea how to enable ADB over Wifi without connecting by usb?

Thanks for any pointers.

Best regards
noheton

pi@rhea:~$ adb shell getprop
error: device unauthorized.
This adb server's $ADB_VENDOR_KEYS is not set
Try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.

When u allow ADB connections in Android TV and after connecting it with rasp using ADB, u have to look at TV for a popup which asks to allow ADB connections,
U have to select always and allow.
After that is done it wont show unauthorised.
Try and let us know ur story.

Hmm unfortunately I don’t seem to get any notification at all when I enable USB Debugging or when I try to connect with the raspi.
The TV is running Android Nougat (7?) by the way.
I will try again later and let you know.

Update: A reboot of the TV seems to have fixed it. Sorry my bad. I will now play with the component a bit :slight_smile: