[Deprecated] Native support for Fire TV

Yes, but I need to know which ADB command is creating the issue. I don’t know if it randomly fails or if it’s one command that’s causing all the trouble.

2018-12-30 16:36:19 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is not available
2018-12-30 16:36:19 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is attempting to re-connect...
2018-12-30 16:36:19 WARNING (SyncWorker_17) [root] Couldn't connect to host: 192.168.1.97:5555, error: Connection reset by peer
2018-12-30 16:36:19 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' failed to re-connect

Are there any other log messages, or does it just repeat those same 4 over and over?

2018-12-30 22:29:09 ERROR (SyncWorker_13) [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-30 22:29:19 WARNING (SyncWorker_8) [root] Couldn't connect to host: 192.168.1.97:5555, error: Broken pipe
2018-12-30 22:29:52 ERROR (SyncWorker_14) [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-30 22:30:03 WARNING (SyncWorker_2) [root] Couldn't connect to host: 192.168.1.97:5555, error: Connection reset by peer

There’s no debugging log statements in that log excerpt.

I changed the debugging version of the component (link: firetv.py) so that it logs at the “critical” level, not the “debug” level. Please try the latest version and post your log. And preferably the full, unabridged log. I need to see where exactly the error is occurring.

I let it run for quite a while.
There are only a few errors out of all the connection attempts.

log.yaml (2.4 MB)

Thanks for posting that log. Here’s a revised version of the component. This version only sends 1 ADB command per update, so hopefully it will minimize the number of errors.

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

How do I turn off critical logging?

You could replace every occurrence of _LOGGER.critical with, say, _LOGGER.debug. Or you could just delete the critical logging statements.

How is this version of the component working? If there are still issues, please post a log!

Been running for 5 hours and no errors yet!

Awesome, thanks for your help with testing and for all the feedback!:+1:

This latest version of the component minimizes the number of ADB commands sent to the device and should help the issue where the device is constantly unavailable. You may need to remove firetv* folders from your /config/deps/** folders so that the latest version of the forked package installs from GitHub. You might also need to set get_sources: false in your Fire TV configuration if you’re still getting errors.

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

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

Thanks - I have gone through this from scratch and am still getting the same error messages in the log.

Originally I created the adbkey on a laptop and copied it to my hassio which runs on ubuntu. I deleted those files and created new ones from the ubuntu server, copied those to my hassio docker container and tried that. I made sure to disconnect the adb connection from ubuntu and even deleted the adbkey files from the location they were created in by adb.

I have changed the file permissions with chmod 777 so think the permissions are okay.

Any other thoughts please?

Try specifying the full path to your adbkey file. It’s probably:

adbkey: '/config/homeassistant/android/adbkey'

Dear, here the last word adbkey is file itself,
Don’t misunderstand,
It locates file itself

I’ve had the native component working for quite some time with an older FireTV Stick, but recently snagged a FireTV 4K. It has been nothing but problems for ADB communication. My log was flooded with this:

2019-01-04 04:26:14 ERROR (Thread-18) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2019-01-04 04:26:47 ERROR (Thread-22) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2019-01-04 04:27:09 ERROR (Thread-15) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2019-01-04 04:27:31 ERROR (Thread-8) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2019-01-04 04:27:53 ERROR (Thread-20) [custom_components.media_player.firetv] Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2019-01-04 04:28:04 WARNING (Thread-14) [root] Couldn't connect to host: 192.168.1.194:5555, error: Connection reset by peer

With the custom_component from @JeffLIrion the errors have calmed down but not gone completely away. However, the device is not always completely unavailable.

Opening up a Python console and trying to execute commands works sometimes, but then I get all sorts of random whacky errors:

>>> ftv.get_properties()
(True, True, False, {'activity': 'com.amazon.tv.launcher.ui.HomeActivity_vNext', 'package': 'com.amazon.tv.launcher'})
>>> ftv.get_properties()
(True, True, False, {'activity': 'com.amazon.tv.launcher.ui.HomeActivity_vNext', 'package': 'com.amazon.tv.launcher'})
>>> ftv.get_properties()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 457, in get_properties
    output = self.adb_shell(SCREEN_ON_CMD + " && " + AWAKE_CMD + " && " + WAKE_LOCK_CMD + " && " + CURRENT_APP_CMD)
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 159, in _adb_shell_python_adb
    return self._adb.Shell(cmd)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 439, in StreamingCommand
    for data in connection.ReadUntilClose():
AttributeError: 'NoneType' object has no attribute 'ReadUntilClose'
>>> ftv.get_properties()
(True, True, False, {'activity': 'com.amazon.tv.launcher.ui.HomeActivity_vNext', 'package': 'com.amazon.tv.launcher'})
>>> ftv.get_properties()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 457, in get_properties
    output = self.adb_shell(SCREEN_ON_CMD + " && " + AWAKE_CMD + " && " + WAKE_LOCK_CMD + " && " + CURRENT_APP_CMD)
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 159, in _adb_shell_python_adb
    return self._adb.Shell(cmd)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 439, in StreamingCommand
    for data in connection.ReadUntilClose():
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 144, in ReadUntilClose
    cmd, data = self.ReadUntil(b'CLSE', b'WRTE')
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 135, in ReadUntil
    self.remote_id, remote_id))
adb.adb_protocol.InvalidResponseError: Incorrect remote id, expected 10267 got 10266
>>> ftv.get_properties()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 457, in get_properties
    output = self.adb_shell(SCREEN_ON_CMD + " && " + AWAKE_CMD + " && " + WAKE_LOCK_CMD + " && " + CURRENT_APP_CMD)
  File "/srv/homeassistant/lib/python3.5/site-packages/firetv/__init__.py", line 159, in _adb_shell_python_adb
    return self._adb.Shell(cmd)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_commands.py", line 376, in Shell
    timeout_ms=timeout_ms)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 411, in Command
    return ''.join(cls.StreamingCommand(usb, service, command, timeout_ms))
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 438, in StreamingCommand
    timeout_ms=timeout_ms)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 381, in Open
    timeout_ms=timeout_ms)
  File "/srv/homeassistant/lib/python3.5/site-packages/adb/adb_protocol.py", line 247, in Read
    'Unknown command: %x' % cmd, cmd, (arg0, arg1))
adb.adb_protocol.InvalidCommandError: ('Unknown command: 54525731', 1414682417, (2628421, 256))
>>>

I was hoping that the recent changes would fix these issues, but oh well. Thanks for the feedback.

If you’re not using Hass.io, you could try installing the ADB binaries and connecting to your devices in that way. Using the latest version of the component (which it sounds like you already are), the configuration should be:

media_player:
  - platform: firetv
    name: Fire TV
    host: 192.168.0.111
    adb_server_ip: 127.0.0.1

If you are using Hass.io, please see my forthcoming post.

A number of users have had issues with the ADB connection, especially to newer devices. This is due to issues/limitations with the Python adb package. I’ve tried to debug these issues, but I haven’t managed to fix them. But there is something else that we can try…

Rather than using the adb Python package to communicate with the device, we can use the actual ADB binaries. You’ll need to use this version of the component: firetv.py. The configuration should look like:

media_player:
  - platform: firetv
    name: Fire TV
    host: 192.168.0.111
    adb_server_ip: 127.0.0.1

Setup for non-Hass.io users

You’ll need to install the ADB binaries, which you already did when setting up the component; for reference, see the Fire TV documentation. I think that before starting Home Assistant, you’ll need to start the ADB server and connect to your devices.

Addon for Hass.io users (work in progress!)

I tried to create an ADB server addon. Currently, it doesn’t work – it won’t install. Contributions are welcome! The repository and all the info you need can be found here: https://github.com/JeffLIrion/hassio-adb-server-addon.

@JeffLIrion first off, thank you for all the time and work you’ve done on this to try and get it working.

Last night, I did try the adb_server_ip config setting and running the ADB binaries with the pure_python_adb branch, and for some reason, the firetv media players never showed up at all in HA. No error messages about setup failure in the logs, which I thought was strange. I reverted my configs and started getting the same error messages. I gave the machine running Hassbian a full reboot and still had the same messages. Then I disabled the custom component completely and went back to the firetv component included in 0.84.5.

After that, everything seemed to be working for both of my firetv devices and still is this morning. I still have enable_source and enable_sources to false for the 4K.

The only thing that has changed is I am using your fork of python-firetv still.

I spoke too soon! All the same errors are back in action.

I’ll try goofing around with the ADB binaries later this evening without HA and see if I get better results and report back.