TTLOCK Integration with HA

Hi,

I am trying to figure a way to integrate the TTLock with the HA, but suddenly ended up with no luck.

I have gain the API ID & password, from the https://open.ttlock.com/

I tried to follow the instructions here : https://open.ttlock.com/doc/api

Anyone can help in this matter?

Thank you

2 Likes

i used the ttlock api to establish this. if you like i can help

1st step youā€™ll need a developer account and it must be reviewed.
once its reviewed youā€™ll need to send the client_id, client_secret, grant_type, a new username, a new password, and a redirect urlā€¦ this can be done with a simple html form

		Access token<br>
<form action="https://api.sciener.com/oauth2/token" method="post" enctype="application/x-www-form-urlencoded">
		<input type="text" name="client_id" value="your app id"><br>
		<input type="text" name="client_secret" value="your app secret"><br>
		<input type="text" name="grant_type" value="password"><br>
		<input type="text" name="username" value="your new username"><br>
		<input type="text" name="password" value="your new password"><br>
		<input type="text" id="lname" name="redirect_uri" value="https://home assistant address/auth/authorize"><br>
		<input type="submit" value="Submit">
		</form>

when you do this it will give you a access token and refresh token
KEEP THIS SAFE!!!

you will need the access token to make rest calls to the api
to get the last user who unlocked the door i used a sensor

sensor:
  - platform: rest
    name: "unlock name"
    scan_interval: 5
    resource_template: https://api.ttlock.com/v3/lockRecord/list?clientId= your client id/app id &accessToken= your access token &lockId= your lock id number &pageNo=1&pageSize=1&date={{ (now().timestamp()  | int  * 1000 ) }}
    value_template: '{{ value_json.list[0].username }}'

the api doc has all the information from here.

4 Likes

thanks, I have managed to integrate my TTLock with HA and everything is working fine, I am getting the desired values and doing the wanted action such as lock and unlock.
I am planning to do tutorial or write a guideline for references within these two days.

3 Likes

Yo, I already gave you the tutorial. Lol. If you donā€™t mind toss a quick shout-out. :rofl:

hi
i donā€™t fiend the ā€œyour lock id numberā€

hi i use this sensor to get the lockId

If youā€™re interested, there is now a local integration of the TTLocks: HASS Addon TTLock offline integration

3 Likes

I have also just started using this API

I can now get the access token and make other requests but I donā€™t understand how my developer account is associated with the locks?

Can I make requests for any lock if I have the lockid?
At the moment my request for locklist returns zero entries

Thanks for any help
Tom

Let me do my steps here to help somebody, but my lock is still not listedā€¦ I donā€™t know why. I already have any lock on my IOS App working, just want to integrate with HA:

  1. Created Account on open.ttlock.com
  2. Created App there and waited it do be approved
  3. got client_id and client_secret from the page
  4. registered a user (returns username to be used later) with the same as my IOS App with:

curl -i -X POST -d ā€œclientId=XXXXXXX&clientSecret=YYYYYYYYY&username=IOS_USERNAME&password=PASS_IN_MD5_FORMAT&date=date +%sā€ https://api.ttlock.com/v3/user/register

5. Got Token from API running:

curl -i -X POST -H ā€˜Content-Type: application/x-www-form-urlencodedā€™ -d ā€œclient_id=XXXXXXX&client_secret=YYYYYYY&username=ZZZZZZZ&password=PASS_MD5_FORMATā€ https://api.ttlock.com/oauth2/token

6. Tried to list my locks:

curl -i -X POST -H ā€˜Content-Type: application/x-www-form-urlencodedā€™ -d ā€œclientId=XXXXX&accessToken=TTTTTTT&pageNo=1&pageSize=20&date=date +%s000ā€ https://api.ttlock.com/v3/lock/list

{ā€œlistā€:[],ā€œpageNoā€:1,ā€œpageSizeā€:20,ā€œpagesā€:0,ā€œtotalā€:0}


Don't know what is happening... will try again later.

