[Deprecated] Native support for Fire TV

Thanks. I’m using Hass.io on Ubuntu, is it possible to change these files in there?

Yes, you just install it as a custom component.

Copy the media_player/firetv.py to your custom_components folder ( custom_components/media_player/firetv.py ) in your configuration directory. If you do not have a custom_components folder, then you will need to create it.

(From the README)

EDIT: Right before I submitted this comment, I changed both of the ‘get_source’ settings true, and it looks like it’s working now. It shows as ‘standby’ when on the home screen, and ‘idle’ when on the screensaver, and ‘playing’ while playing. However, it still shows as playing when paused. Not sure if it’s possible to detect paused or not, but that would be nice to have. Anyways, I’m going to go ahead and submit this comment just in case it might help someone else out. Thanks for all your help with this.

OK. So I think it’s using the new code because it has a warning about it in the logs. And I’m no longer getting a status of ‘disconnected’. However, the status appears to always be ‘playing’ unless I put the Kindle to sleep, and the status changes to idle. Also, when the screen saver starts it shows as idle. However, just moving around in the menu causes it to go to a ‘Playing’ status, even though I’m not playing anything.

Is that supposed to be the normal behavior for this?

And now it seems that it is only working for my FireStick and not my FireTV (The 4k square dongle. This one). The FireStick is the regular version, not the 4k. The Square FireTV always shows ‘unavailable’, unless I put it to sleep. When it goes to sleep, it shows ‘idle’. All other times it shows ‘unavailable’. Except for one time where I think it went to playing when I opened the Kodi app. But I haven’t been able to consistently reproduce that. It’s being pretty flaky. I’ve tried using it with the Ethernet adapter, along with WiFi, and get the same results. When it goes to ‘playing’ every once in a while, it seems to go back to ‘unavailable’ about 30 seconds later.

It doesn’t make any sense to me. Any ideas as to what I can try here?

The state detection definitely isn’t perfect. I just use it to detect whether whether it’s on or off. If someone would like to improve the state detection, they’re welcome to submit a pull request, either to my custom component or the official HA component.

It seems to have trouble with certain Fire TV devices. I can think of 2 things to try.

  1. The custom component that you’re using can use the “pure-python-adb” library to send its ADB commands, which really just makes calls to the ADB binaries. You could try installing ADB on some computer and then providing the adb_server_ip: xx.xx.xx.xx configuration entry. I don’t know that anyone has tried this yet, so if you do please let me know how it works.
  2. You could modify the code in the component and then try sending some ADB commands and checking the output in the log. Use the media_player.firetv_adb_shell service and try the following JSON parameters:
# screen_on (look for 'state=ON')
{"entity_id": "media_player.YOUR_FIRE_TV", "cmd": "dumpsys power | grep 'Display Power'"}

# awake (look for 'Awake')
{"entity_id": "media_player.YOUR_FIRE_TV", "cmd": "dumpsys power | grep mWakefulness"}

# wake_lock (look for 'size=0')
{"entity_id": "media_player.YOUR_FIRE_TV", "cmd": "dumpsys power | grep Locks"}

# current_app
{"entity_id": "media_player.YOUR_FIRE_TV", "cmd": "dumpsys window windows | grep mCurrentFocus"}

As far as modifying the component, just comment out most of the update function, like so:

    @adb_decorator(override_available=True)
    def update(self):
        """Get the latest date and update device state."""
        # Check if device is disconnected.
        if not self._available:
            self._running_apps = None
            self._current_app = None

            # Try to connect
            self._available = self.firetv.connect()

        '''# If the ADB connection is not intact, don't update.
        if not self._available:
            return

        # Check if device is off.
        if not self.firetv.screen_on:
            self._state = STATE_OFF
            self._running_apps = None
            self._current_app = None

        # Check if screen saver is on.
        elif not self.firetv.awake:
            self._state = STATE_IDLE
            self._running_apps = None
            self._current_app = None

        else:
            # Get the running apps.
            if self._get_sources:
                self._running_apps = self.firetv.running_apps

            # Get the current app.
            if self._get_source:
                current_app = self.firetv.current_app
                if isinstance(current_app, dict)\
                        and 'package' in current_app:
                    self._current_app = current_app['package']
                else:
                    self._current_app = current_app

                # Show the current app as the only running app.
                if not self._get_sources:
                    if self._current_app:
                        self._running_apps = [self._current_app]
                    else:
                        self._running_apps = None

                # Check if the launcher is active.
                if self._current_app in [PACKAGE_LAUNCHER,
                                         PACKAGE_SETTINGS]:
                    self._state = STATE_STANDBY

                # Check for a wake lock (device is playing).
                elif self.firetv.wake_lock:
                    self._state = STATE_PLAYING

                # Otherwise, device is paused.
                else:
                    self._state = STATE_PAUSED

            # Don't get the current app.
            elif self.firetv.wake_lock:
                # Check for a wake lock (device is playing).
                self._state = STATE_PLAYING
            else:
                # Assume the devices is on standby.
                self._state = STATE_STANDBY'''

