Can't get new Fire TV integration to work

Hi all,

I was just trying out the new Fire TV integration via adb that has been implemented in this PR.

I’ve installed adb by running apt-get install android-tools-adbon my HASS server. Then, I connected using adb connect [ip] to the Fire TV, which worked flawlessly. Then, after configuring the component (and setting the adbkey path), I get the following errors:

Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
16:27 components/media_player/firetv.py (ERROR)

Couldn't connect to host: [ip]:5555, error: Connection reset by peer
16:27 components/media_player/firetv.py (WARNING)

I’m running version 0.83.3. I’ve already checked the permissions on the key file and tried starting/stopping the adb server manually by running adb kill-server or adb connecting again. Interestingly, if I’m not mistaken, it sometimes, for a brief moment, seems to work:

Do you have any idea where I can start debugging?

Thanks

WiFiCable

Can you connect to the firetv from the command line.
adb connect ip
adb shell

Did you OK the connection from the TV and check Always allow? That happens on the TV screen.

Yes and no. It did work in the beginning. Then I started home assistant and tried tinkering around a bit since I got that error message and suddenly it stopped working. I could still hit adb connect ip and it would tell me that it connected successfully, but it didn’t show up in the devices list and I couldn’t shell into it anymore.

Then I’ve turned the adb option off and on again in the Fire TV settings and voila, it worked again, so I was able to open a shell. Then I restarted home assistant, and now I get a different error message:

Update for media_player.feuerglotze_2 fails
17:50 components/media_player/firetv.py (ERROR)

Couldn't connect to host: [ip]:5555, error: Broken pipe
17:50 components/media_player/firetv.py (WARNING)

The first error has a trace assigned to it:

Traceback (most recent call last):
  File "/home/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
    await self.async_device_update()
  File "/home/homeassistant/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/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/lib/python3.6/site-packages/homeassistant/components/media_player/firetv.py", line 105, in _adb_wrapper
    returns = func(self, *args, **kwargs)
  File "/home/homeassistant/lib/python3.6/site-packages/homeassistant/components/media_player/firetv.py", line 201, in update
    if not self.firetv.screen_on:
  File "/home/homeassistant/lib/python3.6/site-packages/firetv/__init__.py", line 197, in screen_on
    return self._dump_has('power', 'Display Power', 'state=ON')
  File "/home/homeassistant/lib/python3.6/site-packages/firetv/__init__.py", line 245, in _dump_has
    return self._dump(service, grep=grep).strip().find(search) > -1
  File "/home/homeassistant/lib/python3.6/site-packages/firetv/__init__.py", line 234, in _dump
    return self._adb.Shell('dumpsys {0} | grep "{1}"'.format(service, grep))
  File "/home/homeassistant/lib/python3.6/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/home/homeassistant/lib/python3.6/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/home/homeassistant/lib/python3.6/site-packages/adb/adb_protocol.py", line 438, in StreamingCommand
    timeout_ms=timeout_ms)
  File "/home/homeassistant/lib/python3.6/site-packages/adb/adb_protocol.py", line 372, in Open
    msg.Send(usb, timeout_ms)
  File "/home/homeassistant/lib/python3.6/site-packages/adb/adb_protocol.py", line 233, in Send
    usb.BulkWrite(self.Pack(), timeout_ms)
  File "/home/homeassistant/lib/python3.6/site-packages/adb/common.py", line 332, in BulkWrite
    return self._connection.send(data)
BrokenPipeError: [Errno 32] Broken pipe

Maybe something interesting to note: I couldn’t get the “confirm adb connection” screen to show up on my Fire TV. I know that screen from connecting to other adb devices, but here it won’t show up. Probably because I already connected to it a few months ago (if I remember correctly), but yeah… I also couldn’t find an option to reset trusted devices. Would it help to delete the keyfile maybe?

Update: So it turns out, removing the adbkey and connecting to the device again forces it to prompt that dialog again, so I made sure to accept it properly. But unfortunately, that doesn’t solve the problem…

Could not connect to Fire TV at [ip]:5555 using adbkey='/home/homeassistant/.homeassistant/adbkey'
18:05 components/media_player/firetv.py (WARNING)

Couldn't connect to host: [ip]:5555, error: Connection reset by peer
18:05 components/media_player/firetv.py (WARNING)

Thanks

WiFiCable

I just set this up this morning. I am connected via Ethernet both at the FireTV and HA so my connections are reliable. Broken pipe, in this case, certainly can mean a network connection between the two devices dropped even just momentarily.
Other than that, I had issues because I somewhat skipped steps. I didn’t OK the connection at the TV. Used the keys before they had been OK’ed. Restarted HA before disconnecting adb. After going through everything step by step and completing things before going to the next HA connected and set up the component.

My Home Assistant is connected via LAN, while the Fire TV is connected via WiFi. However, the connection should be fairly stable. I’ve never encountered any problems with it. I would understand if it would work most of the time and break sometimes, but currently it’s the other way around. It works for two seconds (and in that time it really does work - I was able to turn the TV on and even select an app) and then it just doesn’t work at all for half an hour. Really weird. Maybe I just have to wait a few more releases until it gets more stable.

Try a continuous ping to the firetv. See if you get consistent ping times. With streaming you wouldn’t notice momentary drops since the content is buffered.

