PaulAnnekov / tuyaha

Implements the special Tuya Home Assistant API.
Other
141 stars 106 forks source link

Adding support for "LSC Smart Connect" #9

Closed sandervankasteel closed 4 years ago

sandervankasteel commented 4 years ago

Hi,

I have a few questions about adding support for LSC Smart Connect. LSC Smart Connect is a Dutch brand which uses the Tuya eco-system. However, I tried to get it to work with this Python library, but couldn't. Then I tried to tried to figure out why it didn't work out, by using a MITM proxy with an Android emulator and I found the following things;

  1. The hostname which is being used is https://a1.tuyaeu.com instead of https://px1.tuyaeu.com.

  2. The login procedure is completely different. First it does an API call to https://a1.tuyaeu.com/api.json with some extra URL parameters such as an appVersion appRnVersion, lang and some other environment variables such as Android version, phone number. The body that is being POSTed only contains the following JSON blob;

    {"countryCode":"31","mobile":"some_phonenumber"}

    Then the server will followup with the following JSON response

    {
    "result": {
        "exponent": "3",
        "pbKey": "some_key",
        "publicKey": "some_public_key",
        "token": "some_token"
    },
    "status": "ok",
    "success": true,
    "t": 1571577602930
    }
  3. After this call, the final authentication call is being done with the following JSON blog being POSted;

    
    {"countryCode":"31","ifencrypt":1,"mobile":"some_phonenumber","options":"{\"group\": 1}","passwd":"an_encrypted_password","token":"some_token"}

Which will be replied with the following JSON blob;

{ "result": { "accountType": 1, "dataVersion": 1, "domain": { "aispeechHttpsUrl": "https://aispeech.tuyaeu.com", "deviceHttpUrl": "http://a.tuyaeu.com", "deviceHttpsPskUrl": "https://a3.tuyaeu.com", "deviceHttpsUrl": "https://a2.tuyaeu.com", "deviceMediaMqttUrl": "s.tuyaeu.com", "deviceMediaMqttsUrl": "ms.tuyaeu.com", "deviceMqttsPskUrl": "m2.tuyaeu.com", "deviceMqttsUrl": "m2.tuyaeu.com", "gwApiUrl": "http://a.gw.tuyaeu.com/gw.json", "gwMqttUrl": "mq.gw.tuyaeu.com", "httpPort": 80, "httpsPort": 443, "httpsPskPort": 443, "mobileApiUrl": "https://a1.tuyaeu.com", "mobileMediaMqttUrl": "s.tuyaeu.com", "mobileMqttUrl": "mq.mb.tuyaeu.com", "mobileMqttsUrl": "m1.tuyaeu.com", "mqttPort": 1883, "mqttsPort": 8883, "mqttsPskPort": 8886, "pxApiUrl": "http://px.tuyaeu.com", "regionCode": "EU" }, "ecode": "some_code", "email": "my_email@domain.tld", "headPic": "", "improveCompanyInfo": false, "mobile": "31-phonenumber", "nickname": "my_nickname", "partnerIdentity": "p1294949", "phoneCode": "31", "receiver": "31-phonenumber", "regFrom": 0, "sex": 0, "sid": "some_id", "snsNickname": "", "tempUnit": 1, "timezone": "", "timezoneId": "Europe/Amsterdam", "uid": "my_user_id", "userType": 1, "username": "my_email@domain.tld" }, "status": "ok", "success": true, "t": 1571577603205 }



4. Once these API calls have been resolved, I can now finally get the device list. 

So basically my question is, would you accept a PR which adds this support, and if so, do you have any suggestions on how to tackle this without breaking current support ?

PS. For obvious reasons, any identifying information has been censored ;-)
JasperMC commented 4 years ago

Would love to see support as well. Currently, I’ve had luck with their smart bulbs but the LED strips are not working unfortunately.

Most devices work when you register them in the SmartLife or Tuya app. The LSC app is basically a rebranded version.

sandervankasteel commented 4 years ago

A hours after my issue post, I found an "old" (last updated Jan 2018) reverse engineering attempt of the Tuya API (https://github.com/Ericmas001/Tuya-Api-Tools/wiki/Requests), after some poking, prodding, coffee, headbanging, facepalming, more reverse engineering and some initial POC work I decided to drop my effort of adding support of this API to anything.

My reasoning behind this is mostly two parts. The Tuya API is way too over engineered, unnecessarily complex and secondly, there are so many changes between the initial reverse enigeering attempt from Ericmas001 and the current version of the API that it would take more time then I would like, to get any code towards a level where it's "usable" and that doesn't factor in any changes Tuya does on their API which can completely undermine any effort from my part.

The thing I am mostly afraid of, is their (completely unnecessary) obfuscation of the values which their API requires and changes in said obfuscation.

For example, just checkout what you need to access their MQTT server(s), it's just a mess unfortunately :(

What also doesn't help is that I flashed Tasmota on my LSC branded device using Tuya-convert yesterday.

PaulAnnekov commented 4 years ago

Yep, you found their OEM API. It's very different compared to API they created specially for HA and unsuitable for our case. Just check how to get credentials to authorize https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md :man_facepalming: .