[Deprecated] Native support for Fire TV

So one issue was, that HA changed to python 3.7 in the new release. So the deps folder needed to be changed from python3.6 to python3.7.

But while the adb addon connects to the firetv just fine, the component itself does not:
17_12h32m51s_002_

17_12h33m25s_003_

So maybe there is more that need to be changed for 0.88
If you need information, let me know!

It’s using the Python ADB implementation to connect, not the ADB server (otherwise the log message would differ). What does your configuration look like? It should include adb_server_ip. If it does, then I suspect the current built-in component is being used, not this custom component. Perhaps with the restructuring of the custom component/platform directory structure, something needs to change in the configuration?

You’re right, I commented the adb_server_ip config out and forgot to add it back.
But that added, I get this error:

Edit: Fire TV was down, now it works :smiley:

First off, thank you. I just purchased an Insignia Fire TV edition to replace my old TV and Fire TV Gen2, and was saddened to find that the standard firetv component (https://www.home-assistant.io/components/media_player.firetv/) couldn’t control or even connect to the new Insignia TV. I managed to install your custom component and the ADB server on Hass.io, and it’s connecting!

I too am wondering about the play/pause status as it seemed to register properly using the standard firetv component. I realize you know more than I do on this subject, so I trust when you say it’s a limitation of adb commands. I just found it odd that the standard component seemed to recognize play.pause. Thanks again!!

State detection

This component works by polling your Fire TV device at regular intervals. It uses the output of ADB commands to determine properties – namely, screen_on, awake, wake_lock_size, and current_app – and uses these to predict the state of the device. The result will be the same regardless of whether you’re using the Python ADB implementation or an ADB server. This method works well for detecting whether the device is off or idle (i.e., in screensaver mode). However, it is not so accurate when it comes to detecting whether or not the device is playing, paused, or on standby. This is a difficult task, since not all Fire TV apps adhere to the same API (as I understand it) and also because many apps aren’t for playing videos at all, and so this state detection doesn’t really apply.

The code that does the state detection has been moved into the firetv package in the update function. If you’d like to try to improve it, you can use the media_player.firetv_adb_cmd service to send ADB commands to the device and view their output, if any, in the Home Assistant log (logging level = INFO). For example, if you send the command

"dumpsys power | grep 'Display Power' | grep -q 'state=ON' && echo -e '1\\c' || echo -e '0\\c'"

(which is the concatenation of the SCREEN_ON_CMD and SUCCESS1_FAILURE0 strings), then the output will be “1” if the device is on and “0” if it is off. It may be necessary to determine the state differently depending on the current app, as indicated by the “TODO” in the update function.

You’re welcome to submit pull requests with improved state detection to the firetv repository.

I’m going to step away from this project for personal reasons. While it’s been fun tinkering and trying to make this component as good as possible, I simply have more important things that I need to focus on right now.

Before signing off, here’s where things are at.

Pull requests

I was hoping to get the latest changes integrated into Home Assistant before the next release, especially because I think the changes to the structuring of custom components may cause issues for people. Unfortunately, my pull requests haven’t gotten approved yet, so I think this will have to wait. Here are my current and planned pull requests.

  1. Bump firetv to 1.0.8 (#21131). This lays the groundwork for the subsequent pull requests.
  2. Add ADB server functionality to Fire TV component (#21221). This allows for using an ADB server, such as the Hassio addon, for sending ADB commands. To accomplish this, it introduces the adb_server_ip and adb_server_port configuration entries. It is a breaking change, due to the fact that it removes the get_source configuration option. Here is the corresponding documentation pull request: Add ADB server functionality to the Fire TV component (#8641).
    • Originally, I was going to submit this after pull request #1 had gone through, under the assumption that it would be easier to get two smaller PRs approved. However, after waiting a few days I decided to submit this before #1 had been merged, thinking that this demonstrated a reason for bumping the version of the firetv package and would perhaps be more likely to get reviewed and approved. It contains all of the changes in the first pull request, so whether it gets approved on its own or whether it gets approved after #1 makes no difference.
  3. (Planned pull request) Register ‘media_player.firetv_adb_cmd’ service. I haven’t yet submitted a pull request for this, since it depends on pull request #2. This links to a branch of my home-assistant repo that contains all the changes that I intended to submit as a pull request. In addition, here is a link to a branch of my home-assistant.io repo that contains the corresponding documentation update: firetv_adb_cmd documentation.

Custom component

Latest version: firetv.py

This component is effectively the same as the final version that I had planned for the built-in component, with the one difference being that it is backwards compatible.

Help wanted

If someone could please see these changes through to completion and integration into Home Assistant, that would be much appreciated.

5 Likes

Thanks for all your work Jeff!

1 Like

Hi!
After updating to 0.88.2 the Fire TV card disappeared. The notification shows:
image

Did anyone else run into that? Any idea how to solve it?

It looks like its complaining of configuration parameters that are no longer valid,
so if correct you’ll need to delete those parameter.

I just started using Fire TV for the first time in 0.88.1 (and still using 0.88.1).
Here is my configuration (under media_player):

  - platform: firetv
    name: Fire TV 
    host: <IPAddressOfFireTV>
    adbkey: <configDirectory>/.homeassistant/android/adbkey

Which fireTV (stick) are you using?
On my 4K one, this config didn’t work previously, I had to do it with the Android debug bridge Addon.

I have a Gen2 (1K). I’m not using the add-on, so it may be different…but I would think not very much different. I also have to use the Android Debug Bridge to build the keys and such.

Tried the native way again (getting the keys using Android Debug Bridge) and then using the configuration you are showing.
FireTV Shows up in Lovelace and I can control it once, then it disconnects and won’t reconnect until I restart HA. It’s the same as described here.

It’s not seeing your custom component due to how they are picked up now, so it’s not overwriting the inbuilt one.
Should be called media_player.py in a folder named after the component, so…

firetv/media_player.py

1 Like

How do I use/install the adb server if I do not use hass.io? I tried running both:
adb -a server nodaemon &
adb start-server &

but the commponent will not connect. Should I use your below docker script as a service?
https://raw.githubusercontent.com/JeffLIrion/hassio-adb-server-addon/master/adb_server/run.sh

@jaaem what does your yaml look like? I also noticed that you need to run adb connect <IP> on the device you wish to connect with in order for it to connect.

Thanks. I edited a version of the docker script, and it seems to work now. I only hard coded one device, but hopefully it will work

#!/bin/bash

DEVICES=(192.168.1.167)

echo "Starting up ADB..."

while true; do
  adb -a server nodaemon > /dev/null 2>&1
  sleep 10
done &

echo "Server started. Waiting for 30 seconds..."
sleep 30

echo "Connecting to devices."
for device in $DEVICES; do
   adb connect $device
done
echo "Done."

while true; do
  for device in $DEVICES; do
    adb connect $device > /dev/null 2>&1
  done
  sleep 60
done
2 Likes

This unfortunately has been a headache and a half trying to get it work with HA. Both HA and ADB containers are running in unraid. If I just pull sorccu/adb container and then manual connect and restart ha. It connects np. Once I try adding a script for it to auto connect it does connect np but HA will not connect to adb at all. So stuck with this. This is the script im using. The Jaaem one I tried but the container just shuts down right away.

#!/bin/sh

sleep 5

DEVICES="192.168.0.206"

echo "Connecting to devices."
for device in $DEVICES; do
   adb connect $device
done
echo "Done."

while true; do
  for device in $DEVICES; do
    adb connect $device > /dev/null 2>&1
  done
  sleep 60
done

I can not use media_player.select_source to open netflix as sample in Android TV component on hassio 0.92.1 and Fire 4k Stick
script file:

      mo_youtube:
       sequence:
         - service: media_player.turn_on
           data:
             entity_id:  media_player.fire_4k_tv
         - service: media_player.select_source
           data:
             entity_id: media_player.living_room_tv
             source: "HDMI 1"
         - service: media_player.select_source
           data:
             entity_id:  media_player.fire_4k_tv
             source: 'com.netflix.ninja'

Besides, how can i get app id of firefox browser (or other apps) on amazon fire 4k tv? Please help me @JeffLIrion

Try sending this command via the androidtv.adb_command service and see if it launches Netflix:

monkey -p android.intent.category.LAUNCHER -c com.netflix.ninja 1; echo $?

Regarding your script, you might need to wait until HA recognizes that the device is on before calling the media_player.select_source service. You can do that via:

  - wait_template: "{{ not is_state('media_player.fire_4k_tv', 'off') }}"
    timeout: 00:00:10

To find out the app_id, open the app and look at the app_id attribute for the media player on the states page.

Thanks for replying @JeffLIrion.

=> I tried but it can not launch Netflix