Hi,

I’m struggling a little with getting this setup and wondered if anyone can point me in the right direction for where I ought to be looking for my mistakes.

I’ve got this set up, have created the adbkey and when I start up Hassio I get the following errors in the Hassio log:

Could not connect to Fire TV at 192.168.1.24:5555 using adbkey='/homeassistant/android/adbkey'

and:

Couldn't connect to host: 192.168.1.24:5555, error: No such file or directory

I’ve checked that my key is in the right directory and the Fire Stick IP is correct.

I’m sure the problem here is somewhere between chair and keyboard, but I can’t quite see it!

Thanks in advance.

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

Using the latest firetv.py i’m still getting connection errors. It was working at one point.

Couldn’t connect to host: 192.168.1.97:5555, error: Connection reset by peer
Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update

I can connect using terminal on OSX and have verified the server port and address are the same.

@simpat1zq, @Picnic, and anyone else having issues with the state always being ‘unavailable’, please try this version of the component and post your logs.

You’ll need to set the log level for the Fire TV component to debug, which you can do in your configuration as follows:

logger:
  default: error
  logs:
    homeassistant.components.media_player.firetv: debug

Same error.

Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2:49 PM custom_components/media_player/firetv.py (ERROR)
Couldn’t connect to host: 192.168.1.97:5555, error: Connection reset by peer
2:49 PM deps/lib/python3.6/site-packages/firetv/init.py (WARNING)
Failed to execute an ADB command; will attempt to re-establish the ADB connection in the next update
2:49 PM custom_components/media_player/firetv.py (ERROR)
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.

2:48 PM loader.py (WARNING)

There should be more logging statements than that… I need to see the output of the debug log entries. Did you set the log level for the Fire TV component to debug? Alternatively, you could replace all the _LOGGER.debug commands in the component with _LOGGER.critical.

this is my config log setting:

logger:
  default: error
  logs:
    homeassistant.components.media_player.firetv: debug

Is that all the logging that it showed? Does the version of the component that you’re using say import re near the top?

Looks like it had one successful connection:

> 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is not available 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is attempting to re-connect... 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' successfully re-connected 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' <checking the 'screen_on' property> 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is on (output = 'Display Power: state=ON ') 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' <checking the 'awake' property> 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' is not idle (output = ' mWakefulness=Awake mWakefulnessChanging=false ') 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' <checking the 'running_apps' property> 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' running apps are: 'android.ext.services', 'com.amazon.vizzini', 'com.amazon.imp', 'com.amazon.tcomm', 'com.amazon.tv.devicecontrol', 'com.baronkiko.launcherhijack', 'com.amazon.client.metrics', 'net.i.akihiro.halaunches', 'logcat', 'com.amazon.tv.ime', 'com.amazon.connectivitycontroller', 'com.amazon.discoveryservice.core.android', 'eu.thedarken.wl', 'com.silicondust.view', 'com.amazon.whisperlink.core.android', 'com.amazon.kso.blackbird:BlackbirdServiceProcess', 'com.amazon.tv.notificationcenter', 'com.amazon.kindle.cms', 'com.android.proxyhandler', 'com.amazon.device.software.ota', 'com.amazon.webview.chromium:AWVMetricsProcess', 'com.amazon.tv.settings', 'com.android.defcontainer', 'com.android.providers.tv', 'com.amazon.avod', 'com.amazon.tv.launcher', 'com.amazon.venezia', 'com.amazon.ssm', 'com.amazon.device.messaging', 'com.semperpax.spmc16', 'com.amazon.kso.blackbird' 2018-12-30 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' <checking the 'current_app' property> 2018-12-30 15:04:47 ERROR (SyncWorker_17) [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 15:04:47 DEBUG (SyncWorker_17) [custom_components.media_player.firetv] 'Fire TV' current_app is 'None'

Please set get_sources: false in your Fire TV configuration and post a log.

OK that worked. My goal in connecting over ADB was to be able to send custom commands.
Do you know the correct way to send something like:
adb shell monkey -p com.semperpax.spmc16 -c android.intent.category.LAUNCHER 1
from HASSIO?
I’ve tried creating a shell command and a command line switch but it doesn’t seem like the ADB command is recognized.
Thanks for your help!

I spoke to soon, still getting the same unable to connect errors.

I created a service called media_player.firetv_adb_shell. You can send your command by giving it the following data:

{"entity_id": "media_player.YOUR_FIRE_TV", "cmd": "monkey -p com.semperpax.spmc16 -c android.intent.category.LAUNCHER 1"}

The result will be logged at the level INFO.

1 Like

More logs, please!

It’s exactly the same as before.
Could not connect and failed to execute adb.