devWaves / SwitchBot-MQTT-BLE-ESP32

Allows for multiple SwitchBot bots and curtains to be controlled via MQTT sent to ESP32. ESP32 will send BLE commands to switchbots and return MQTT responses to the broker. Also supports Temperature, Motion, Contact sensors
MIT License
549 stars 68 forks source link

Added support for Home Assistant MQTT Discovery #6

Closed HardcoreWR closed 3 years ago

HardcoreWR commented 3 years ago

Added Home Assistant MQTT Discovery in this commit. Please review and adjust if necessary.

devWaves commented 3 years ago

nice work. I'll try to review that this weekend

HardcoreWR commented 3 years ago

I made an extra change regarding the Prometheus metric for the curtain position. Forgot to set the device_class so it wasn't exported properly.

I need to take a look how I can add that to this Pull Request.

devWaves commented 3 years ago

I think i see a reference to curtain in the bot auto discovery JSON...

HardcoreWR commented 3 years ago

I think i see a reference to curtain in the bot auto discovery JSON...

Thanks, didn't notice it. I fixed and pushed the change.

Edit: Looks like the PR will automatically include the newest commits

devWaves commented 3 years ago

thanks. Does your bot auto discovery account for bots in both modes ? 1) press mode 2) switch mode

In press mode they work as a single button push action. I'll see when I try it out how it behaves

the mode is part of the info payload from the bot

the bot 'state' is basically irrelevant when in press mode

HardcoreWR commented 3 years ago

thanks. Does your bot auto discovery account for bots in both modes 1) press mode 2) switch mode. In press mode they work as a single button push action. I'll see when I try it out how it behaves

the mode is part of the info payload from the bot

the bot 'state' is basically irrelevant when in press mode

It doesn't yet, mainly because I didn't know they could have different modes. I just ordered a Bot and a Meter so I can test some more and hopefully improve the discovery for these 2 devices.

devWaves commented 3 years ago

It doesn't yet, mainly because I didn't know they could have different modes. I just ordered a Bot and a Meter so I can test some more and hopefully improve the discovery for these 2 devices.

cool. Ok I only use bots in press mode so ill try to take a look at that. That was one of the things I wasn't sure about when I looked into mqtt discovery

I'm guessing https://www.home-assistant.io/integrations/device_trigger.mqtt/

Schluggi commented 3 years ago

I'm guessing https://www.home-assistant.io/integrations/device_trigger.mqtt/

The device trigger discovery is for devices like my wall switch. So HomeAssistant can get the action of the press (double press, long press, etc).

As the doc say:

The mqtt device trigger platform uses an MQTT message payload to generate device trigger events. So, it is just a one way communication (device event -> HomeAssistant).

For the switch implementation you should use: https://www.home-assistant.io/integrations/switch.mqtt/

I don't own a bot, so maybe my thoughts are wrong.

devWaves commented 3 years ago

@Schluggi you're probably correct. I think that is what @HardcoreWR used. Makes sense. New to HA and just looked for button/press/remote references

The switch is good for on/off (while in 'switch' mode), but in 'press' mode the state doesn't matter. It is for pushing things like PC/tv/coffee power buttons. The bot doesn't actually know the state. I have 2 bots in 'press' mode and one returns "state": "ON", the other returns "state": "OFF". I think it stays whatever values it was last time it was in 'switch' mode

So not 100% sure if switch will work, unless the state is already in OFF mode maybe you can just press ON and it will immediately turn back to OFF? Sounds like that can work

devWaves commented 3 years ago

I did some quick testing with the bot and it seems like it works correctly

When the switch is in 'press' mode you need to make sure the state is already "state": "OFF". Otherwise the state will always remain ON. Change back to 'switch' mode, turn it off, then go back to 'press' mode

so basically someone just needs to test the meter. Once that is done I'll release a v2.0

I see batteries now. They didn't auto add to dashboard like switch part, but they were auto found

HardcoreWR commented 3 years ago

I'm expecting an Amazon delivery for the Bot and Meter tomorrow so I can do that myself.

devWaves commented 3 years ago

@HardcoreWR meters don't support a control/set command. So you can take that part out, unless HA MQTT discovery needs it for some reason

marithpl commented 3 years ago

Im testing with 2 Switchbot Curtain, Discovery works fine but I found that status are not changing properly. Ex. I full opened via HA curtain opened but after a while it shows that curtain is 100% closed.

HardcoreWR commented 3 years ago

I just received my Bot and Meter so I'm currently testing the code.

When the switch is in 'press' mode you need to make sure the state is already "state": "OFF". Otherwise the state will always remain ON. Change back to 'switch' mode, turn it off, then go back to 'press' mode

I did fix this in code so it assumes "OFF" when mode is "Press".

so basically someone just needs to test the meter. Once that is done I'll release a v2.0

I want to make some tweaks for Meter to update the discovery config whenever the scale is changing from C to F (and vice versa).

I see batteries now. They didn't auto add to dashboard like switch part, but they were auto found

That's odd, I have not seen that issue actually.

devWaves commented 3 years ago

sounds good.

When in press mode, the on command does the same things as press technically. they are different commands, but it seems to work.

assuming state "OFF" when in press mode should work. I just tested sending "ON" while state = "ON" and it did the press action

in switch mode it would be nice if the HA state didnt go from on, off, then back on as the final state. That works nice when in press mode though. at the moment it depends on the status returned from the esp32

the battery thing was probably just me not fully familiar with HA yet

devWaves commented 3 years ago

@HardcoreWR please edit your version number to v2alpha

this will avoid confusion with version 1 non HA stuff. Then I can pull your code in as contributor and I'll make some final minor edits for v2.0

devWaves commented 3 years ago

Im testing with 2 Switchbot Curtain, Discovery works fine but I found that status are not changing properly. Ex. I full opened via HA curtain opened but after a while it shows that curtain is 100% closed.

default settings?

marithpl commented 3 years ago

Default...

Here is a record from HA. 1) Set position ~50% 2) Covers moved correctly 3) Update entity 4) Check position status is updated 18 hours ago.

edited: I checked a file and I had switbotone not curtainone in refresh section. Now seems to work

https://user-images.githubusercontent.com/25366097/118602552-230cc300-b7b3-11eb-98db-50a597a0127d.MP4

Schluggi commented 3 years ago

@devWaves can you merge the pr into a new branch, so we work on it together? Or simple use github releases to publish the current version and than merge to master.

devWaves commented 3 years ago

@devWaves can you merge the pr into a new branch, so we work on it together? Or simple use github releases to publish the current version and than merge to master.

https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/tree/HA-MQTTDiscovery