Native support for Fire TV


#1

Currently, the Fire TV component requires the user to run an HTTP server in a Python 2 environment. I’m trying to develop a Fire TV component that is completely controlled by Home Assistant, no addon or service necessary. I’m also trying to make it handle ADB device authentication.

Here’s what I’ve got so far: https://github.com/JeffLIrion/homeassistant_native_firetv

Installation

Copy the media_player/firetv.py to your custom_components folder (custom_components/media_player/firetv.py).

Configuration

media_player:
  - platform: firetv
    name: Fire TV 1
    host: "192.168.0.111"

  - platform: firetv
    name: Fire TV 2
    host: "192.168.0.222"
    adbkey: "/config/android/adbkey"

ADB Authentication (for Fire TV devices with recent software)

If you get a “Device authentication required, no keys available” error when trying to setup Fire TV, then you’ll need to create an adbkey and add its path to your configuration. Follow the instructions on this page to connect to your Fire TV from your computer: Connecting to Fire TV Through adb.

Important! You must check the box that says “always allow connections from this device.” ADB authentication in Home Assistant will only work using a trusted key.

Once you’ve successfully connected to your Fire TV via the command adb connect <ipaddress>, the files adbkey and adbkey.pub will be created on your computer. Copy these to your Home Assistant folder and add the path to the adbkey file to your configuration.


#2

I fixed a bug. It might be working correctly now! But I’ve gotta get to bed. If you try it out, please let me know how it works for you.


#3

Wow, that’s cool. I moved the server into a docker container so that Python 2 does not bother me much, but this is way more elegant of course. Need to try it out!

Lars


#4

Update: I found this resource and managed to install M2Crypto and no longer get an error message that I cannot load component however no media_players are populated

I tried installing but get an error message saying there’s no M2Crypto

Tried installing like this and failed: pip3 install M2Crypto==0.30.1

----------------------------------------

Command “/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -u -c “import setuptools, tokenize;file=’/private/var/folders/n1/qp1syqb92yz_5w3n65c2f48c0000gn/T/pip-install-_ve_q8nq/M2Crypto/setup.py’;f=getattr(tokenize, ‘open’, open)(file);code=f.read().replace(’\r\n’, ‘\n’);f.close();exec(compile(code, file, ‘exec’))” install --record /private/var/folders/n1/qp1syqb92yz_5w3n65c2f48c0000gn/T/pip-record-b9ic5_j6/install-record.txt --single-version-externally-managed --compile” failed with error code 1 in /private/var/folders/n1/qp1syqb92yz_5w3n65c2f48c0000gn/T/pip-install-_ve_q8nq/M2Crypto/


#5

Same error here:

2018-08-17 15:53:41 ERROR (SyncWorker_15) [homeassistant.util.package] Unable to install package adb==1.3.0: Failed building wheel for M2Crypto
Command "/srv/homeassistant/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-vaooe6o9/M2Crypto/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-nswglak3/install-record.txt --single-version-externally-managed --compile --install-headers /srv/homeassistant/include/site/python3.6/M2Crypto" failed with error code 1 in /tmp/pip-install-vaooe6o9/M2Crypto/
2018-08-17 15:53:41 ERROR (MainThread) [homeassistant.requirements] Not initializing media_player.firetv because could not install requirement adb==1.3.0
2018-08-17 15:53:41 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform media_player.firetv: Could not install all requirements.
2018-08-17 15:54:40 WARNING (SyncWorker_5) [netdisco.ssdp] Found malformed XML at http://192.168.7.35:9080: status=ok

Tried to manually install M2Crypto, didn’t work either.


#6

@Jer78 and @chairstacker, thanks for testing it out! I switched from using M2Crypto to using pycryptodome, so that issue should be fixed now.

My status report

I have 2 Fire TV sticks, one of which requires authentication and the other does not. Currently, I’m able to setup the Fire TV that does not require authentication and it seems to be working correctly. Here’s the error message for the other one:

