Native support for Fire TV


#62

Can someone give me a hand with this error? I feel like I’m close to getting this up and going…

2018-09-03 20:14:58 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-09-03 20:15:09 WARNING (MainThread) [homeassistant.setup] Setup of config is taking over 10 seconds.
2018-09-03 20:15:25 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 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/firetv.py", line 52, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/config/custom_components/media_player/firetv.py", line 90, in __init__
    self._firetv = FireTV(host, adbkey)
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 105, in __init__
    self.connect()
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 118, in connect
    self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host, rsa_keys=[signer])
  File "/config/deps/lib/python3.6/site-packages/adb/adb_commands.py", line 142, in ConnectDevice
    self._Connect(**kwargs)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_commands.py", line 173, in _Connect
    conn_str = self.protocol_handler.Connect(self._handle, banner=banner, **kwargs)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_protocol.py", line 336, in Connect
    data=rsa_keys[0].GetPublicKey() + b'\0')
TypeError: must be str, not bytes

#63

I think the problem is that you’re not using a trusted key. Please let me know if this fixes it for you!


#64

I already had an adb connection to my fire tv from long ago. I’m not sure where in the process the check box to “always allow connections from this device is”. Guess I need to start over from scratch. Thanks for your help.


#65

Requiring authentication for connecting via ADB is a security feature that Amazon implemented, and they’ve been rolling it out over time. It broke the existing integration with Home Assistant, hence the need for this updated component. But if your device doesn’t yet require authentication, then leave out the adbkey portion of the configuration and this component should work just fine.


#66

Working for me now, thanks. I was finally able to get the Fire Tv to prompt to “Always allow”. I appreciate all your work on this project.


#67

Hi,

first of all, thanks for the great work!

Currently iam trying to setup a connection to my fire tablet, rather than a fire tv stick.
The adb connection works fine on my computer with windows. I first got a connection from usb and then was able to start the adb on tcp with port 5555.
I was asked by the tablet if i would always allow the connection, which i enabled.
Its just a bit unclear to me, where i can find the adb key and if its possible to transfer it to another device.
I used the android plattform-tools for the adb connection on windows and the only key i found was in C:\Users\User.android. The key was made around 2017 when i first installed android sdk. So iam not sure if this is the right key which is used. I copied this key to my raspberry in the HA config and get the current error:

Couldn't connect to host: 192.168.178.26:5555, error: Connection refused

adb key seems to be needed, because i get the error, if i dont specify one

btw: i had a similiar error like @dickcollier which was solved by restart the fire tablet


#68

I don’t know if it will work with a Fire tablet. I tried once to connect to my Fire HD 8 and it didn’t work. It sounds like you have the right adbkey file. You do have both adbkey and adbkey.pub, right?

You could always try connecting using my forked python ADB package in the Python interpreter to help diagnose what the issue might be.


#69

Yep, both adbkey and adkey.pub are in the same folder.

You mean the pyadb library? The pyadb command doenst seem to have a connect option. How do i connect to the device? The “normal” adb command from android has a connect command to use

EDIT: i got a working connection to the fire tablet :+1:. I had to start the adb tcpip manual via usb. After that, i restarted the tablet and my guess is, that this stopped the tcpip adb client


#70

It looks like you already figured it out, but in case it helps you out with any further troubleshooting, here is what I was referring to.

pip install pycryptodome
pip install rsa
pip install git+https://github.com/JeffLIrion/python-adb
pip install git+https://github.com/JeffLIrion/python-firetv

And then in Python:

import firetv
f = firetv.FireTV('192.168.0.111:5555', '/path/to/adbkey')

#71

@JeffLIrion thanks for all the effort. My firetablet its running now!

Iam using your component to turn off/screen off my firetablet when i go too sleep. I think the screen_on property is good to have, for example if you use firetablet as wall mounted dashboard or something. Made a PR to your repo if you want to include it. (Although making an other component for firetablets might be better).

Just a short question: from what i experienced and seen in the code: if i start up/reboot my HA and the firetv/firetablet is offline, the setup will fail and the device is not setup and wont be reconnected once the device is online again. Is that right? I think thats the way most components are implemented and i think its quite aweful. What are your thoughts?


#72

Thanks for the PR!

You are correct: if the device is offline, it won’t be setup in Home Assistant. That’s what I’ve seen with every component that I’ve used. But the Fire TV doesn’t have to be on, it just has to be connected to a power supply. I haven’t had any issues with this.


#73

Hm, not sure why its implemented in all components this way. You mean by “doenst have to be on”, the TV doesnt have to show fireTV stream? Because i think if fireTV is powered, its always “connected”, at least has wifi connection and therefore is running.

I have a wifi switch to turn on/off the standby of TV and devices like firetv. If the tv is not running, everything is powered off. So sometimes its not connected via wifi. Same goes for my fire tablet, which sometimes doenst have battery and therefor is off. So if i then restart my HA, because i have changed config or update or something else: every component in my house has to be connected/powered on. This is so annoying. Meant this in general

