kind3r / hass-addons

Integration of the offline TTLock sdk into Home Assistant
59 stars 21 forks source link

Using the TTLock addon without hassio #6

Open jbg opened 3 years ago

jbg commented 3 years ago

Hey! Thanks for making this addon. I have a couple of these locks and would love to help with testing and improvements, but I don't use hassio and it's not an option for me to switch to it. I made a container image based on the Dockerfile in the repository, with the same set of dependencies (bluez etc), and passed through the environment variables that start.sh sets from bashio config vars, as follows:

- name: LANG
  value: C.UTF-8
- name: MQTT_HOST
  value: mosquitto.default.svc.cluster.local
- name: MQTT_PORT
  value: "1883"
- name: GATEWAY
  value: none
- name: TTLOCK_DEBUG_COMM
  value: "1"
- name: MQTT_DEBUG
  value: "1"
- name: WEBSOCKET_DEBUG
  value: "1"

(The DEBUG ones are part of an attempt to get more logging, but they don't seem to have any effect.)

I can pull up the frontend on :55099/frontend/ and it appears functional but nothing happens when I click the scan button. I can see that the websocket connection is active, and the scan message is sent when I click the button.

hciconfig inside the container gives this output:

/ # hciconfig
hci0:   Type: Primary  Bus: USB
    BD Address: XX:XX:XX:XX:XX:XX  ACL MTU: 1021:4  SCO MTU: 96:6
    UP RUNNING 
    RX bytes:18145 acl:0 sco:0 events:2861 errors:0
    TX bytes:686715 acl:0 sco:0 commands:2859 errors:0

I have tried running node both as root and as an ordinary user after using setcap to give the node binary the needed capabilities as per the noble docs.

In the logs of the container there is just:

$ kubectl --context=home logs deployment/ttlock-hass
[Error: ENOENT: no such file or directory, access '/data/lockData.json'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'access',
  path: '/data/lockData.json'
}
Server started

From what I can see, the error is nothing to worry about because the data file just hasn't been created yet. Nothing is written to the log when I click the scan button. Is there any way to increase log verbosity?

kind3r commented 3 years ago

Hi !

If you want to run the addon standalone it should be possible, even without an MQTT server, but if you want one then all the envs need to be configured (MQTT_HOST, MQTT_PORT, MQTT_SSL, MQTT_USER, MQTT_PASS).

Also, docker is not 100% required, and it should run fine directly on any system that noble supports (alto I did not have much success on MacOS, I think permissions for bluetooth changed and it's not really discovering devices as it should, so a linux distro would be the best bet). But if you run in docker, I think the container needs to run with host networking to get access to the bluetooth adapter.

The missing lockData.json file indeed is nothing to worry about as it will be generated after the first lock is paired. But the /data folder needs to exist.

There are no other verbosity options, but you could also check the console logs on the frontend (in the browser). After loading, the UI should connect to the websocket and receive a status message. Is this message received ?

Another thing you could try is running the examples from the ttlock-sdk-js as the addon is at it's core just a UI for the sdk.

jbg commented 3 years ago

The container is running with host networking (and hciconfig shows the Bluetooth adapter from within the container).

There is nothing in the console logs in the browser, but in the Network tab of devtools I can see that the status message is received on the websocket. (And the scan message is sent on the same websocket when I click the scan button.)

With the examples (I tried listen.js and status.js which both look like they should print the JSON of any found lock even if it's not paired) I just get "Scan started" and then nothing. I tried resetting both locks and waking them up via the keypad both before and during the scan.

It looks like "Scan started" is printed unconditionally even if the scan was not started due to the various conditions in startScan (e.g. not powered on, already scanning, state unknown). I'll do some patching to add error reporting and see if I can find something out.

~/ttlock-sdk-js/examples # node listen.js 
Scan started
jbg commented 3 years ago

The startScanLock() promise never resolves, yet the onScanStart event isn't fired and no devices are found. I get the same behaviour on the host system (outside the container), too. The standard bluetooth tools find many devices including the locks, though. I think it may be the same issue as abandonware/noble#136 - noble uses the old raw sockets approach to accessing the BT hardware, which is not well supported in versions of the userspace bluetooth libraries released in the last few years. I'll try building a container with an older distro e.g. ubuntu 18.04, as suggested in that issue.

kind3r commented 3 years ago

I've tried running the addon locally (with a gateway instead of hardware bluetooth) and I'm getting the same behaviour. I'm digging more into it.

Update: my bad, had the wrong configuration for the gateway, now it works.

So yeah, you might be right and it's probably related to the noble bug. Since HA uses it's own distro probably that's why I did not notice it. Same goes for the debian flavored PI distro on which I did most of the testing for the sdk. I chose noble for the BLE stack because it claimed to be available for all platforms, turns out it's not really the case anymore and maybe I should look into other platform specific implementations that actually work.

kind3r commented 3 years ago

Hi @jbg , did you make any progress with the older Ubuntu ?

hamishfagg commented 1 year ago

@jbg any chance you can supply the dockerfile for your container? I am trying to do the same thing

louisdcmr commented 8 months ago

Hi everyone, do you know how to install it on a Raspberry pi without a home assistant or anything?

alexpalmer commented 8 months ago

Anyone able to post a docker-compose to build this without HASSIO?