2018-08-17 17:31:23 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 129, 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/firetv.py", line 47, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/config/custom_components/media_player/firetv.py", line 61, in __init__
    self._firetv = FireTV(host, adbkey)
  File "/config/custom_components/python_firetv.py", line 102, in __init__
    self.connect()
  File "/config/custom_components/python_firetv.py", line 115, in connect
    self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host, rsa_keys=[signer])
  File "/config/custom_components/adb/adb_commands.py", line 137, in ConnectDevice
    self._Connect(**kwargs)
  File "/config/custom_components/adb/adb_commands.py", line 168, in _Connect
    conn_str = self.protocol_handler.Connect(self._handle, banner=banner, **kwargs)
  File "/config/custom_components/adb/adb_protocol.py", line 335, in Connect
    usb, [b'CNXN'], timeout_ms=auth_timeout_ms)
  File "/config/custom_components/adb/adb_protocol.py", line 242, in Read
    msg = usb.BulkRead(24, timeout_ms)
  File "/config/custom_components/adb/common.py", line 333, in BulkRead
    raise usb_exceptions.TcpTimeoutException(msg)
custom_components.adb.usb_exceptions.TcpTimeoutException: Reading from 192.168.0.31:5555 timed out (Timeout 0.1s)

#7

I’m getting the same error message and neither one of my devices work. I had ADB Debugging enabled on both of them. I turned one off to see if that made a difference but same error:

2018-08-17 23:43:35 WARNING (MainThread) [homeassistant.loader] You are using a custom component for media_player.firetv which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2018-08-17 23:43:58 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 47, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 60, in __init__
    from custom_components.python_firetv import FireTV
  File "/Users/mediacenter/.homeassistant/custom_components/python_firetv.py", line 13, in <module>
2018-08-17 23:43:58 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 47, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 60, in __init__
    from custom_components.python_firetv import FireTV
ImportError: cannot import name 'FireTV'

#8

That looks like a strange error. It’s looking in the right file – python_firetv – but for some reason it can’t import the class defined therein. You clearly copied the “python_firetv.py” and “media_player/firetv.py” files to the custom_components folder, but did re-copy the latest files from the repo? And did you copy the “adb” folder as well?


#9

Current status

  • I’m not able to setup the Fire TV that requires authentication.
  • I’m able to setup the Fire TV that doesn’t require authentication. However, when I turned it off from within Home Assistant I started getting this error:

.

2018-08-18 00:13:55 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.fire_tv_living_room fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 199, in async_update_ha_state
    yield from self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 322, in async_device_update
    yield from self.hass.async_add_job(self.update)
  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/firetv.py", line 106, in update
    }.get(self._firetv.state, STATE_UNKNOWN)
  File "/config/custom_components/python_firetv.py", line 131, in state
    if not self._screen_on:
  File "/config/custom_components/python_firetv.py", line 420, in _screen_on
    return self._dump_has('power', 'Display Power', 'state=ON')
  File "/config/custom_components/python_firetv.py", line 483, in _dump_has
    return self._dump(service, grep=grep).strip().find(search) > -1
  File "/config/custom_components/python_firetv.py", line 472, in _dump
    return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
  File "/config/custom_components/adb/adb_commands.py", line 366, in Shell
    timeout_ms=timeout_ms)
  File "/config/custom_components/adb/adb_protocol.py", line 406, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/config/custom_components/adb/adb_protocol.py", line 433, in StreamingCommand
    timeout_ms=timeout_ms)
  File "/config/custom_components/adb/adb_protocol.py", line 369, in Open
    timeout_ms=timeout_ms)
  File "/config/custom_components/adb/adb_protocol.py", line 247, in Read
    'Unknown command: %x' % cmd, cmd, (arg0, arg1))
custom_components.adb.adb_protocol.InvalidCommandError: ('Unknown command: 414b4fba', 1095454650, (967769, 256))

#10

I’m getting this error message now:

2018-08-18 11:23:57 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 129, 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 "/home/homeassistant/.homeassistant/custom_components/media_player/firetv.py", line 47, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/home/homeassistant/.homeassistant/custom_components/media_player/firetv.py", line 60, in __init__
    from custom_components.python_firetv import FireTV
  File "/home/homeassistant/.homeassistant/custom_components/python_firetv.py", line 13, in <module>
    from custom_components.adb import adb_commands
ModuleNotFoundError: No module named 'custom_components.adb'

Do I need to find a custom adb component and copy it into my custom_components folder?


