raetha / wyzesense2mqtt

Configurable WyzeSense to MQTT Gateway intended for use with Home Assistant or other platforms that use MQTT discovery mechanisms.
MIT License
79 stars 22 forks source link

Major updates and fixes #67

Open nabahr opened 1 year ago

nabahr commented 1 year ago

Added availability topic in HA as V1 devices report state every 4 hours at most, and V2 devices report every 2 hours at most. Added a state file that gets saved on shutdown. This file saves the last seen timestamp and online state for each sensor so that it has consistancy across reboots.

Switched mqtt connection method so it connects faster and availability topics are sent after the connection is active rather than before. This also always the main thread to loop and check availability of the sensors.

The main loop also checks communication with the dongle, by getting the mac address, which allows us to close and reopen the dongle if we lose communication with the device. This is an issue I've been dealing with for a long time.

Fixed shutdown by sending the correct signal in the service file and properly handling it in shutdown. Wait for dongle loop to finish before closing the fd.

Updated logging: set sample logging conf to INFO instead of DEBUG and updated log messages to primarily log mqtt messages/events, etc at this level. Passing in LOGGER to the dongle class so logs from the dongle show up in the log file.

Fix packet parsing to not throw away partial packets which can be parsed correctly when the rest of the message is received.

Signed-off-by: Nathan Bahr nabahr@users.noreply.github.com

raetha commented 1 year ago

Hi @nabahr. This looks great, and I'm happy to see some activity after so long. Unfortunately I never got any of the v2 stuff that others were working on and was unable to help test or resolve issues in that realm. I'm also a bit rusty on this stuff given how long I've been away from it, so going to request that @drinfernoo and @AK5nowman do code reviews if they see this and have a moment. Otherwise I'll be happy to merge in soon. One suggestion though is to review the readme file as well for potential updates on the new configuration file, and anything that someone might need to know about v2 sensors. Thanks!

nabahr commented 1 year ago

Yes, there are quite a few changes here that will need some additional testing and I am sure will need some further refinement. I just wanted to throw it out there now to get some feedback.

I've got the original dongle and a bunch of original motion and contact sensors, plus a handful of the new motion and contact sensors. Unfortunately, I do not have any of the other new stuff that may be supported.

nabahr commented 1 year ago

So my main goal when I started this was to add the availability topic when I realized that the sensors sent periodic status messages, but it ballooned a bit more than that. My question as far as maintaining history is if you'd want me to go through and break things up into smaller commits or if squashing it all together is fine?

nabahr commented 1 year ago

I've gone through and cleaned up a few more little things and I believe it should be ready for others to test. In all my messing around with hass discovery, I've been able to mess up my mqtt topics and had to wipe out the wyzesense2mqtt and discovery topics and start fresh, but I think that switching to this branch and running it should work fine with no changes required to any config files.

One more thing I've been thinking about adding is a config option to have the entitiy_id automatically updated in hass as well. Looks like setting the object_id instead of name in the discovery config will do that. I may just save that for another PR though.

drinfernoo commented 1 year ago

Hey, I am planning to do a review on this soon, but I've been pretty dang busy lately myself.

I'll try to remember to take a look in the near future.

nabahr commented 1 year ago

Somehow a typo made it in my last push, fixed that. Also added a flag to block the processing of dongle events until everything is initialized.

nabahr commented 1 year ago

Just wanted to bump this PR and say that I've been running this for a while now with success and the device availability feature has been quite useful because when a device goes offline you have at least a day or two to change the battery and avoid the null mac issue, making the v1 sensors a bit more reliable long term.