StyraHem / ShellyForHASS

Shelly smart home platform for Home Assistant
MIT License
621 stars 112 forks source link

Handling complex click events on Shelly1, Shelly2.5, Shelly dimmer and Shelly dimmer2 with fw 1.8.x and above #438

Open g-kiss opened 4 years ago

g-kiss commented 4 years ago

Overwiev

I created a new event to handle complex events on fw 1.8.x and above. I tested it on Shelly1, Shelly2.5, Shelly dimmer and Shelly dimmer2. With a new "momentary_button" yaml parameter defines which button is connected to Shelly. If the clicking speed is slow or fast or very fast this event works very well (its accurate is 100%).

These events allows to:

See the examples below.

This PR needs the (StyraHem/pyShelly#42) PR too.

1. Momentary button

1.1 If a momentary button is attached to Shelly, then the momentary_button parameter in yaml must be set to true. click_events contains a complex event, like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_haloszoba_mennyezet_switch",
        "click_events": "SSL"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:16:34.063499+00:00",
    "context": {
        "id": "52343a3df34611eab4b431423721197c",
        "parent_id": null,
        "user_id": null
    }
}

This event will be send after last release of button.

1.2 In case of long push, it also sends another event, when the long push starting (LSTART) or stopping (LSTOP), like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_haloszoba_mennyezet_switch",
        "click_events": "SSLSTART"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:24:55.476679+00:00",
    "context": {
        "id": "7d11ce25f34711eab2829b2412e9ff51",
        "parent_id": null,
        "user_id": null
    }
}

"LSTART" and "LSTOP" can be used when the long push is the last action. With "LSTART" event can start executing the action before releasing the long push.

1.3 The time diagram of events kép

2. Toggle button

If a toggle button is attached to Shelly, then the momentary_button parameter in yaml must be false (this is the default) and in shelly settings (web interfacae) the long push time must be set to 10 ms. In this case the click_count contains number of clicks, like this:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_barni_agy_melletti_switch",
        "click_count": 3
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-10T09:39:51.135123+00:00",
    "context": {
        "id": "92ec6fd3f34911ea9ed6f77f27f2ff32",
        "parent_id": null,
        "user_id": null
    }
}

3. Examples

3.1 Shelly1 with momentary, detached button:

3.2 Shelly dimmmer with momentary, detached button:

gerard33 commented 4 years ago

Tried this on a Shelly dimmer (first model, firmware 1.8) with a momentary switch, but I get no shelly_click events in HA when pressing either of the 2 buttons. Anything I can add in the code to make this work?

thecem commented 4 years ago

@gerard33 Yes, RTFM ;-) -> Sensors in https://github.com/StyraHem/ShellyForHASS#sensor-global-and-per-device

Or add

shelly:
  sensors:
    - all #show all sensors
    #or
    - switch #show only switch actions

I searched also a lot ;-), happy to help...

gerard33 commented 4 years ago

@thecem I meant if I can add anything in the .py code as addition to this PR as it doesn't work for Shelly dimmers 😉

My config contains the part you are mentioning

shelly:
  sensors:
    - switch

The shellyforhass.click event is showing data for my Shelly dimmer, but always as a single press. And this PR should make double press etc. possible again.

thecem commented 4 years ago

Oh this would be nice!

Do you receive only the shellyforhass.click or also the shelly_switch_click, in HA 0.115.0 I´m not able to search with wildcard like *shelly*. So I only receive for shelly 2.5 on double click a message for cnt with shelly_switch_click and the one shellyforhass.click

g-kiss commented 4 years ago

I tested it with HA 115.0, Shelly Dimmer and momentary_button: true in yaml. It works. I get these events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "S"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T08:51:53.561663+00:00",
    "context": {
        "id": "330f9255f98c11eab1ac3320c994fda4",
        "parent_id": null,
        "user_id": null
    }
}
{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "L"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T08:51:52.304406+00:00",
    "context": {
        "id": "324fbb07f98c11ea8f43cb6a68433bdb",
        "parent_id": null,
        "user_id": null
    }
}
thecem commented 4 years ago

Undocumented feature: momentary_button: true ?

I get also the info without this config in HA 115 but I have exactly to search for shelly_switch_click and shellyforhass.click, but not a shelly_click, maybe there should be a detailed explanation...

g-kiss commented 4 years ago

