jasonacox / tinytuya

Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
879 stars 159 forks source link

MQTT Gateway for Server #364

Closed mschlenstedt closed 1 year ago

mschlenstedt commented 1 year ago

Hey,

I really love the idea to include MQTT into the server. I created a seperate gateway script which passes status of devices from the Server to a MQTT Broker and subscribes to a "set" topic and passes the payload back to the set API call of the Server.

The script polls the status of all known devices (in seperate threads, 20 at the same time currently) every x seconds. Polling is not the best solution here of course, I would really love to have a persist socket to each device and get status changes immediately. But I think this needs a complete rewrite of the server. So maybe polling is "better than nothing" here.

Code needs definetly some clean up and documentation is still missing.

mschlenstedt commented 1 year ago

MQTT topic structure:

image

uzlonewolf commented 1 year ago

So, I'm currently in the middle of a server rewrite that does all this and more. It currently makes persistent connections to all devices and does this with a single thread via non-blocking sockets, supports HTTP, WebSockets, plain TCP, and all of the above wrapped in SSL/TLS. It also has hooks for native MQTT but I have not yet had a chance to hook them up yet https://github.com/uzlonewolf/tinytuya/blob/socket-server/multiserver/multiserver.py#L963 . I'm currently out of town and have not had much time to work on it, but should be able to get back to it next week.

mschlenstedt commented 1 year ago

Fully agree - best option is to have persist socket connections. Maybe it's a good idea to include the mqtt_gateway into the existing server until your new rewrite is ready for production. If you leave the MQTT structure the same, users can easily upgrade.

jasonacox commented 1 year ago

Thanks @mschlenstedt ! I agee, this is a good incremental feature for our current server.

I know @uzlonewolf 's multiserver is going to be amazing! Safe travel, LW!