jasonacox / tinytuya

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

How to do something when the door opens? #264

Open anar4732 opened 1 year ago

anar4732 commented 1 year ago

I want to do something instantly when the door opens. so the idea of checking the status every minute not works. Since the device disconnects from the network on sleep mode I think should do something with the cloud.

uzlonewolf commented 1 year ago

I do not know of any good way of doing this, either via the cloud or locally. You might be able to either have your router trigger something on a DHCP request or have a python script listening for said DHCP broadcasts, however that will miss events if the door opens and then closes too fast.

If you don't care about using Tuya's app then loading a non-cloud firmware onto the device would be a better option.

Do you have a link for the door sensors you have?

anar4732 commented 1 year ago

@uzlonewolf here

fhempy commented 1 year ago

Some time ago I implemented a solution for fire sensors which work the same way - they only go online on alarm. I tried every 15s an async connect request with a timeout of 15s. I couldn't test it myself but users reported that it worked.

anar4732 commented 1 year ago

Nice idea! Thank you!

fhempy commented 1 year ago

https://github.com/fhempy/fhempy/blob/v0.1.491/FHEM/bindings/python/fhempy/lib/tuya/tuya.py#L400

Here is the code, I used pytuya in that case.

uzlonewolf commented 1 year ago

Well here's an idea I just had. What if you took an extra smart plug, configured scene linkage/automation to turn it on when the door opens and turn it off when the door closes, and had tinytuya monitor the socket status? Would that work? I guess I'll find out tomorrow when my door sensors get here.

uzlonewolf commented 1 year ago

And I totally forgot to post an update with my results. It... kinda/sorta works. The biggest issue is the door sensor does not always report immediately, more than 1 open/close action causes it to queue up the events and report them all at once after about a minute. Also, if the door is open for less than 1 second the smart plug reports 2 off events instead of the expected on+off. Beyond that it actually works pretty well.

rlm96 commented 1 year ago

In case anyone is interested, I have been able to use the (UDP) 6667 port broadcast capture approach (check main.py)