Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Switchbot support and discovery. #995

Merged
merged 1 commit into from
Apr 30, 2022

Conversation

h2zero
Copy link
Collaborator

@h2zero h2zero commented Jun 22, 2021

Adds Switchbot support and automatic discovery for Home Assistant and OpenHAB.

Improves BLE command support by using a one-shot task to process immediate BLE actions, this prevents MQTT disconnections if the action takes too long to complete.

Adds state_on and state_off parameters to createDiscovery().

@darkjavi
Copy link

darkjavi commented Jul 8, 2021

Just received 4 MagicSwitchbot units and can try this. Already compiled and uploaded the firmware in the pr but cannot figure out the command I have to send to trigger the switch.

Im guessing its a write command like the one in ble.md

mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{
"ble_write_address":"AA:BB:CC:DD:EE:FF",
"ble_write_service":"cba20d00-224d-11e6-9fb8-0002a5d5c51b",
"ble_write_char":"cba20002-224d-11e6-9fb8-0002a5d5c51b",
"ble_write_value":"TEST",
"value_type":"STRING",
"ttl":4 }'

but with the correct mac and payload.
any hint?

@h2zero
Copy link
Collaborator Author

h2zero commented Jul 8, 2021

@darkjavi That should be working, I do not know what value / value type the switchbot needs to receive but you may need to change that as well.

@darkjavi
Copy link

darkjavi commented Jul 8, 2021

been looking to this python lib to manage the switchbot:
https://github.com/OpenWonderLabs/python-host/blob/master/switchbot.py

if act == "Turn On":
    con.sendline('char-write-cmd ' + cmd_handle + ' 570101')
elif act == "Turn Off":
    con.sendline('char-write-cmd ' + cmd_handle + ' 570102')
elif act == "Press":
    con.sendline('char-write-cmd ' + cmd_handle + ' 570100')
elif act == "Down":
    con.sendline('char-write-cmd ' + cmd_handle + ' 570103')
elif act == "Up":
    con.sendline('char-write-cmd ' + cmd_handle + ' 570104')

Looks like it needs a handle+fixed command , don't know yet how to generate that and the char-write-cmd

@h2zero
Copy link
Collaborator Author

h2zero commented Jul 8, 2021

@darkjavi I used this https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/main/SwitchBot-BLE2MQTT-ESP32.ino as a reference for the basic implementation. Maybe that will help.

@nielsfaber
Copy link

I have 2 SwitchBot curtains which I want to integrate with Home Assistant.
I tried compiling and running this branch on an ESP32 via Arduino SDK, but couldn’t reach very far unfortunately.
I can confirm that the SwitchBots are seen (their MACs are showing up in the MQTT traffic).
The additional steps needed for open/close functionality and reading the state info are unclear to me.
I switched now to https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32 which gives a very good out-of-the-box experience (curtains show up as cover entities in HA through discovery).
This PR looks very promising as I like OMG for versatility but i think I’m missing some steps/docs to get it up-and-running.
If anyone could give some pointers I would happily try again.

@h2zero
Copy link
Collaborator Author

h2zero commented Aug 16, 2021

Thanks for the feedback and the link. I am looking for specifics on the commands to interface with the switchbots as I do not have any. This is just a base framework to expand on. If you know the data and commands to read/write for your application please post the info and I'll be glad to write docs and or update the code for support.

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 9, 2022
@h2zero
Copy link
Collaborator Author

h2zero commented Jan 9, 2022

Bump, not stale.

@DigiH
Copy link
Collaborator

DigiH commented Mar 18, 2022

Hi @h2zero @1technophile I'm wondering how this project for SwitchBot connectivity is planned for the future? Like with the recent SwitchBot S1 status decoder this could allow for all READ commands info to be substituted by the advertised broadcast information through status decoders, while WRITE commands are issued through this connectivity implementation through a encoder-like substitution in place of the raw hex commands?!?

I'm just wondering if a split implementation like this is planned, or if all WRITE and READ will be implemented here?

If the former, and with the detailed SwitchBot APIs, which @1technophile linked to and the S1 decoder is based on, I'd be happy to work on the other SwitchBot devices' status decoder implementations, like @nielsfaber's SwitchBot curtains etc.

With the API info the WRITE commands should also be clearly defined.

Thanks

@1technophile
Copy link
Owner

Hi @DigiH,

I'm planning to integrate the new version of the decoder and leverage it for identifying the SwitchBot S1, once identified we will auto-discover those as a switch or press button depending on the mode.
I'm planning to use the regular connectivity implementation for the WRITE commands and to avoid READ through the connection

So yes you can go ahead and implement the Switchbot devices that you want, it will be a great contribution to the decoder!

@DigiH
Copy link
Collaborator

DigiH commented Mar 18, 2022

Hi @1technophile sounds great, thanks for the info. I already created a Curtain status decoder at

https://github.com/DigiH/decoder/tree/switchbot-curtain

So @nielsfaber or anyone else with the SwitchBot Curtain if you could test this version, as the test cases I included are only based on the API documentation, but no real world device testing.

Currently the decoder contains the status for motionstate, position, calibrated (only gets published if state is false, i.e. calibration is needed), lightlevel and battery.

Bit [7] – Whether to allow connection 1- allowed, 0- not allowed

is not currently implemented, but might actually be needed/helpful for WRITE connections.

Probably best to move any further discussions of SwitchBot devices' status decoders here

theengs/decoder#95

and leave this for further control implementation exchange.

Thanks

@h2zero
Copy link
Collaborator Author

h2zero commented Apr 10, 2022

Rebased now to latest development branch, needed to bump decoder version also.

@h2zero h2zero force-pushed the add-switchbot branch 3 times, most recently from 50ebe6c to da4c942 Compare April 25, 2022 15:10
* Use a task to process immediate BLE actions to prevent MQTT disconnection if the action takes too long to complete.

* Add state_on and state_off parameters to createDiscovery
@h2zero h2zero changed the title EXPERIMENTAL: Do not merge - switchbot connectivity. Add Switchbot support and discovery. Apr 28, 2022
@h2zero h2zero marked this pull request as ready for review April 28, 2022 01:01
@1technophile
Copy link
Owner

@nielsfaber @darkjavi This PR adds Switchbot S1 (button) autodiscovery and control, would you like to test it?
Note that we don't support password for the moment

@nielsfaber
Copy link

@1technophile thanks for your efforts, sounds great!
I only have the SwitchBot curtain modules, so I can only test those. Is this included in this version?

As said in my previous comment, at this point I do not know how to send (or request) the open/closed state of these modules, so I would need some help regarding the topic/payload data that is used (unless they are automatically added as cover entities in HA).

@DigiH
Copy link
Collaborator

DigiH commented Apr 28, 2022

Hi @nielsfaber, did you ever get a chance to test the SwitchBot curtain status decoder I mentioned above?

While it doesn't allow you to send commands to the SwitchBot curtain modules, it shows you their current status (motion state, position, calibration status, light level and battery) and status changes which you might have initiated through the SwitchBot app so far, and similarly to the current SwitchBot S1 implementation is part of getting a full SwitchBot curtain OMG inclusion ready.

If you feel like testing the status decoder and giving some feedback please use the SwitchBot decoder discussion

theengs/decoder#95

Thanks

@1technophile 1technophile merged commit 8d6acbe into 1technophile:development Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants