I am trying to control a WIPRO smart bulb which is registered in "Smart Life" app (basically Tuya app) . The following python code works:
In B4X, as a first step, I am trying to discover devices. I translated the above python code as follows:
python code to discover and turn on and off WIPRO bulb:
import requests
import pprint
import time
print("GET AUTH-TOKEN")
auth = requests.post(
"https://px1.tuyaus.com/homeassistant/auth.do",
data={
"userName": "appemailtest@test.com",
"password": "apppassword",
"countryCode": "1",
"bizType": "smart_life",
"from": "tuya",
},
).json()
pprint.pprint(auth)
access_token = auth["access_token"]
print(">> ACCESS_TOKEN=" + access_token)
print("GET DEVICES")
devices = requests.post(
"https://px1.tuyaus.com/homeassistant/skill",
json={"header": {"name": "Discovery", "namespace": "discovery", "payloadVersion": 1}, "payload": {"accessToken": access_token}}
).json()
pprint.pprint(devices)
bell_device = next(dev for dev in devices["payload"]["devices"] if "WIPRO" in dev["name"])
bell_id = bell_device["id"]
print(">> BELL_ID=" + bell_id)
print("TURNING ON")
turnon = requests.post(
"https://px1.tuyaus.com/homeassistant/skill",
json={"header": {"name": "turnOnOff", "namespace": "control", "payloadVersion": 1}, "payload": {"accessToken": access_token, "devId": bell_id, "value":"1"}}
).json()
pprint.pprint(turnon)
print("SLEEP FOR 10 SECONDS")
time.sleep(10)
print("TURNING OFF")
turnoff = requests.post(
"https://px1.tuyaus.com/homeassistant/skill",
json={"header": {"name": "turnOnOff", "namespace": "control", "payloadVersion": 1}, "payload": {"accessToken": access_token, "devId": bell_id, "value":"0"}}
).json()
pprint.pprint(turnoff)
In B4X, as a first step, I am trying to discover devices. I translated the above python code as follows:
B4X code to discover devices registered to smart life app:
Sub GetDevices(accessToken As String)
Log($"The Access Token is:${accessToken}"$)
Dim job As HttpJob
job.Initialize("client",Me)
Dim data As Map
data.Initialize
data.Put("header", CreateMap("name": "Discovery", "namespace": "discovery", "payloadVersion": 1))
data.Put("payload", CreateMap("accessToken": accessToken))
job.PostString("https://px1.tuyaus.com/homeassistant/skill",$"${data}"$)
Log($"${data}"$)
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
Log(job.GetString)
Dim devices As Map
devices = JsonToMap(job.GetString)
Log($"{devices}"$)
lbl_devices.Text =$"${devices}"$
Else
Log("Failed to send message. Error: " & job.ErrorMessage)
End If
job.Release
End Sub
However I don't get any device in log . The output of Log(job.GetString) is:
Please let me know where is the error.{"payload":{},"header":{"code":"DependentServiceUnavailable","payloadVersion":1}}