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

Can access status of ANTELA wireless sensor #270

Closed tyge68 closed 1 year ago

tyge68 commented 1 year ago

I recently purchased a wireless sensor device to detect open/closed door (under brand Antela / DY-MC400A) which is managed via smart life / tuya.

It is listed on Tuya IoT platform as "SENSORE PORTA/FINESTRA SENZA FILI".

d = tinytuya.Device('<device id as on Tuya IoT plateform>', '<device ip detected>', '<device localkey as on Tuya IoT plateform')
d.set_version(3.3)
data = d.status() 

It simply doesn't return as if it cannot really connect or get stuck, while the same command works on other devices. Could it be that even if that is available and detected by the device scanner it won't be able to read status as maybe this is a very passive device ? (I suppose to save energy it only push information to Tuya IoT / mqtt probably when device detect that the "magnet" is out of range)

uzlonewolf commented 1 year ago

Yes, battery-operated devices shut off their WiFi module to save power and only power up to report events, you only have a window of like 1-2 seconds to poll it when it does this. WiFi is terribly power hungry and if it didn't do this the battery life would be measured in hours. There are a few work-arounds however none of them are particularly pleasant.

tyge68 commented 1 year ago

@uzlonewolf ok thanks, what would you see as a workaround (i.e reprogram the device firmware etc..) ?

uzlonewolf commented 1 year ago

Yes, replacing the firmware with either Tasmota or OpenBeken (depending on which chip the sensor uses) would be the best as it would give you complete control over the device.

The next option is to listen for the network broadcasts when the device connects to the WiFi and quickly poll it when you see them.

Another option is to use a smart plug with nothing plugged into it and configure it so that it turns on when the sensor opens and shuts off when the sensor closes. You would then just watch the status of this smart plug.

The problem with those last 2 options is they will miss events. It takes the sensor several seconds to connect to the WiFi, and if there are multiple events it only reports about once per minute, and during this time you will not see any of the events.

If you do not need to see the events as they happen in real time and instead don't mind looking at the logs later, you could also use tinytuya.Cloud() to pull the device logs.

tyge68 commented 1 year ago

@uzlonewolf Thanks a lot, I will explore the options, at least for that device it's ok if I don't really integrate it later in my home assistant , it would be a plus, but I suppose the best would be that I find another device in the future what would allow custom / local mqtt integration.