TTLOCK Integration with HA

Can we get this on a github page, and create an action to publish to docker hub automatically? I can certainly help with this.

Might be better for most folks, rather than just pulling and running something without provenance from Docker hub.

3 Likes

Hey Guysā€¦Iā€™m a newbieā€¦just starting to understand .yaml . I have a TTLock and Iā€™m totally out of my league with your knowledge. Any chance of a HACS integrati

The API for TT lock is changing and so I have updated the image.

@vk-hass - here is a link to the GitHub repository I havenā€™t bothered with action however there is a Docker file there if you want to create your own: GitHub - stevendodd/ttlock-docker

Dear Developer,

In response to the legal requirements for better data security and privacy protection, we have assigned different domain names to users from China and other countries. Please be reminded to update the domain name if you are using any of our API. For software used in China, please call the China domain name. Otherwise, please call the domain name for users out of China. Following are the detailsļ¼š

Original domain name: api.ttlock.com
New domain name (China): cnapi.ttlock.com
New domain name (Out of China): euapi.ttlock.com

Original domain name: api.sciener.com
New domain name (China): cnapi.sciener.com
New domain name (Out of China): euapi.sciener.com

The original domain name will be deleted and no longer in service after Oct. 1st, 2022. Your speedy action is highly appreciated.

If you need any assistance, please feel free to contact us at [email protected]

Best Regards,
Sciener/TTLock Team

1 Like

very very interested in a walk thru. I have a Smonet door lock controlled via TTLOCK software and Alexa. Iā€™d like to reveal it on my Lovelace dashboard but I cannot find an existing integration. Any help GREATLY appreciated. :slight_smile:

hi steven. unfortunately Iā€™m not well versed in these things. can I ask you the favor of giving me a hand in the configuration, paying you the trouble? I ask you the courtesy to write to me via whatsapp: 3355273527 so that we can agree.

This is a modified version to pass the lock number in the URL as well as get the lock status

from flask import Flask
from flask import request
import json
import requests
import time
import os
 
tokenExpiryTime = 0

def current_milli_time():
    return round(time.time() * 1000)

def get_token():
    global tokenExpiryTime
    if current_milli_time() > tokenExpiryTime:
        global clientId
        global accessToken
        global lockId

        clientId = os.environ['CLIENTID']
        lockId = os.environ['LOCKID']
        clientSecret = os.environ['CLIENTSECRET']
        user = os.environ['USER']
        password = os.environ['PASSWORD']
      
        data = {"client_id": clientId,
                "client_secret": clientSecret,
                "username": user,
                "password": password
                }
        response = requests.post("https://euapi.ttlock.com/oauth2/token", data)
            
        if response.status_code == 200:
            accessToken = response.json()["access_token"]
            tokenExpiryTime = int(response.json()["expires_in"])*1000 + current_milli_time() - 25000
        
def handle_users(lock):
    get_token()
    response = requests.get("https://euapi.ttlock.com/v3/lockRecord/list?clientId=" + clientId + "&accessToken=" + accessToken + "&lockId=" + lock + "&pageNo=1&pageSize=1&date=" + str(current_milli_time()))
    if response.status_code == 200:
        return(response.json())
    
def handle_unlock(lock):  
    get_token()
    data = {"clientId": clientId,
            "accessToken": accessToken,
            "lockId": lock,
            "date": current_milli_time()
        }
    
    response = requests.post("https://euapi.ttlock.com/v3/lock/unlock", data)
            
    if response.status_code == 200:
        return(response.json())


def request_lock(lock):
    get_token()    
    response = requests.get("https://euapi.ttlock.com/v3/lock/detail?clientId=" + clientId + "&accessToken=" + accessToken + "&lockId=" + lock + "&date=" + str(current_milli_time()))
         
    if response.status_code == 200:
        return(response.json())

def request_lockStatus(lock):
    get_token()    
    response = requests.get("https://euapi.ttlock.com/v3/lock//queryOpenState?clientId=" + clientId + "&accessToken=" + accessToken + "&lockId=" + lock + "&date=" + str(current_milli_time()))
         
    if response.status_code == 200:
        return(response.json())

app = Flask(__name__)

@app.route("/",methods = ['GET'])
def hello():
    return('stevendodd/TTLock')

@app.route("/<lock>/unlock",methods = ['POST', 'GET'])
def unlock(lock):
    return handle_unlock(lock)

@app.route("/<lock>/users",methods = ['GET'])
def users(lock):
    return handle_users(lock)

@app.route("/<lock>",methods = ['GET'])
def get_lock(lock):
    return request_lock(lock)

@app.route("/<lock>/getstatus",methods = ['GET'])
def get_lockStatus(lock):
    return request_lockStatus(lock)

1 Like

Hi!
What is your question? What would you like to know, I donā€™t think Iā€™m interested in getting paid to help support individuals setups. Perhaps someone else has the same question you do

Sample configuration.yaml file