Updating, after a lot of tries, what I did to get it recognised:

  1. In my old account (the one I am logged into IOS App), selected transfer lock
  2. Choose to transfer to the new user I created on the developer account, the pone that starts with fccd_

Thats itā€¦ ater transferred, now I can get LockerID and configure it into HA. Now I need to test what I can do with it.

does this integration still work. I have tried to use it. However, im struggling in creating new account. Does the new account has to be in an email format?
Thanks

I was able to make an account with TTLock but I am completely lost as to how to integrate the TTLock add-on and actually use it. I did use an email when I created the account. I then set and confirmed the password. Then on the right side of the registration form it says ā€œGet Codeā€. When you complete that process and before you can hit NEXT there will be a code emailed to you. Check you spam folder in your email as well as keep refreshing your email because there is a countdown for how long the code is valid for and it seemed like I recieved the email with the code seconds before it times out. Enter the code even if it does time out and click NEXT and you should be able to proceed. However, as for integrating it with HA I am still lost.

Hi. Any update on the tutorial? Iā€™m having some trouble understanding this.

Iā€™m hoping to give this a crack soon. I have two different TTLocks on the iOS app, so Iā€™ll keep a log of what Iā€™m doing in case I am successful then post a tutorial.

After moving into our own place a few weeks ago, Iā€™ve been building up the smart devices on Google Home, until recently when I reached a good amount to start migrating it all to HA. So far I have been successful with some garden lights, a downlight globe, solar system and Deco Mesh Wifi. I failed on the Livolo Roller Blinds and a random Garage Door switch which I really want to get working. Next will be the TTLock Door locks and a dozen indoor downlights. Oh yeah and the Kommander board I just ordered to replace the crappy Bosch home security system so I can add in all the PIR sensors for light automationā€¦ All equally fun and frustrating!!! but something to do during lockdown!

Has anyone managed to get this api to work? Iā€™m going crazy, I did all the steps mentioned above but nothing, I managed to get token by registering a user through the api, but I canā€™t control my lock, nor do I get the lockid.

this method for ttlock2mqtt

i managed this to work with ha and here what i did
first you need to register at this site : https://open.ttlock.com/login
then click on create application and fill application name , logo , type (choose whatever you want , i chosed them all) and description .
after you get the approval you will be having 1- client_id 2- client_secret (save them to text file for use later ) .
** important note : the user for this application you can view it on the same site , press on top right at your email beside managment , and there is you can see your user name (not email) and write it in the text file with previous info .
now open terminal and paste this to link account with the application

** please note you have to md5 encrypted your password , open this site https://www.md5online.org/md5-decrypt.html  and put your password to decrypt 

$ curl --location --request POST 'https://api.ttlock.com/v3/user/register?clientId=put here your client id&clientSecret=put here your client secret&username=put here the user name from the site not the email&password=put here the password md5 decrypted for account at site&date=CURRENTMILLIS' \
--header 'Content-Type: application/x-www-form-urlencoded' \

next step i assume you already have the app at the mobile working since before , so do this the following command at terminal to get the token

curl --location --request POST 'https://api.ttlock.com/oauth2/token?client_id=put here your client id&client_secret=put here your client secret&username=put here the user name from your account&password=put here your decrypted password&grant_type=password&redirect_uri=https://yourdomain.com/' \
--header 'Content-Type: application/x-www-form-urlencoded' \

if all goes successfully you will get token and refresh token and other info , save them all

add this to your repo add ons

tonyldo HassIO Addo-ns
[email protected]

install the TTlock2MQTT
at configuration fill the client ID and ttlock token and fill the mqtt data , server , port , mqtt user , mqtt password and start the addon

at Mosquitto broker you will get two devices , the lock and the gateway , enjoy

and many sorry if my english wasnt clear enough

** few references

https://www.md5online.org/md5-decrypt.html

1 Like

Any luck with this ? I get this error - HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

Apache Tomcat/7.0.57