#11

Yes. I put this in the README in the repo, but I’ll add it to my original post, as well.

Installation

Copy the adb folder and the files python_firetv.py and media_player/firetv.py to your custom_components folder.


#12

I deleted all the files from custom_components folder and downloaded them fresh again and here’s the error message:

2018-08-19 07:54:56 WARNING (MainThread) [homeassistant.loader] You are using a custom component for media_player.firetv which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2018-08-19 07:55:16 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
File “/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py”, line 47, in setup_platform
device = FireTVDevice(host, name, adbkey)
File “/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py”, line 60, in init
from custom_components.python_firetv import FireTV
File “/Users/mediacenter/.homeassistant/custom_components/python_firetv.py”, line 13, in


#13

It looks like you might be having the same issue as chairstacker. Just to be sure, did you copy the adb folder from the repo to custom_components/adb? Also, what version of Home Assistant are you using? I’ve tested it on version 0.66 (in a virtual environment on Ubuntu) and 0.75.3 (Hass.io on a Raspberry Pi) and I haven’t gotten that error on either.


#14

Yes I copied the ADB folder to custom_components. I’m using the latest 0.76. I will try some more troubleshooting this evening to see why this is happening


#15

Hi,

think this is a super good idea/development :+1:
Do you know if your control will be available for Fire Tablets too? Because i think ADB is supported too
That would be awesome


#16

Maybe it’s due to unmet dependencies because I included the adb package in the custom_components folder (since I needed to make some minor modifications) rather than having Home Assistant install it? You could try changing the REQUIREMENTS variable in firetv.py as follows:

REQUIREMENTS = ['libusb1>=1.0.16', 'pycryptodome', 'adb>=1.1.0']

# or
REQUIREMENTS = ['libusb1>=1.0.16', 'pycryptodome', 'adb==1.3.0']

Or some other variation.


#17

To be honest, I don’t know. I’m really just trying to take the existing Fire TV component and port it over to Python3 so that it can be natively integrated into Home Assistant, and I’m also trying to add device authentication functionality. I’m also not sure how useful it would be, since a Fire tablet is more than just a media player. But it might be feasible to take this component and modify it to do what you want with a Fire Tablet.


#18

I updated the requirements and still same error. How do I check which version of all the dependencies I currently have installed?

I can try installing manually

edit: so I have pycryptodome 3.6.1 installed.
The files are in the following directories:

custom_components/adb (all ADB folder contents)
custom_components/python_firetv.py
custom_components/media_player/firetv.py

And still getting the following error message:

2018-08-19 18:04:18 WARNING (MainThread) [homeassistant.loader] You are using a custom component for media_player.firetv which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2018-08-19 18:04:36 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform firetv
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 47, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/Users/mediacenter/.homeassistant/custom_components/media_player/firetv.py", line 60, in __init__
    from custom_components.python_firetv import FireTV
  File "/Users/mediacenter/.homeassistant/custom_components/python_firetv.py", line 13, in <module>

#19

I’m not sure why it’s having trouble importing the adb package from the custom_components folder, but here’s a crude attempt at getting it to work. In the file python_firetv.py, replace this:

from custom_components.adb import a    db_commands
from custom_components.adb import sign_pycryptodome
from custom_components.adb.adb_protocol import InvalidChecksumError

with this:

try:
    from custom_components.adb import adb_commands
    from custom_components.adb import sign_pycryptodome
    from custom_components.adb.adb_protocol import InvalidChecksumError
except:
    import os
    import sys
    sys.path.insert(0, os.path.dirname(__file__))

    from adb import adb_commands
    from adb import sign_pycryptodome
    from adb.adb_protocol import InvalidChecksumError

    # you can change the log level to whatever you'd like
    logging.info(adb_commands.__file__)

That logging statement should print out the path to the adb_commands.py file in the custom_components directory. If not, then there’s something strange going on with the imports.


#20

Current status

  • The Fire TV without authentication seems to be working correctly for me.
  • The Fire TV that requires authentication shows up in Home Assistant when I use M2Crypto, but not when I use pycryptodome. I’m able to use M2Crypto on my Home Assistant installation in a virtual environment on my laptop, but not in Hass.io on my Raspberry Pi.