I get zero dropped packets and nothing with more than 150ms, so I don’t think the connection is the problem. My guess would be that maybe the Fire TV itself kills the connection for some reason and then HASS just gives up or can’t recover.

Maybe someone else has a suggestion

With the latest PR and ADB integration, some Android devices seem to have a problem with the upstream lib (python-adb). As far as I understand the Python implementation doesn’t support multi stream to the device, so a simple component update (which quickly sends several commands) can break the connection. I can definitely say that I got the same error as you because of that.

This is being discussed here: https://github.com/google/python-adb/issues/136

Interesting. Thanks for letting me know. I’ll make sure to keep an eye on that thread.

As @a1ex4 said, the issue is with the Python adb package. I tried to handle the errors in the component. For some people it works flawlessly, other people experience issues.

Here are some things to try.

  1. See the “ADB Troubleshooting” section on the Fire TV documentation page.

  2. Set the get_source and get_sources configuration options to false:

    media_player:
      - platform: firetv
        name: Fire TV 3
        host: 192.168.0.123
        get_source: false
        get_sources: false
    
  3. Try the latest version of the official component, which catches BrokenPipeError exceptions: firetv.py.

  4. Try this branch of my custom component, which wraps all of the ADB commands in the update function individually: firetv.py (custom component).

Thanks for the help!

I already tried 1 and 3 without luck. While trying 4, I noticed that there’s a missing _ on line 340, causing a syntax error to pop up. However, after adding this, it still didn’t work. I’m getting the same errors as before:

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

Couldn't connect to host: [ip]:5555, error: Connection reset by peer

Then I tried tinkering with the get_source* config parameters. As it turns out, disabling the get_sources parameter works mostly. I’m able to control the Fire TV and show its status. But after I pressed pause (which worked) and then play (which worked as well), it died and I got the Couldn’t connect to host error again. So it seems like it doesn’t like sending more than a few commands per minute… This by the way is true for both versions.

Thanks for the feedback. So even with #4 it disconnects and then it never successfully reconnects? Could you please post a log so I can try to figure out exactly what’s going on.

At least the “get_source*” solution kinda works :confused:. I don’t know if there’s any more that can be done in the component to overcome the limitations of the adb package.

Yeah, I’ll probably have to wait for a fix for that. I’ll still attach the log file, just in case. If you’d like to add some more debug logging, I’m happy to test that as well.

Configuration:

media_player:
  - platform: firetv
    name: Feuerglotze
    host: 192.168.13.92
    adbkey: "/home/homeassistant/.homeassistant/adbkey"
    get_source: true
    get_sources: false

Log:

2018-12-06 14:52:03 INFO (SyncWorker_17) [homeassistant.components.media_player.firetv] Setup Fire TV at 192.168.13.92:5555 using adbkey='/home/homeassistant/.homeassistant/adbkey'
2018-12-06 14:52:28 ERROR (SyncWorker_16) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:52:39 WARNING (SyncWorker_6) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:52:39 ERROR (SyncWorker_6) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:52:50 WARNING (SyncWorker_5) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:52:50 ERROR (SyncWorker_5) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:01 WARNING (SyncWorker_12) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:53:01 ERROR (SyncWorker_12) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:12 ERROR (SyncWorker_5) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:12 ERROR (SyncWorker_5) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:23 WARNING (SyncWorker_13) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:53:23 ERROR (SyncWorker_13) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:34 WARNING (SyncWorker_6) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:53:34 ERROR (SyncWorker_6) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:45 WARNING (SyncWorker_5) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:53:45 ERROR (SyncWorker_5) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:53:56 WARNING (SyncWorker_11) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:53:56 ERROR (SyncWorker_11) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:07 WARNING (SyncWorker_13) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:54:07 ERROR (SyncWorker_13) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:18 WARNING (SyncWorker_13) [root] Couldn't connect to host: 192.168.13.92:5555, error: Broken pipe
2018-12-06 14:54:18 ERROR (SyncWorker_13) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:28 ERROR (SyncWorker_14) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:28 ERROR (SyncWorker_14) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:39 WARNING (SyncWorker_18) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:54:39 ERROR (SyncWorker_18) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:50 ERROR (SyncWorker_6) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:54:50 ERROR (SyncWorker_6) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2018-12-06 14:55:01 WARNING (SyncWorker_9) [root] Couldn't connect to host: 192.168.13.92:5555, error: Connection reset by peer
2018-12-06 14:55:01 ERROR (SyncWorker_9) [homeassistant.components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update

(and it just goes on and on like this…)

Thanks. Which version of the component are you using: the built-in component, #3, or #4?

That was #4, but the logs don’t look much different on the other versions.

I haven’t forgotten about this issue! It’s an issue with the Python adb package that occurs on some systems, and I don’t know what can be done to fix it.

The good news is, I’ve been working on an Android TV component with @a1ex4 that can use the adb package or a package called pure-python-adb that performs ADB commands by communicating with the ADB binaries. Once we get that working well, I’ll do the same for the Fire TV component.

Oh well glad someone is working on that lol. Browsing the forum cause I am having the same issue, my log looks just like that!

Will you post in this thread when you have that new update?

Yeah, I’ll post in this thread. @a1ex4 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.

Here’s an initial attempt at allowing the component to use the “pure-python-adb” library: Native support for Fire TV

Let’s direct any future discussions to that thread.