HASS Addon TTLock offline integration

I can confirm a few things:

  1. BT proxy does not work (Everything Presence One/Lite, or M5Stack.)
  2. Once I passed through my mobo BT to the hassio VM, this integration could be refreshed to show/pair my lock.
  3. Credential management is unfortunately very flakey. Shows failures, even though the lock accepts the fingerprint – codes disappearing, etc.

Would be cool to bring this integration up-to-date. I probably have all the equipment to do this, just a lack of know-how. Realistically this needs to work with proxies to have any relevancy.

3 Likes

i got this lock from ali
it was only 37,- euro and has everything you could have want from a lock
the thing is that it won’t show up in this integration. Does anyone have this lock working ?

Peripheral connect start
Peripheral connect triggered
Peripheral connect error: Error: connection canceled!
    at Peripheral.cancelConnect (/app/node_modules/@abandonware/noble/lib/peripheral.js:61:26)
    at NobleDevice.connect (/app/node_modules/ttlock-sdk-js/dist/scanner/noble/NobleDevice.js:91:37)
    at async TTBluetoothDevice.connect (/app/node_modules/ttlock-sdk-js/dist/device/TTBluetoothDevice.js:47:17)
    at async TTLock.connect (/app/node_modules/ttlock-sdk-js/dist/device/TTLock.js:53:27)
    at async Manager._connectLock (/app/src/manager.js:583:21)
    at async Manager.initLock (/app/src/manager.js:163:13)
    at async WebSocket.<anonymous> (/app/api/index.js:52:30)
Connect failed
Lock connect failed
Connect to lock failed 79:7F:C3:EB:7F:38

Hi guys, how do I actually unpair the lock from the app?
I tried the delete option from the app but I’m just getting wrong password eventhough it’s the same admin password.
I really want to avoid reset since I’ve already setup pincodes/fingerprint for the family

Also, once the lock’s in pairing mode, would it just show up in ttlock addon?

Make sure the battery is full, the deleting was the easy part for me, and It did show up in the integration but it wouldn’t pair for me

The battery was at 40%.
I could try replacing with new battery
You’re using the same admin password right to delete the lock?

've been trying to build the bluetooth gateway for a few weeks but it simply isn’t working for me.
did anybody share a built binary? or an up to date version of the code?

Thanks

So i’ve been reading this thread and as i’m currently debating with myself on what lock to get, i’ve narrowed it down to yale and this other (BG4000 ot BG3000) ttlock based one:

It seems like both can be operated with local bluetooth solutions witch is much more desireble that the cloud, also ttlock seems to be hosted in china?
Coud be a problem in the future, who knows really.

Anyway, my question is, as i run a few bt beacons (mainly because i run/track out phones with Bermuda Bluetooth/BLE Triangulation / Trilateration for HomeAssistant) i would guess using my “normal” beacon config via esphome would work, i usually run ESP32-WROOM (might be misspelled) devices.

Would be cool if anyone could try this.
Found some info here for yale, so it seems possible for this as well i’de guess.

this is the config i use:


esphome:
  name: beacon2
  friendly_name: beacon2

esp32:
  board: node32s
  # framework:
  #   type: arduino

  framework:
      type: esp-idf
      version: recommended
      # Custom sdkconfig options
      sdkconfig_options:
        CONFIG_COMPILER_OPTIMIZATION_PERF: y

#CONFIG_COMPILER_OPTIMIZATION_PERF
#COMPILER_OPTIMIZATION_SIZE

# Enable logging
logger:
  #level: VERBOSE

#web_server:
#  port: 80

#captive_portal:

wifi:
  fast_connect: true
  networks:
  - ssid: !secret wifi_ssid
    password: !secret wifi_password

time:
  - platform: homeassistant    

esp32_ble_tracker:
  scan_parameters:
    interval: 1000ms
    window: 900ms

bluetooth_proxy:
  active: true

switch:
  - platform: restart
    name: device_WiFi-restart
    id: restart_switch
sensor:
  - platform: uptime
    name: "device Uptime Sensor"
api:
  encryption:
    key: "MASKED_OUT"

ota:
  platform: esphome
  password: "MASKED_OUT"