@gerard33, @thecem This PR needs the (https://github.com/StyraHem/pyShelly/pull/42) PR too.

gerard33 commented 4 years ago

Ah that makes sense 👍

gerard33 commented 4 years ago

@g-kiss I have added your pyShelly PR and tested it with my Shelly dimmer on fw1.8 and I get the click_count. I have set the following in configuration_schema.py as I have only momentary switches with the Shelly devices -> vol.Optional(CONF_MOMENTARY_BUTTON, default=True): cv.boolean so would have expected the click_events.

But for me this is working fine like this as well. Will do some more testing, but it seems really reliable and get's up to 5-clicks 👍

Below an example for a triple click.

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.shelly_kantoor_2_switch",
        "click_count": 6,
        "click_events": ""
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:29:23.558574+00:00",
    "context": {
        "id": "a39a5039f9ed11ea8286f382ca21a762",
        "parent_id": null,
        "user_id": null
    }
}
g-kiss commented 4 years ago

@gerard33 My yaml is like this:

  devices:
    - id: "xxxxxxxx"
      name: Fürdőszoba mennyezet
      entity_id: lampa_furdoszoba_mennyezet
      light_switch: true
      momentary_button: true

and gives click_events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "LS"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:56:48.610026+00:00",
    "context": {
        "id": "782140e9f9f111ea98aa79d2c3fe8fd7",
        "parent_id": null,
        "user_id": null
    }
}
thecem commented 4 years ago

Is it possible to set the variable global in yaml? I have only momentary switches.

gerard33 commented 4 years ago

@g-kiss I have added momentary_button: true for the device I am testing with and then it shows S etc. indeed.

I didn't have that device_id in the devices: list and apparently it needs to be in or otherwise it wont get the momentary_button value, even not if it's set to default = true (changed that locally).

g-kiss commented 4 years ago

@gerard33 The button type (momentary or toggle) is the property of device. It must be assigned to device, because for example I have about 10 Shellys with momentary button and about 15 Shellys with toggle button.

danstr1 commented 4 years ago

Hi, there is any estimation date when it will be merged to the master?

Thanks in advance

jacobhallgren commented 3 years ago

@gerard33 My yaml is like this:

  devices:
    - id: "xxxxxxxx"
      name: Fürdőszoba mennyezet
      entity_id: lampa_furdoszoba_mennyezet
      light_switch: true
      momentary_button: true

and gives click_events:

{
    "event_type": "shelly_click",
    "data": {
        "entity_id": "binary_sensor.lampa_furdoszoba_mennyezet_switch",
        "click_count": 0,
        "click_events": "LS"
    },
    "origin": "LOCAL",
    "time_fired": "2020-09-18T20:56:48.610026+00:00",
    "context": {
        "id": "782140e9f9f111ea98aa79d2c3fe8fd7",
        "parent_id": null,
        "user_id": null
    }
}

Where can I look for the events when they are fired? In some log file?

g-kiss commented 3 years ago

@jacobhallgren You can see it on "developer tools" / "events" page of HA web interface.

jacobhallgren commented 3 years ago

@hakana Please add this to master

libussa commented 3 years ago

are single, long and double clicks already supported? Seeing this now https://github.com/StyraHem/ShellyForHASS#events

g-kiss commented 3 years ago

@hakana Do you have any plan with this PR?

hakana commented 3 years ago

I have tried this but it is not stable when testing. So I need to investigate more around this.

jacobhallgren commented 3 years ago

I have tried this but it is not stable when testing. So I need to investigate more around this.

Works great for me, use it every day for 2 months. No problems.

gerard33 commented 3 years ago

For what it's worth, it's working fine for me for several months as well. Double and triple clicks for all Shelly devices easily available now.

wauswaus commented 3 years ago

Hi @hakana, did you had a chance of looking into this a bit more? I would love to use this.

@g-kiss, are you planning on adding this functionality to the Shelly Home Assistant native integration as well? I can imagine more people would really like to use it.

g-kiss commented 3 years ago

Hi @wauswaus, I will thinking about it

vipial1 commented 2 years ago

Would be awesome to have this on master!

vipial1 commented 2 years ago

@g-kiss I would like to be part of the testing...how can I install your change?

wauswaus commented 2 years ago

Hi @wauswaus, I will thinking about it

What's the outcome? I'm running the native integration nowadays . Would be nice to have your nice work in there as well

g-kiss commented 2 years ago

Hi @wauswaus, see this PR: https://github.com/home-assistant/core/pull/58436

gerard33 commented 2 years ago

@g-kiss Have migrated to the native HA integration since a couple of weeks and added your code and that works like a charm! Thanks for sharing it 👍🏻 Hopefully it will be added to the native HA integration or library soon.