Native support for Fire TV


#142

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.


#143

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


#145

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


#146

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.


#147

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 ?


#148

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'

Can't get new Fire TV integration to work
#149

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?


#150

Try this version of the component and let me know!


#151

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.


#152

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.


#153

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:


#154

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?


#155

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.