Adapt and try? :slight_smile:

Hello, i’m intrested in this project.

I’m works with hotel automation.

We have some locks that use ttlock tecnology.

I would like develop a system that’s work out of TTLOCK SERVER to encode cards and lock or unlock remotaly the lock.

For what I read this project just can lock or unlock remotaly. Is possible integrate a module to encode cards?

My understanding is that this integration allows to manage the cards/biometrics from home assistant, but currently i am struggling to install the bluetooth proxy. I tried the ESP bluetooth proxy and after months trying i am trying to install GitHub - kind3r/ttlock-sdk-js: JavaScript port of the TTLock Android SDK instead on a pi.
i am running homeassistant on a PC with no bluetooth, so the proxy is the only option for me.

If i can get it to run, i’ll post with the step by step instructions for it

1 Like

Has anyone picked up this project?

I have been trying all kinds of things for months now.
I tried the ESP-32 based proxy but it wouldn’t work for me.
I tried to install [kind3r][ttlock-sdk-js] on an orangepi 3 i had lying around as i had no raspberry pis but it didn’t work. At that point i had some typescript errors that i thought i had sorted, but i guess it didn’t.

Finally bought a RPI zero w 2 and upon installing the above repo… it seems a lot more promising.

when scanning for locks they now show, but pressing pair isn’t working.

I assume the issue is on:
ttlock-sdk-js/src/device/TTDevice.ts
on line 63:

    Object.getOwnPropertyNames(temp).forEach((key) => {
      if (!excludedKeys.has(key)) {
        const val = Reflect.get(this, key);
        if (typeof val != 'undefined' && ((typeof val == "string" && val != "") || typeof val != "string")) {
          if ((typeof val) == "object") {
            if (val.length && val.length > 0) {
              Reflect.set(json, key, val.toString('hex'));
            }
          } else {
            Reflect.set(json, key, val);
          }
        }
      }
    });

the problem is the lines:
if (val.length && val.length > 0) {
Reflect.set(json, key, val.toString(‘hex’));

the error is: error TS2339: Property ‘length’ does not exist on type ‘string extends keyof this ? this[keyof this & string] : any’.

I dont know enough of typescript to fix this. I tried to cast the val to any (val as any) or (val) but although they compile, the function to pair does not work

If anybody knows how to fix the code would be greatly appreciated

Ermargerd!!! i got it to work. No idea what made it work… so here’s all the steps:

On a fresh rpi zero w 2

sudo apt install git
sudo apt install npm
git clone https://github.com/kind3r/ttlock-sdk-js.git
cd ttlock-sdk-js
sudo nano ./src/device/TTDevice.ts

**Edited to add missing closing } as pointed out by MuzzaM **
replace the file’s content with the following:

'use strict';

import { EventEmitter } from "events";
import { LockType } from "../constant/Lock";

export class TTDevice extends EventEmitter {
  id: string = "";
  uuid: string = "";
  name: string = "";
  manufacturer: string = "unknown";
  model: string = "unknown";
  hardware: string = "unknown";
  firmware: string = "unknown";
  address: string = "";
  rssi: number = 0;
  protocolType: number = 0;
  protocolVersion: number = 0;
  scene: number = 0;
  groupId: number = 0;
  orgId: number = 0;
  lockType: LockType = LockType.UNKNOWN;
  isTouch: boolean = false;
  isUnlock: boolean = false;
  hasEvents: boolean = true;
  isSettingMode: boolean = false;
  txPowerLevel: number = 0;
  batteryCapacity: number = -1;
  date: number = 0;
  isWristband: boolean = false;
  isRoomLock: boolean = false;
  isSafeLock: boolean = false;
  isBicycleLock: boolean = false;
  isLockcar: boolean = false;
  isGlassLock: boolean = false;
  isPadLock: boolean = false;
  isCyLinder: boolean = false;
  isRemoteControlDevice: boolean = false;
  isDfuMode: boolean = false;
  isNoLockService: boolean = false;
  remoteUnlockSwitch: number = 0;
  disconnectStatus: number = 0;
  parkStatus: number = 0;