After painfully working through the ttlock api to get all the relevant info I have TTlock2MQTT installed and it seems to have found my locks (two) and the gwy in the logs but I am also getting an comm error. Not sure where to go from here and how to configure ha to see/use the locks. Any help would be appreciated.

Log file snippet, this just repeats over and over. (I xā€™d out part of the code numbers for privacy)
ā€˜ā€™ā€™
2022-01-02 20:21:45 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 TTlock Mqtt Created
2022-01-02 20:21:45 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 TTlock Mqtt on client_loop
2022-01-02 20:21:55 - [ERROR] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 Bad connection Returned code= 5
2022-01-02 20:21:55 - [INFO] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 disconnected!
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 TTlock Mqtt Created
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 TTlock Mqtt on client_loop
2022-01-02 20:22:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4353816-xxxxx72905 has 5 times bad connection
2022-01-02 20:22:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 Bad connection Returned code= 5
2022-01-02 20:22:45 - [INFO] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 disconnected!
2022-01-02 20:23:45 - [INFO] TTLOCK2MQTT: Current threads: 3
2022-01-02 20:23:45 - [ERROR] TTLOCK2MQTT: Client LOCK-4309730-xxxxx72965 has 5 times bad connection
2022-01-02 20:23:46 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 TTlock Mqtt Created
2022-01-02 20:23:46 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 TTlock Mqtt on client_loop
2022-01-02 20:23:56 - [ERROR] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 Bad connection Returned code= 5
2022-01-02 20:23:56 - [INFO] TTLOCK2MQTT: Client GATEWAY-240978-xxxxx73026 disconnected!
ā€˜ā€™ā€™

Tried some more trouble shooting, thought maybe it was not connecting to my mqtt server but if I change any of those parms I see mqtt errors that were not there before. I put them back and fudged the token and I see good initiation but no locks or gwys appear. When the correct data is running as above with the locks and gwy repeating I have sniffed the mqtt messages and donā€™t see any from ttlock. Any help would be greatly appreciated, I seem so close. J

thanks all of you, i managed to have the lock in HA but with very limited actions.
i will write down the steps I used:

  • created account on https://open.ttlock.com

  • created application and filled in the requested informations

  • after the application was approved, extracted the client_id and client_secret

  • opened a SSH connection to HA and entered the next commands:
    curl --location --request POST 'https://api.ttlock.com/v3/user/register?clientId=<YOUR_CLIENT_ID>&clientSecret=<YOUR_CLIENT_SECRET>&username=<USERNAME>&password=<MD5_ENCRYPTED_PASSWORD>&date=CURRENTMILLIS'

          - where USERNAME I think it might be anything but in my case when I logged out from dev account and clicked on login window I was presented with a 6 digit username. It didn't  worked to login with that but that is what I used next
          - password needs to be encrypted md5; you can use https://www.md5online.org/md5-encrypt.html
         - after running this command, in the output I received my username but with a prefix, something like bhdca_<username>
    
  • now you can run the next command:
    curl --location --request POST 'https://api.ttlock.com/oauth2/token?client_id=<YOUR_CLIENT_ID>&client_secret=<YOUR_CLIENT_SECRET>&username=<YOUR_USERNAME_FROM_PREVIOUS_COMMAND_OUTPUT>&password=<MD5_ENCRYPTED_PASSWORD>&grant_type=password&redirect_uri=https://yourdomain.com/' --header 'Content-Type: application/x-www-form-urlencoded'

      - in the output you should have the token and other info
    
  • install the add-on and configure as @tammeryousef mentioned in the above post; at this moment you should not see any new device in MQTT broker (Iā€™m using Mosqoitto Broker add-on)

  • open the TTLock app on your phone and transfer the locks and gateways to your new user created with 1st command (e.g. bhdca_111111)

  • logout and login using the new user and the same password used for developer account.

  • in 2 - 3 minutes you should be able to see the gateway and the loks in MQTT in Configuration ā†’ Integrations

At the moment I can lock /unlock using HA but that is it, no real time status :frowning:

p.s actually there is a 60 sec delay set in config for status update

4 Likes