codetheweb / tuyapi

🌧 An easy-to-use API for devices that use Tuya's cloud services. Documentation: https://codetheweb.github.io/tuyapi.
MIT License
2.07k stars 341 forks source link

Possible to link tuya zigbee-hub? #366

Closed cicci44 closed 5 days ago

cicci44 commented 3 years ago

putting tuya-zigbee-hub(TYGWZ-01) to linking mode. running this in terminal tuya-cli link --api-key APIKEY --api-secret SECRET --schema com.NAME --region eu --ssid WIFI --password WIFIPASSWORD

got some error (but just for a few moments ago i did clear the terminal window when i gave up and linked it to tuya official android app)

I thought i was going to get it to link with the DEV-APP made in iot.tuya.com (as it work with tuya-wifi devices) but i can only get it to work with tuyas official android app.

Is this even possible with tuya-cli today? to bind a hub(gateway) to my own APP?

tsightler commented 3 years ago

TuyAPI does not work with Tuya zigbee hubs today. The whole linking thing seems pretty unreliable overall as I'm consistently successful with some devices and not at all with many, mostly newer devices. It's generally more reliable to use tuya-cli wizard method to get the keys.

That being said, it wouldn't completely surprise me if you could get it to link, but, as far as I can tell, you can't actually use TuyAPI to access any of the zigbee devices so there's not much value even if you did. Based on the data I do have, I have a theory on how a Tuya zigbee hub could be supported, but realistically, it will be very difficult to develop without having a hub to test.

cicci44 commented 3 years ago

TuyAPI does not work with Tuya zigbee hubs today.

ok :(

Based on the data I do have, I have a theory on how a Tuya zigbee hub could be supported, but realistically, it will be very difficult to develop without having a hub to test.

Im waiting for cc2531 to proceed with my own testrig to get stuff work, maybe my tuya zigbee hub might be to some use for you?

tsightler commented 3 years ago

That being said, I have a little bit of data from the Tuya zigbee hub. It appears that each device on the gateway has it's own device id, but uses the same key. This makes sense as all communications is via the hub. I'm thinking this is why Tuya commands like DP_QUERY/CONTROL have a gwId and devId, so that you can set an id for the device, but then define the gwId and send commands to the gateway with the key. Right now there's no logic in TuyAPI to do this, today it just always sets gwId and devId the same.

I found some Tuya zigbee hubs online for <$30, but I guess I'm not 100% sure it's worth the effort. What is the logic of connecting open Zigbee devices to some proprietary controller and then trying to control them by some reverse engineered local hacks? Why not just buy a Zigbee stick and have full control of these devices?

msillano commented 3 years ago

My experience with gateways is different.

I'm using a cheap Zigbee gateway (Smart Gateway, model TYGWZW-01N) and 4 thermometers/humidity meters and 3 thermostats for radiators, all battery-powered. I GET the values PUSHed by devices/tuya cloud, included all 28 different dps that each thermostat sends, I GET the STATUS changes after the smartlife commands and I can SET many of them (SCHEMA doesn't work) from node-red. The Zigbee gateway itself is not accessible, but it is transparent for the connected devices.

example: the data packet to set the target temperature to 20.5 on a thermostat is (using node-red-contrib-tuya-smart-device): "payload": { devId: "<cid-virtual-device>" dps: "103" set: 205 }

Some data requires extra encoding/decoding, example: dps 115: "today real temperature": base64(int8[24]). data: Dw4NDQwMDxESERAQERITExMAAAAAAAAA => [15,14,13,13,12,12,15,17,18,17,16,16,17,18,19,19,19,0,0,0,0,0,0,0]

And it is rock solid, never disconnect. It works with smartlife app, with Google-nest (with some limitations) and it is perfect for node-red control.

The WiFi battery-powered devices can't be used directly with tuyapi, because they are connected to wifi only to send data. Viceversa the Zigbee gateway (AC powered via USB) is always WiFi connected., so all ZigBee devices are accessible.

Best regards. m.s.

danielk117 commented 3 years ago

i own a cheap gateway and thermostats for radiators too. getting values is working. the result looks like this:

{"payload":{"data":{"dps":{"3":230},"cid":"<unique-zigbee-device-id>","t":1609886870},"deviceId":"<gateway-virtual-id>","deviceName":"zigbee"},"_msgid":"cf2aa672.a86208"}

<gateway-virtual-id> is the id shown in the tuya app. <unique-zigbee-device-id> is not the id for the zigbee device shown in the app.

@msillano i'm not able to set a value. tried the following send to the gateway:

{"operation":"SET","cid":"<unique-zigbee-device-id>","dps":2,"set":"65"}

and just getting "Timeout waiting for status response from device id: <gateway-virtual-id>" also tried devId and gwId as key for the cid.

can you explain to me how you did that?

rospogrigio commented 3 years ago

@msillano I'm joining @danielk117 's question, how were you able to communicate with the device through the gateway? Can you provide some support? I am developing an integration for local handling of Tuya devices in Home Assistant but I am having problems with Zigbee gateways. Thank you!

danielk117 commented 3 years ago

@rospogrigio i moved to zigbee2mqtt, because i think its not possible to set values to zigbee devices using tuyapi. you need to tell the bridge a id for the zigbee device. and as far as i unterstand the code, this isnt implemented in tuyapi for now.

rospogrigio commented 3 years ago

@danielk117 Thanks, are you using a USB Zigbee dongle or a wifi Zigbee gateway?

msillano commented 3 years ago

danielk117 wrote: "not possible to set values to zigbee devices using tuyapi."

My experience is very different: I work very well with WiFi Zigbee gateway, better than using WiFi sensor devices !! My climatization devices (thermometers and thermostats) are Zigbee, my security devices (door, PIR) are WiFi.

rospogrigio, about thermostats, my experience with WiFi Zigbee gateway is the following:

warning: in SET, with sub-devices, you must use "devId", not "cid" (undocumented)

I'm thinking about a simple way to share public information about device's capabilities and quirks.

For the general problem, i.e. to integrate tuya-cloud in node-red (it is mine), I'm testing a possible complete solution: see https://github.com/msillano/tuyaDAEMON.

Best regards m.s.

rospogrigio commented 3 years ago

@msillano I am afraid I'm not getting something, because I am having serious problems in connecting to the devices. Could you please post exactly what commands you issue and how? Because I am getting the "json data unvalid" error with lots of devices (and by the gateway too), which we actually have fixed in my repo. Can you please be very specific on describing how you operate? Thank you! 😄

msillano commented 3 years ago

1) "json data unvalid" minds "the required operation is not available" or "error on parameters", it is a good sign (becomes from device, not from tuyapi).

2) "schema" is very rare in devices, don't use it. "GET" also is not implemented in all dps. 3) Start doing commands with 'smartlife' and capture answers: you find so writable dps. 4) the Zigbee Gateway is deaf and dumb: no dps, no message. 5) for subdevices try some simple as: { "devId": "sub-device-cid", "dps": "103", "set": 215}, sending it to gateway. note: you can found the cid in captured answer at point (3). 6) If dp is BYTE or INT or ENUM use set: value, if it is STRING use set:"value".

See also issues#389. It can be long and boring.... I know :)

Best regard m.s.

danielk117 commented 3 years ago

@rospogrigio

Thanks, are you using a USB Zigbee dongle or a wifi Zigbee gateway?

a simple c2531 USB dongle for my pi. works great!

@msillano

warning: in SET, with sub-devices, you must use "devId", not "cid" (undocumented)

thanks for the feedback. i tried it before. it doesnt work for me.

rospogrigio commented 3 years ago

@danielk117

a simple c2531 USB dongle for my pi. works great! Thanks, I just purchased one too and will receive it today, will try with that. Are you using it with zigbee2mqtt?

@msillano Still when you say to try { "devId": "sub-device-cid", "dps": "103", "set": 215}, I don't understand how I am supposed to send it to the gateway... can you write me an email at rospogrigio@gmail.com so we can have a longer conversation? We are both italian so we can be more comfortable. Also, I have quite a good knowledge of the "json data unvalid" issue, and will post an interesting comment in issues#389 that will allow to improve tuyapi. Thank you for your support in the meantime

danielk117 commented 3 years ago

@rospogrigio yes, zigbee2mqtt.