rest:
  - scan_interval: 60
    resource: http://192.168.176.3:5000/1234567
    sensor:
      - name: "Back Door TTLock"
        value_template: "OK"
        unique_id: backdoor_ttlock
        json_attributes:
          - "lockFlagPos"
          - "autoLockTime"
          - "electricQuantity"
          - "firmwareRevision"
          - "hardwareRevision"
          - "lockAlias"
          - "modelNum"
          - "passageMode"
          - "passageModeAutoUnlock"
          - "soundVolume"
          - "tamperAlert"
  - scan_interval: 60
    resource: http://192.168.176.3:5000/7654321
    sensor:
      - name: "Inner Door TTLock"
        value_template: "OK"
        unique_id: inner_door_ttlock
        json_attributes:
          - "lockFlagPos"
          - "autoLockTime"
          - "electricQuantity"
          - "firmwareRevision"
          - "hardwareRevision"
          - "lockAlias"
          - "modelNum"
          - "passageMode"
          - "passageModeAutoUnlock"
          - "soundVolume"
          - "tamperAlert"
  - scan_interval: 60
    resource: http://192.168.176.3:5000/1234567/getstatus
    sensor:
      - name: "Back Door Status"
        value_template: "{{ value_json.state }}"
        unique_id: backdoor_ttlock_status
        json_attributes:
          - "sensorState"
          - "state"
  - scan_interval: 60
    resource: http://192.168.176.3:5000/7654321/getstatus
    sensor:
      - name: "Inner Door Status"
        value_template: "{{ value_json.state }}"
        unique_id: innerdoor_ttlock_status
        json_attributes:
          - "sensorState"
          - "state"
rest_command:
  unlock_backdoor:
    url: "http://192.168.176.3:5000/1234567/unlock"
    method: get
  unlock_innerdoor:
    url: "http://192.168.176.3:5000/7654321/unlock"
    method: get
  lock_backdoor:
    url: "http://192.168.176.3:5000/1234567/lock"
    method: get
  lock_innerdoor:
    url: "http://192.168.176.3:5000/7654321/lock"
    method: get
lock: 
    - platform: template
      name: Storeroom Inner Door
      value_template: "{{ state_attr('sensor.inner_door_status', 'state') | int != 1 }}" #"{{ is_state('sensor.inner_door_status', '0') }}" 
      optimistic: true
      lock: 
      - service: rest_command.lock_innerdoor 
      unlock: 
      - service: rest_command.unlock_innerdoor
    - platform: template
      name: Storeroom Outer Door
      value_template: "{{ state_attr('sensor.back_door_status', 'state') | int != 1 }}" 
      optimistic: true
      lock: 
      - service: rest_command.lock_backdoor 
      unlock: 
      - service: rest_command.unlock_backdoor 
sensor:
  - platform: template
    sensors:
      ttlock_innerdoor_battery:
        friendly_name: "TTLock Inner Door Battery"
        value_template:  '{{ states.sensor.inner_door_ttlock.attributes.electricQuantity|float }}'
        unit_of_measurement: '%'
        device_class: battery
      ttlock_backdoor_battery:
        friendly_name: "TTLock Back Door Battery"
        value_template:  '{{ states.sensor.back_door_ttlock.attributes.electricQuantity|float }}'
        unit_of_measurement: '%'
        device_class: battery
1 Like

Iā€™m sorry, I didnā€™t mean to offend you. I simply have no experience in ā€œprogrammingā€ and would simply need and maybe it would be useful for everyone a step by step guide to achieve integration. Thanks

No offense taken; the instructions are here: TTLOCK Integration with HA - #26 by stevendodd and also on the docker hub page.

You donā€™t need any programming experience, although you do need to edit the configuration.yaml file. You also need to be able to host a docker container.

Why donā€™t you try following the instructions and ask any questions that youā€™re not sure of here?.

What do you run your home assistant server on or are you using a cloud version?

home assistant is running in a virtual machine and works great.
i created the application on open.ttlock and it is under review
i donā€™t know where to go to find this configuration.yaml file.
I think the .yaml file needs to be modified and then created a container in the dashboard, right?

configuration.yaml is the main configuration file that you edit with home assistant. You will find it in your config directory on your home assistant installation.

The first step for you will be to set up a Docker server and create the container from the image linked above.

If you can access it via a browser then you can start looking at updating configuration.yaml

Thanks; I have updated the repository with your new code Support for multiple locks; Ā· stevendodd/ttlock-docker@647f6dd Ā· GitHub

perfect. this docker server how do i do it?

How do you host your virtual machine

my virtual machine is on hyperv. do i need to create a new virtual machine?

Itā€™s up to you; you just need to install Docker somewhere. Inside the same virtual machine as home assistant sounds like a good idea depending on your set up.

is there a guide on how to install Docker alongside HA?

There is not; log onto your virtual machine download and install Docker its separate software

i installed docker desktop 4.12.0 on a separate machine into windows 10