Another problem i found: my firetablet got setup correctly on startup and is now shutdown (no wifi connection). I get the same error message, as if the startup would fail:

Error doing job: Task exception was never retrieved
Couldn't connect to host: 192.168.178.26:5555, error: Connection refused

But every 15(?) seconds, with the scan_interval of the platform running the update command…


#74

I noticed when I initially set this up, the firestick did indeed have to not only be on but doing something for it to register. However, once registered and I attached something to the entity, i.e.

group:
  media_players:
  entities:
  - media_player.firetv_406_livingroom
  - media_player.firetv_406_kitchen
  - media_player.firetv_406_bedroom

HA remembered them.


#75

@Alexxander0, how did you connect to your Fire tablet via ADB?

I’ve got a Fire HD 8 tablet. I plugged it into my laptop (running Ubuntu) with a USB cable and connected to it by following this guide – https://developer.amazon.com/docs/fire-tablets/ft-set-up-your-kindle-fire-tablet-for-testing.html – and running the following commands:

# change into the "platform-tools" folder
./adb start-server
# it asked me if I wanted to always allow this device, so I checked the box

./adb devices
# Output:
#   List of devices attached
#   <serial number>    device

But I can only connect to it via ADB when it’s plugged in with a USB cable. When I try to connect over WiFi, here’s what I get:

./adb connect 192.168.0.74
# Output:
#   unable to connect to 192.168.0.74:5555: Connection refused

#76

Connect it via usb to the computer
check if device is shown
adb devices

start tcpip on firetablet with:
adb tcpip 5555
close console and disconnect usb

connect via tpcip with:
adb connect IP:PORT

From what noticed, if you restart the firetablet you have to do this again as it doesnt save the adb tcpip. Iam checking if i can activate it permanently. if you find anything, please let me know. You might also check the settings in firetablet if there is “adb with wifi” or something


#77

Just a heads up, my upgrading to HA v 0.78.1 seems to have broken it:

Log Details (ERROR)
Fri Sep 21 2018 16:07:12 GMT-0500 (Central Daylight Time)

Error while setting up platform firetv
Traceback (most recent call last):
  File "/config/deps/lib/python3.6/site-packages/adb/adb_protocol.py", line 226, in Unpack
    cls.format, message)
struct.error: unpack requires a buffer of 24 bytes

During handling of the above exception, another exception occurred:

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/firetv.py", line 53, in setup_platform
    device = FireTVDevice(host, name, adbkey)
  File "/config/custom_components/media_player/firetv.py", line 91, in __init__
    self._firetv = FireTV(host, adbkey)
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 105, in __init__
    self.connect()
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 120, in connect
    self._adb = adb_commands.AdbCommands().ConnectDevice(serial=self.host)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_commands.py", line 142, in ConnectDevice
    self._Connect(**kwargs)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_commands.py", line 173, in _Connect
    conn_str = self.protocol_handler.Connect(self._handle, banner=banner, **kwargs)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_protocol.py", line 314, in Connect
    cmd, arg0, arg1, banner = cls.Read(usb, [b'CNXN', b'AUTH'])
  File "/config/deps/lib/python3.6/site-packages/adb/adb_protocol.py", line 243, in Read
    cmd, arg0, arg1, data_length, data_checksum = cls.Unpack(msg)
  File "/config/deps/lib/python3.6/site-packages/adb/adb_protocol.py", line 228, in Unpack
    raise ValueError('Unable to unpack ADB command.', cls.format, message, e)
ValueError: ('Unable to unpack ADB command.', b'<6I', b'', error('unpack requires a buffer of 24 bytes',))

#78

That error is due to the adb Python package. I’ve seen it before, but I’m not sure what the cause is. Maybe try restarting HA and/or your Fire TV. I’ve got 0.78.1 and the component is working fine for me.


#79

It’s a weekend coming up. I will play and let ya know. :slight_smile:


#80

It’s been a busy Saturday morning. :slight_smile:
Anyway, here’s the sequence of events:
Three days ago. Upgrade to HA v 0.78.1 = firetv media_devices disappear with error messages shown above when restarting.
Hard reboot of PI3 = no change.
Power recycle of all firesticks = no change.
Hard reboot of PI3 after firestick reboot = no change.
Upgrade to HA v 0.78.2 this morning = no change.
Restart HA = no change.
Hard reboot of PI3 = some change. One previously used firestick from last night showed up.
Power recycle of all firesticks = no change.
Hard reboot of PI3 = JOY!

It appears that upgrading to HA v 0.78.2 with a power recycle of all firesticks and hard reboot (in that order), exonerates you of any coding issues. Again I tip my hat your you and your code. Well done Sir!

Oh, before I forget, environment: PI3B+, HASSIO v 1.10, Supervisor v 131, HA v 0.78.2


#81

Thanks for detailing your steps to get it working, and I’m happy to hear that you were able to get it working again! That said, I have no idea what the issue was or what exactly caused it to start working again. My guess is that something was already connected to the Fire TVs via ADB and so HA failed when trying to connect, but I really don’t know.