How to complete a HTTP POST from a Device Handler

Hi,

I am completely new to Smartthings, picked one up yesterday and immediately wanted to get my hands dirty. I have a few Lenovo smart plugs which are incompatible with SmartThings so that seemed like a good place to start.

I sniffed the packets sent between my phone and the Lenovo app and pretty soon I had CURL requests which could turn the plug on and off.

I could not post the CURL requests as "new users can only create posts with two links in them. But happy to share

After a bit of searching I found some examples of device handlers and thought I’d give it a go. I created a handler and am attempting to run the HTTP POST, the code runs in the simulator, but I never get a response back from the API. Which, I’m guessing means I have not successfully sent the request.

Perhaps I am doing something wrong. I can code in C# and make sense of most C based languages but I’ve never really used Java or Groovy.

Here is the code I am having trouble with (I’ve tried this many different ways and a fair bit of googling but so far cannot figure this one out).

def cmdStr = new physicalgraph.device.HubAction([

    method: "POST",

    path: "https://a1.tuyaeu.com/api.json?appVersion=1.0.8&appRnVersion=5.16&channel=oem&sign=aeba6e2099943efabbfeb755ceb4c1bcb98ca97f029dd2b0cb1d481682261867&lon=151.34221&deviceId=45a6e73f040d9e5b12f98a7a3c62c9dc4a7ad242a036&platform=Pixel%25204%2520XL&sid=eu158183N26952181nPeWpi331d46bfb56a27534421f684dea448741&requestId=9559c792-dc1d-4912-bb0b-1ba83a8cecc1&lang=en&lat=-33.4315803&a=tuya.m.device.dp.publish&clientId=7cpsrpqaundffdfedrhedrosSystem=10&os=Android&timeZoneId=Botswana%252FArctic&ttid=sdk_tuya%25407cpsrpqaundffdfedrhedret=0.0.1&v=1.0&sdkVersion=3.12.0&time=1582370580",

    body: "postData=%7B%22devId%22%3A%222800434484f3ebc1b3af%22%2C%22dps%22%3A%22%7B%5C%221%5C%22%3Afalse%7D%22%2C%22gwId%22%3A%222800434484f3ebc1b3af%22%7D",

    headers: ["HOST": "a1.tuyaeu.com",

    "User-Agent":"TY-UA=APP/Android/1.0.8/SDK/null",

    "Content-Type":"application/x-www-form-urlencoded",

    "Content-Length":"145",

    "Connection":"Keep-Alive",

    "Accept-Encoding":"gzip"

    ]]

)



//Send the command to the Hub

def CmdResponse = sendHubCommand(cmdStr)



log.debug cmdStr **// I see this**

log.debug CmdResponse **//There is never a response from the server here.**

I’ve tried (among other things) removing the hostname from the path but that seems to get me in the same situation

The hubAction you are using is for requests on your LAN (and is also still limited to just http I believe). You need to be looking at using httpPost() for external requests.

I don’t know anything about the Lenovo plugs, but I note that a lot of brands piggy back on the Tuya Smart system, and that is being integrated with SmartThings, with the first brand integration being Globe Electric. So that may be worth looking into. My superficial understanding is that there is interoperability between brands on Tuya.

@THeBingoKing, Just to add @orangebucket’s comment above. The Globe Suite integration is not ready yet. People are waiting for it for months. When it would be ready you could add Tuya devices to ST.

Otherwise, I wouldn’t post that much detailed code here. If you haven’t changed some part of it, then you just gave out a link to control your plug…
But I guess that method would use some sort of authentication like OAuth, so you need that for your DH too.

Some links regarding HTTP POST.

Hi @orangebucket / @GSzabados

Thanks for the responses, the HTTPPOST is exactly what I needed and I have it running now.

P.S. I did change my code, all of the DeviceID’s etc have been changed so no one can control the plug.

Thanks Again

1 Like