[TESTERS NEEDED] Android TV / Fire TV key generation

I’m trying to simplify the setup necessary to use the Android TV (and Fire TV) integration.

For reference: Android TV documentation

TLDR

Use this custom component (HACS compatible): https://github.com/JeffLIrion/ha-androidtv

Your config should look like:

media_player:
  - platform: androidtv
    name: MEDIA PLAYER NAME
    host: 192.168.0.111

# optional config entries: apps, device_class, state_detection_rules, turn_on_command, turn_off_command
  • Do not include adbkey or adb_server_ip in your config

Instructions

  1. If you are running an ADB server, stop it
  2. Reboot your Android TV / Fire TV device
  3. Turn on your Android TV / Fire TV device, then reboot HA

You should see a pop-up on your TV asking if you want to trust this computer. Click yes. You should have 60 seconds to do this. If you have less time, please let me know. If you wait longer than 60 seconds, please tell me what happens.

Details (for those interested)

There are two ways to communicate with Android TV / Fire TV devices:

  1. ADB server (e.g., the Hass.io add-on)
  2. Python ADB implementation
    • This was not reliable for newer devices, but it should be fixed in HA 0.100
    • Currently, this requires you to connect to the device and copy the key to your HA config before it can be integrated

What this custom component does is generate the key for you and allow you 1 minute to approve the connection, avoiding all of the setup necessary for the Python ADB approach. The generated key is stored as <config>/.storage/androidtv_adbkey.

I have a pull request that adds this functionality to HA, but before it gets reviewed I need to know if this actually works.

4 Likes

Nice… but I don’t see any changes here, what should I install to get your changes?

That’s because I copy & pasted the wrong link. I fixed it.

1 Like

Ok, makes sense… but how to install this? …adding the hacs-url as repo didn’t add it, sorry if this is a stupid question but I’m still quite new to hass

Method 1: HACS

Method 2: Copy this androidtv folder (including subfolders) into the folder <config_directory>/custom_components

  • If the <config_directory>/custom_components folder doesn’t exist, create it

I can test this in a few hours. I hope it works!

Hey @JeffLIrion, I installed the custom component via method 2. I tested one scenario but now I’m at a standstill because the Fire TV is refusing ADB connections to HA after the first failed attempt. More on that later.

Tested on:
Amazon Fire TV Stick 4K
Home Assistant 0.99.3 via Hass.io on Docker

I tested to see what would happen if I waited over 60 seconds before accepting the ADB connection on the Fire TV.

  1. COnfigured FIre TV to WiFi, set static IP (on router side), and accept ADB connections
  2. I copied the \androidtv folder to \customcomponents
  3. Added AndroidTV configuration to configuration.yaml
  4. Restarted Home Assistant

After HA restarted, the pop-up appeared on the Fire TV to accept the ADB connection.

  1. I waited ~65 seconds before accepting the ADB connection and closing the pop-up.
  2. The Fire TV was not added to Home Assistant (as expected)

Sounds like this scenario is working as planned. Now, I wanted to test what would happen if I accepted the ADB connection in under 60 seconds. However, the pop-up dialog no longer appears even after restarting Home Assistant. What do I need to do to attempt the ADB connection again?

I can see this being a problem for people who miss the 60-second window to approve the ADB connection. Some additional instructions or log messages would help me out.

Edit: forget what I said earlier about the Fire TV no longer connecting to Home Assistant. After taking a break for an hour, I see the Fire TV in the Home Assistant UI now. I’ll test again after deleting the androidtv_adbkey files.

Will this work for multiple Fire TVs? I have 3 of them currently. How will it differentiate what key belongs to the proper Fire stick?

So it worked?

I was going to suggest deleting the config/.storage/androidtv_adbkey files and trying again.

Edit: it may be because HA tried to setup the component again too soon (probably before a minute had even elapsed). I think when it fails multiple times to setup a component, it waits longer between retries. Maybe that’s why it worked an hour later?

You can use the same key for multiple Fire TV’s. But accepting all 3 keys within 60 seconds could be an issue…

Yes, it worked. Though is that the expected result when waiting over a minute before accepting the ADB connection pop-up? I think it’s fine…

For my second test, I deleted the androidtv_adbkey files, restarted HA, accepted the Fire TV ADB connection pop-up, and the Fire TV appeared as a media player in Home Assistant. It works!

I’ll do more testing to see whether the Fire TV stays connected to Home Assistant. I used the previous custom component of yours and had some issues where the Fire TV entity would disappear from Home Assistant. I’ll need to look more into that and get back to you.

I dunno, I need to figure out what the behavior should be and consult with HA higher-ups.

How long ago was that? I fixed some bugs in the last couple weeks. At least, I think they’re fixed…

I want to say i installed your custom component on a different HA instance and Fire TV a week ago. I’ll test again with your latest component soon.

Anyways, to see if I could replicate the issue I let the Fire TV play a video off of Amazon Prime. Didn’t see any issues and the Fire TV state was reporting Paused which is accurate enough for me. There were a few times when the Fire TV state went to Unavailable, but it didn’t happen during playback.

I thought I fixed the issue where the device would become unavailable… If you collect debug logs, I’ll take a look at them. Just set the log level via the logger.set_level service:

{"custom_components.androidtv.androidtv.adb_shell": "debug"}

When you’re done, you can set it back to warning, error, etc.

I made one simple change in an attempt to fix the “unavailable” issue. You could download everything again, or you could apply the change yourself: https://github.com/JeffLIrion/ha-androidtv/commit/455341a67b186bb083f71b5d7db6dc054c7b436c

i am getting this error

2019-10-11 08:17:12 ERROR (SyncWorker_15) [custom_components.androidtv_2.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: 'NoneType' object has no attribute 'ReadUntilClose'
2019-10-11 08:17:45 ERROR (SyncWorker_13) [custom_components.androidtv_2.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: Incorrect remote id, expected 4 got 3
2019-10-11 08:18:07 ERROR (SyncWorker_15) [custom_components.androidtv_2.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ('Unknown command: 54525731', 1414682417, (1093, 256))
2019-10-11 08:18:29 ERROR (SyncWorker_6) [custom_components.androidtv_2.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ('Unknown command: 615731ba', 1633104314, (1277191531, 1936417647))
2019-10-11 08:18:51 ERROR (SyncWorker_0) [custom_components.androidtv_2.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: ('Unknown command: 6f6f672e', 1869571886, (778398823, 1919184481))

Are you using the most recent version of the custom component?

I need debug logs in order to try to fix any issues. You can set the log level to debug for the adb_shell package via the logger.set_level service:

custom_components.androidtv.adb_shell: debug

You can open an issue and post the log here: https://github.com/JeffLIrion/adb_shell

Yes the latest version.

I reverted the last change. I still could use some debug logs!:+1:

You’re not using the custom component. I can tell because there is no 'ReadUntilClose' method used in the custom component, only in the built-in component up until HA 0.100.