JeffLIrion / homeassistant_native_firetv

43 stars 16 forks source link

Don't Get the ADBKey Files #5

Closed TexanScot closed 6 years ago

TexanScot commented 6 years ago

Following the guide I successfully installed Android Studio on my Mac however I seem to have done something wrong as I don't see the ADBkey files being created on my machine. I have listed below the commands and output that I received.

$ ./adb connect 192.168.10.163
adb server version (32) doesn't match this client (40); killing...
* daemon started successfully
failed to authenticate to 192.168.10.163:5555

At this point I realized that I need to stop and restart the server:

$ ./adb kill-server
$ ./adb start-server
* daemon not running; starting now at tcp:5037
* daemon started successfully

$ ./adb connect 192.168.10.163
failed to authenticate to 192.168.10.163:5555

At this point I received some dialog on the TV providing some kind of RSA key? and a message "Always trust this computer? (I think! - I clicked the box to say yes and the message disappeared).

./adb devices
List of devices attached
192.168.10.163:5555 device

I don't see the adbkey or adbkey.pub files on my computer, presumably because of the 'failed to authenticate' response when I tried to connect. However when I repeat the connect command I get the message that I'm already connected:

$ ./adb connect 192.168.10.163
already connected to 192.168.10.163:5555

I tried killing and restarting the server again however I still receive the same message. Could you Kindly point out where I have gone wrong and the steps that I need to take to correctly generate the necessary files.

Many thanks in advance.

TexanScot commented 6 years ago

OK, on further reading of adb documentation I noted that the key files should be stored in the $HOME/.android folder.

By default key pairs generated by the adb server are stored in the following key store directories as adbkey (private key) and adbkey.pub (public key):

Linux and Mac: $HOME/.android.

Looking in this folder I do indeed see adbkey and adbkey.pub files however they are timestamped from May of last year (I'm really not sure how these were created - possibly when I tried installing OpenVPN on the firetv stick?). I copied these files into my HA config folder however and restarted HA. Relevant output from the log file is shown below:

2018-09-16 13:03:43 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=media_player.family_room_fire_tv, old_state=None, new_state=<state media_player.family_room_fire_tv=unavailable; friendly_name=Family Room Fire TV, supported_features=23357 @ 2018-09-16T13:03:43.507101-05:00>>

2018-09-16 13:03:50 WARNING (MainThread) [homeassistant.components.media_player] Setup of platform firetv is taking over 10 seconds.
2018-09-16 13:04:40 ERROR (MainThread) [homeassistant.components.media_player] Setup of platform firetv is taking longer than 60 seconds. Startup will proceed without waiting any longer.
2018-09-16 13:04:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=media_player.family_room_receiver, old_state=<state media_player.family_room_receiver=off; friendly_name=Family Room Receiver, supported_features=18828 @ 2018-09-16T13:03:40.576838-05:00>, new_state=<state media_player.family_room_receiver=on; volume_level=0.6799999999999999, is_volume_muted=False, source=AV7, source_list=['AUDIO1', 'AUDIO2', 'AUDIO3', 'AUDIO4', 'AV1', 'AV2', 'AV3', 'AV5', 'AV6', 'AV7', 'AirPlay', 'FireTV', 'MULTI CH', 'NET RADIO', 'PHONO', 'Pandora', 'Rhapsody', 'SERVER', 'SiriusXM', 'TUNER', 'USB', 'V-AUX', 'iPod (USB)'], friendly_name=Family Room Receiver, supported_features=18828 @ 2018-09-16T13:04:41.483782-05:00>>

2018-09-16 13:04:59 WARNING (SyncWorker_3) [custom_components.media_player.firetv] Could not connect to Fire TV at 192.167.10.163:5555

Any guidance on how to proceed would be greatly appreciated!

JeffLIrion commented 6 years ago

Make sure you're not connected to your Fire TV from your computer. I believe only 1 ADB connection is allowed at a time.

TexanScot commented 6 years ago

Jeff

Thanks for your impressively quick response.

I'm afraid however that I don't quite understand what you are saying when you say "Make sure you're not connected to your Fire TV from your computer". Home Assistant is running on the same computer (Mac Book Pro) as I used to configure the connection to the FireTV. Are you saying that I need to actively disconnect the MBP via terminal prior to restarting HA?

JeffLIrion commented 6 years ago

Yeah, run the command that you used before (./adb kill-server) prior to starting Home Assistant.

TexanScot commented 6 years ago

OK, here is what I did.

1) Shutdown HA by stopping the container.

2) Connected to the firetv from terminal.

