[Deprecated] Native support for Fire TV

Just upgraded to 83.2 and got new adb keys. Running the above I get:

>>> import firetv
>>> host = '192.168.1.41:5555'
>>> abdkey = '/config/adbkey'
>>> f = firetv.FireTV(host, abdkey)
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 "<stdin>", line 1, in <module>
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 106, in __init__
    self.connect()
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 119, in connect
    self._adb = adb_commands.AdbCommands().ConnectDevice(**kwargs)
  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',))
>>>

And this:

>>> f = firetv.FireTV(host, abdkey)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 106, in __init__
    self.connect()
  File "/config/deps/lib/python3.6/site-packages/firetv/__init__.py", line 119, in connect
    self._adb = adb_commands.AdbCommands().ConnectDevice(**kwargs)
  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
>>>

Any ideas?

I’m not sure about the first error log, but the second one means that your key didn’t work. Did you tell it to “always allow connections from this computer”?

I’m not sure how to do that. All I did was adb connect 192.168.1.41 from the terminal and it generated the key and connected.

Ah, got it. I had to turn on the FireTV then run adb connect .... That caused a pop up to show up on the FireTV. I checked always allow connections from this computer and clicked OK. Seems to be working now.

Thanks

Been trying to get this component going for a few hours now. Followed everything by the letter.
Keep getting these in my log:

> 2018-12-11 20:52:45 ERROR (SyncWorker_3) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
> 2018-12-11 20:52:56 WARNING (SyncWorker_1) [root] Couldn't connect to host: 192.168.2.90:5555, error: Broken pipe
> 2018-12-11 20:53:28 WARNING (SyncWorker_17) [root] Couldn't connect to host: 192.168.2.90:5555, error: Connection reset by peer

What am I doing wrong?
Running latest hassio on RPi.

Thanks

Some people encounter that error and I haven’t been able to pinpoint why. Ultimately, the issue is with the adb Python package, upon which this component depends.

First of all, you should try the steps in the “ADB Troubleshooting” section on the Fire TV documentation page.

Beyond that, I’ve been working with @a1ex4 to allow for using the “pure-python-adb” package to run the ADB commands, which utilizes the ADB binaries on your system. He has a PR for an Android TV component, and it might work OK for Fire TV devices as well. Here are his thread and pull request, so you can give that a try. Once he’s gotten more feedback on his component, I’ll port that alternative ADB approach to the Fire TV component.

Before anything you need to connect to your Fire TV once with an ADB server on the RPi, I’m very unfamiliar with hassio, did you manage to do that ?

For those with ADB issues, you can try this version of the component. You’ll need to do some setup involving the ADB binaries. As you can probably tell, I’m not really sure how to use the “pure-python-adb” library. Maybe @a1ex4 can post a write-up somewhere about how to use it.

Component: https://github.com/JeffLIrion/homeassistant_native_firetv/blob/pure-python-adb/media_player/firetv.py

Example config:

media_player:
- platform: firetv
  host: '192.168.0.123'
  name: 'Fire TV Living Room'
  adb_server_ip: '127.0.0.1'

Thanks for this plugin @JeffLIrion.

What happens if the Fire Tv is turned off completely (cut off power) and turned back on after, lets say, one day? Does your plugin “refresh” and go back to work as intended?

Try this version of the component and let me know!

1 Like

Living on an island where power disruptions are not uncommon, my experience has been; if you power down the firestick while HA is running and then power it back up later… no big deal. If you had to restart HA while the firestick was powered off, you will need to power on the firestick and restart HA again to rebuild the firestick entity.

1 Like

Thanks for your replies.

I am just asking, because I want to put a smart wall plug before my media centre power strip. This switch would turn off/on my AVR, subwoofer and my FireTV stick. My AVR and subwoofer draw too much power in standby mode. Then I want to use my Logitech Harmony remote to turn on/off the switch for my media centre and (after a short delay) turn on my AVR, subwoofer and FireTV. Then it should open Spotify or Netflix App on FireTV.

This is working just fine and your version for handling-offline-devices is working, but I got some error messages. Here is a description of what I did before the error messages appear:

  1. Cut power from my media centre (AVR, subwoofer, FireTV)
  2. Waited for five minutes
  3. Turned on switch to power media centre
  4. Triggered my “Start Netflix” automation

Here is what happened in the frontend:

  1. FireTV state turned to “off”
    Now I turned switch on, FireTV starts
  2. FireTV state stays “off”
    FireTV booted up completely
  3. FireTV state turns to “not available”
  4. FireTV state turns to “standby”
  5. My automation triggers and starts Netflix

Automation:

automation:
- alias: "Remote  Harmony  Netflix"
  trigger:
    platform: state
    entity_id: input_select.harmony__media_center
    to: 'Netflix'
  condition:
    - condition: state
      entity_id: input_select.harmony__media_center
      state: 'Netflix'
    - condition: state
      entity_id: media_player.fire_tv
      state: 'standby'
  action:
    - service: media_player.select_source
      data:
        entity_id: media_player.fire_tv
        source: com.netflix.ninja 

home-assistant.log:

I got plenty of these errors:

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

I turned on power switch:

Update for media_player.fire_tv fails
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
    await self.async_device_update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 349, in async_device_update
    await self.hass.async_add_executor_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 123, in _adb_wrapper
    return func(self, *args, **kwargs)
  File "/config/custom_components/media_player/firetv.py", line 221, in update
    if not self.firetv.screen_on:
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 197, in screen_on
    return self._dump_has('power', 'Display Power', 'state=ON')
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 245, in _dump_has
    return self._dump(service, grep=grep).strip().find(search) > -1
  File "/usr/local/lib/python3.6/site-packages/firetv/__init__.py", line 234, in _dump
    return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
  File "/usr/local/lib/python3.6/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 438, in StreamingCommand
    timeout_ms=timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 374, in Open
    timeout_ms=timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/adb_protocol.py", line 242, in Read
    msg = usb.BulkRead(24, timeout_ms)
  File "/usr/local/lib/python3.6/site-packages/adb/common.py", line 341, in BulkRead
    return self._connection.recv(numbytes)
ConnectionResetError: [Errno 104] Connection reset by peer

FireTV booted up completely.

Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update


Everything is working as expected, but as I already said, FireTV boot takes too long. Easy fix would be to keep the FireTV always on, I guess.

Just wanted to let you know about the errors.

I pushed a change to the handle-offline-devices branch that doesn’t require the device to be on when HA starts. Of course, if you specify the wrong IP address (host) in your configuration, then that’s on you!:stuck_out_tongue_winking_eye:

The way that this component handles offline devices is by pinging them, and if they’re not online then it doesn’t try to update. With that in mind, please let me know if this is the correct sequence of events.

  1. Power to the Fire TV is cut off.
  2. You get an error message saying “Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update.” Maybe you also get something about the update taking longer than 10 seconds. The state of the Fire TV is unknown. There shouldn’t be any additional error messages until the next step.
  3. Power is restored to the Fire TV.
  4. The device comes online, so HA tries to connect, but the ADB connection is flawed in some way so you get that lengthy “ConnectionResetError” message in your log, perhaps multiple times.
  5. During one or more unsuccessful reconnect attempts, you get the “Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update” error message.
  6. HA successfully establishes the ADB connection, the error messages in the log stop, and everything works correctly once again.

Is that accurate?

Almost - I made some edits to your list:

  1. Power to the Fire TV is cut off.
  2. I get an error message saying something about the update taking longer than 10 seconds. The state of the Fire TV is “off”. I get this error message multiple times (maybe every 10 seconds).
  3. Power is restored to the Fire TV.
  4. The device comes online, so HA tries to connect, but the ADB connection is flawed in some way so you get that lengthy “ConnectionResetError” message in my log, only once.
  5. During one or more unsuccessful reconnect attempts, you get the “Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update” error message. I guess there is only one attempt to reconnect, but I am not sure.
  6. HA successfully establishes the ADB connection, the error messages in the log stop, and everything works correctly once again.

Hi everybody. I was wondering if this change has, or will be, pushed to the next main release? I’ve been wanting to get firetv integration with my lights for a while and just now realized that native integration was added.

I’ve got this kind of working with my new Firestick 4k version (Had to do get_sources: false) to avoid connection errors…

Where is the correct place to raise feature requests for the component? The new firestick can send power on/off signals (I guess via HDMI-CEC) to turn my TV and AV amp off… wondering if the component could also have the firetv trigger those as well? Currently it just seems to power down just the firetv

It seems like it won’t allow me to edit my original post…

These changes were integrated into HA 0.83. There are a couple minor differences between the custom component and the integrated component, as described in the README (which also contains setup information). For reference, here is the Fire TV documentation.

Someone already raised the issue here.

I only have regular, non-4k Fire TV sticks, so I can’t test and experiment with that. What I can try to do is create a Home Assistant service called media_player.firetv_adb_command, or something like that, that will allow users to send ADB commands to their Fire TV device. A list of ADB key codes can be found here: https://developer.android.com/reference/android/view/KeyEvent

Thanks. My setup has an issue then. I’m on the latest version of Hass, and it is able to connect to the firetv stick, it even updates the status to playing when I start a movie, but after about 30 seconds it just disconnects and is not able to connect again.