  toJSON(asObject: boolean = false): string | Object {
    const json: { [key: string]: any } = {};

    const excludedKeys = new Set([
      "_eventsCount"
    ]);

    Object.getOwnPropertyNames(this).forEach((key) => {
      if (!excludedKeys.has(key)) {
        const val = Reflect.get(this, key);

        if (val !== undefined && val !== '') {
          if (typeof val === "object" && val !== null) {
            if (Buffer.isBuffer(val)) {
              if (val.length > 0) {
                json[key] = val.toString('hex');
              }
            }
            else if(Array.isArray(val)) {
              if (val.length > 0) {
                json[key] = val.toString();
              }
            }
            else {
              json[key] = val;
            }
          } else {
            json[key] = val;
          }
        }
      }
    });

    return asObject ? json : JSON.stringify(json);
  }
}

sudo npm i
sudo npm run server-tool

when its running set the add on configuration to have the gateway to say noble
the ip to the pi IP
the gateway port to 2846
the dateway key to f8b55c272eb007f501560839be1f1e7e
and both user and pass to be admin

Finally on the webUI of the add on press the top right corner and activate the lock. it should show to be paired.
then try to pair. For me it didn’t go through first time. It took a good 20-30 attempts

lt only took the better part of a year… damn

Now… i need multiple proxies because the locks are too far to each other… good times

2 Likes

Nice work and thanks for the instructions, that’s a lot of attempts. Did you do any reset or is it just re-pair?

Just to clarify, is that esp32 bluetooth proxy or rpi w running the ttlock-sdk?

The locks will only pair to one device at a time. I had to remove them from the app before pairing to Home Assistant.

This was a RPI zero W 2 running the ttlock-sdk-js.
I never got the esp32 version to work unfortunately.

I now still need to do some extra work on this as i have 4 locks fairly far from each other beyond bluetooth range.

im looking at:

  1. Hacking the Pi to add an external antenna

  2. Alter the addon to allow multiple proxies

  3. use regular esp32 to extend the range of the pi.

I’m not sure what will pan out. Adding the antenna seems the simplest, and might work as my house isn’t that big and the locks are just barely out of range.
The other two options might be a bit more useful for the community.
We’ll see how it goes.

I am stubborn and keep at it for a while, but i do have other fights to fight. The cat flaps for example :slight_smile:

1 Like

Just to clarify, removing the lock from the app and doing a factory reset seems to amount to the same. In both cases all the stored access gets wiped and it all has to be done again.

A bit of a pain, but well worth it to get into Home Assistant with offline support

I was gonna ask about that if you need extra rpi zeros running nearby the locks. Would adding one of those long range bluetooth dongle to the pi work?

For now it’s limited to one proxy?

I do hope you could get that working

Really? I thought removing the lock from the app would still keep all the saved keycodes/fingerprint

That TTDevice.ts file content you listed is missing the last trailing ’ } ’
by my trial of your process steps.
Just so anybody else runs into the error it produces .

Thanks for the documenting the steps, have not yet completed pairing yet…

(Running a test on rpi4)

Completely correct, i must have missed it on the copy and paste.

I have edited my original post to correct the code

OK all done following the instructions above.
Thanks @TheTrueWanderer for documenting this.

A few notes from my testing this install.
I forgot to delete a TTLock HACS cloud integration, and this offline version had similar name which was a bit confusing before I realised.
Remember to turn off the original WIFI Bridge if you are coming from the cloud interface.
Once I set up the Pi4 running the Server I managed to PAIR the lock on the second attempt. I think the key is the order you activate the lock and hit the PAIR button.
From my reading and my own experience you need to activate the lock FIRST (mine ‘spoke’ a message to now enter bluetooth admin or some such words) … Then hit the PAIR button.

Very happy that I can run this OFFLINE.

With the old cloud connection I did use an option to SET Passage Mode between certain hours which was very handy, but this is not provided as far as I can see.
The sensors displayed via MQTT are LOCK/UNLOCK, Battery Level, RSSI.

I did change the Auto Lock from 5s to 59s, so this works.
I also tested the Sound On/OFF feature which worked.

I’ll probably be running this on a PiZero next once I tidy things up and do some more testing.