$ ./adb kill-server
$ ./adb start-server
* daemon not running; starting now at tcp:5037
* daemon started successfully
$ ./adb connect 192.168.10.163
connected to 192.168.10.163:5555
$ ./adb devices
List of devices attached
192.168.10.163:5555 device

$ ./adb kill-server

3) Restarted the HA container - log output is as before.

I have both the adbkey and adbkey.pub in my HA config folder and my configuration.yaml container the following:

  - platform: firetv
    name: Family Room firetv
    host: 192.167.10.163
    adbkey: "/config/adbkey"

Is there any way of checking whether the 'old' key files are those that the firetv presently expects or alternatively is there a way to remove keys from the Mac and firetv and start over?

JeffLIrion commented 6 years ago

This might be easier to debug in the Python interactive shell than in Home Assistant. I'd suggest doing the following.

  1. Make a Python virtual environment

  2. In that virtual environment:

    pip install pycryptodome rsa
    pip install https://github.com/JeffLIrion/python-adb/zipball/master
  3. Run the following Python commands:

    from adb import adb_commands
    from adb.sign_pythonrsa import PythonRSASigner
    from adb.adb_protocol import InvalidChecksumError
    
    serial = '192.167.10.163:5555'
    adbkey = '<path to your adbkey>'
    
    signer = PythonRSASigner.FromRSAKeyPath(adbkey)
    
    conn = adb_commands.AdbCommands().ConnectDevice(serial=serial, rsa_keys=[signer])

That should let you know whether or not your key works.

TexanScot commented 6 years ago

My Python knowledge is pretty limited however I managed (I think) to install the required modules.

  Downloading https://files.pythonhosted.org/packages/18/50/439be74dd1b63c8fe87bcb163ce2c9edb0419c77b94ee45159e20b2d714c/pycryptodome-3.6.6-cp36-cp36m-macosx_10_6_intel.whl (7.3MB)
    100% |████████████████████████████████| 7.3MB 170kB/s 
Collecting rsa
  Downloading https://files.pythonhosted.org/packages/02/e5/38518af393f7c214357079ce67a317307936896e961e35450b70fad2a9cf/rsa-4.0-py2.py3-none-any.whl
Collecting pyasn1>=0.1.3 (from rsa)
  Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 3.7MB/s 
Installing collected packages: pycryptodome, pyasn1, rsa
Successfully installed pyasn1-0.4.4 pycryptodome-3.6.6 rsa-4.0
Collecting https://github.com/JeffLIrion/python-adb/zipball/master
  Downloading https://github.com/JeffLIrion/python-adb/zipball/master
     - 296kB 40.6MB/s
Collecting libusb1>=1.0.16 (from adb==1.3.0.1)
  Downloading https://files.pythonhosted.org/packages/39/c6/a9c8c38e3a8a587cd5c32146a5156375e107e483eb2ccb80284a147921dd/libusb1-1.6.6.tar.gz (56kB)
    100% |████████████████████████████████| 61kB 46kB/s 
Requirement already satisfied: rsa in /Users/Alan/Software/Anaconda/envs/Python36/lib/python3.6/site-packages (from adb==1.3.0.1) (4.0)
Requirement already satisfied: pyasn1>=0.1.3 in /Users/Alan/Software/Anaconda/envs/Python36/lib/python3.6/site-packages (from rsa->adb==1.3.0.1) (0.4.4)
Building wheels for collected packages: adb, libusb1
  Running setup.py bdist_wheel for adb ... done
  Stored in directory: /private/var/folders/x0/0qf853q11kv9c4jby793wyt40000gq/T/pip-ephem-wheel-cache-vqbp_jra/wheels/74/8a/d2/25f3b7b102a452986746fed44769bfc8db401d10ac86aafb70
  Running setup.py bdist_wheel for libusb1 ... done
  Stored in directory: /Users/Alan/Library/Caches/pip/wheels/a5/b2/67/35a33bcfd6fc3d04e180f52079f4ec48f12250d67da7a98ab0
Successfully built adb libusb1
Installing collected packages: libusb1, adb
Successfully installed adb-1.3.0.1 libusb1-1.6.6

I then opened up the Python interpreter and executed the first command:

Python 3.6.4 |Anaconda, Inc.| (default, Mar 12 2018, 20:05:31) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from adb import adb_commands
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/adb/adb_commands.py", line 31, in <module>
    from adb import common
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/adb/common.py", line 25, in <module>
    import libusb1
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/libusb1.py", line 8, in <module>
    from usb1.libusb1 import *
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/usb1/__init__.py", line 61, in <module>
    from . import libusb1
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/usb1/libusb1.py", line 199, in <module>
    libusb = _loadLibrary()
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/site-packages/usb1/libusb1.py", line 173, in _loadLibrary
    return dll_loader('libusb-1.0' + suffix, **loader_kw)
  File "/Users/Alan/Software/Anaconda/envs/python36/lib/python3.6/ctypes/__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(libusb-1.0.dylib, 6): image not found

Did I misinterpret your guidance in trying to run the command in the interactive shell? I tried entering the commands in a simple Python script however this crashes as well when executing the line:

conn = adb_commands.AdbCommands().ConnectDevice(serial=serial, rsa_keys=[signer])

My IDE shows an 'unresolved reference' for the adb_commands.AdBCommands() statement. I think that this statement is passing the IP address and key to the firetv and that some kind of response is returned. However I don't know how to define the adb_commands variable (integer, string, etc.)

JeffLIrion commented 6 years ago

That's an odd error. Try this (in your virtual environment):

pip uninstall rsa
pip install rsa==3.4.2

And then repeat the commands in the shell, or run the script.

Also, if you copy / move your .android folder (e.g., mv $HOME/.android $HOME/backup.android), I assume you can retry connecting and it will make a new key for you.

TexanScot commented 6 years ago

Same error.

(python36) $ pip install rsa==3.4.2
Collecting rsa==3.4.2
  Downloading https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl (46kB)
    100% |████████████████████████████████| 51kB 145kB/s 
Requirement already satisfied: pyasn1>=0.1.3 in /Users/Alan/Software/Anaconda/envs/Python36/lib/python3.6/site-packages (from rsa==3.4.2) (0.4.4)
Installing collected packages: rsa
Successfully installed rsa-3.4.2

Python script is:

import adb
from adb.sign_pythonrsa import PythonRSASigner
from adb.adb_protocol import InvalidChecksumError

serial = '192.167.10.163:5555'
adbkey = "/Users/Alan/software/home-assistant/development/current_dev/adbkey"

signer = PythonRSASigner.FromRSAKeyPath(adbkey)

conn = adb_commands.AdbCommands().ConnectDevice(serial=serial, rsa_keys=[signer])

Console output:

Traceback (most recent call last):
  File "/Applications/PyCharm Edu.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm Edu.app/Contents/helpers/pydev/pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm Edu.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/alastair/PycharmProjects/untitled1/firetv.py", line 10, in <module>
    conn = adb_commands.AdbCommands().ConnectDevice(serial=serial, rsa_keys=[signer])
NameError: name 'adb_commands' is not defined

I did successfully recreate the keys earlier today using the method that you suggested hence the keys in the HA folder should now be fully up to date.

I do appreciate the time that you are spending on trying to help me.

Many thanks!

JeffLIrion commented 6 years ago

In that case, I'd suggest backing up your key, creating a new one, and trying the new one in Home Assistant.

Also, is this the correct path for your adbkey file?

  - platform: firetv
    name: Family Room firetv
    host: 192.167.10.163
    adbkey: "/config/adbkey"

Shouldn't your configuration be:

  - platform: firetv
    name: Family Room firetv
    host: 192.167.10.163
    adbkey: "/Users/alastair/software/home-assistant/development/current_dev/adbkey"
TexanScot commented 6 years ago

I'm running HA in a docker container. The current_dev folder is mapped to the /config folder within the docker container. I can attach to the container and see the keys within the config folder.

root@hass:/usr/src/app# ls -l /config
total 45444
-rw-------  1 root root     1704 Sep 16 14:10 adbkey
-rw-r--r--  1 root root      722 Sep 16 14:10 adbkey.pub

Are the permissions on the adbkey file ok? Looks like it to me but I'm certainly not a Unix guru. Assuming they are this would seem to indicate that the data that HA reads from the file is not being accepted by the fire stick. Stupid question perhaps, but is there a way to confirm that the key stored on the firetv is corresponds to the one that HA is presenting?

I'll try regenerating the keys one more time though it doesn't seem to be helping.

JeffLIrion commented 6 years ago

Try restarting the Fire TV stick and see if that helps.

JeffLIrion commented 6 years ago

I looked over this issue again and I noticed that in the first post the IP address of the Fire TV that you successfully connected to was 192.168.10.163, but in all subsequent posts the IP address is 192.167.10.163. Is that the root of the problem?

TexanScot commented 6 years ago

@JeffLIrion

Hard to believe that it was something so simple and stupid! I corrected the IP address and the firetv now loads correctly. Thanks for spotting the typo and many, many thanks for taking the time to work with me so patiently.

Not only do I appreciate that you did so but I also appreciate all that you are doing for the HA community.

JeffLIrion commented 6 years ago

You're welcome, and thanks for the kind words! I'm glad we got this issue resolved.