TheAgentK / tuya-mqtt

Nodejs-Script to combine tuyaapi and openhab via mqtt
MIT License
173 stars 80 forks source link

Feature/configurable version support #26

Closed d-lamers closed 4 years ago

d-lamers commented 4 years ago

The new tuyaapi npm package has support for newer devices by setting the protocol version to 3.3, this is something I needed for the newer tuya devices I got.

This small PR adds the following features:

tsightler commented 4 years ago

Thanks for the PR but I already have a build, available here, that fully supports protocol 3.3 and updates to newer packages as well. Your PR would only work for cases where all devices in an environment were a single protocol which I think is less than ideal as a lot of people will have devices with different protocols (I'm one of them!).

The code I have written allows specification of the protocol on a per-device level and also supports auto-IP discovery which doesn't require specifying the protocol at all (the discovery packets include the protocol version) so will potentially make configuration easier (you don't even have to specific the IP address).

I've had a number of users testing my build for a while with no issues (once we got the memory leak in tuyapi 5.1.1 fixed) but I want to clean up and simplify the topic configuration a little more before I merge it. I'll try to get that done next week, but thanks for submitting the PR and supporting the project.

d-lamers commented 4 years ago

Ah yes, I figured you could run multiple instances for different protocol versions. I'll take a look at your PR when its ready. Taking a quick peek at your readme, I do have a question. Is it also possible to not use discove but still work with Protocol version 3.3?

tsightler commented 4 years ago

Yes, it is. Since current script versions no longer require (or even use) the "socket" or "lightbulb" topic level, but this topic level is still accepted for legacy compatibility, I just overloaded that topic level so that you can specify the protocol version like so:

tuya/ver3.3/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/state
tuya/ver3.3/<tuyAPI-id>/<tuyAPI-key>/<tuyAPI-ip>/command

If you don't specify the version in the topic, but do specify the IP address, the current default is to use protocol 3.1, but you can always add ver3.1 or ver3.3 to the topic to specify a protocol version, regardless of the use of discovery.

I'm not sure this is the best place to do this, and it may change slightly before I release it, but it's proven to work at this point with a number of users. Happy to hear suggestions on that, but I think it's probably OK for a temporary solution.

My future plan is to support a devices.json file which specifies the device info and configures the devices on script startup. This will mean we no longer have to be dependent on retain for the MQTT topics and makes it easier to do things like define a friendly name, etc., which will allow for better auto-discovery (using MQTT discovery features in Home Assistant and OpenHAB) and I can also shorten the topic names and make them easier to use. That way the topics will be consistent and it won't matter what the protocol is as long as it's defined in the devices.json file for each device, or when auto-discovery is used.