I’m planning on a proper rewrite of the Switchmate integration. The original implementation is buggy: It only turns the light on/off 50% successfully of the time, it does not reliably read out the current status of the switch if manually switched, and it sometimes makes my entire home assistant instance stuck.
I wonder if there is an example of a good Bluetooth component in Bluetooth?
In particular, I wonder how to handle the following stuff:
- I need to scan for Bluetooth beacon to update the current status of the switch. How can I do it so that it’s not in conflict with other components that also want to do Bluetooth scan?
- I need to connect to the device when I want to flip the switch. Should I connect every time and disconnect afterward? Or I should keep the connection open just in case a second command comes? Also, how to make this work with asyncio?
- For connect, similar to all BLE devices, the connect command may fail. It could be because a poor wireless environment or other devices is current connected to the switch. How can I keep trying to establish the connection without stuck the entire home assistant process?
For the reference, Switchmate is a smart switch roughly work like this:
- It always broadcast a beacon with one bit indicating whether it’s current status (on/off).
- If the user wants to control this switch, the system needs to first connect to the switch, write a value to a Bluetooth attribute, then the switch will toggle.
Thanks!