Blackymas / NSPanel_HA_Blueprint

This allows you to configure your complete NSPanel via Blueprint with UI and without changing anything in the code
1.47k stars 263 forks source link

`Enhancement` Detect "confirm" or "decline" notification #2344

Closed KjellVerb closed 1 month ago

KjellVerb commented 1 month ago

Enhancement Summary

Detect "confirm" or "decline" notification

Detailed Description

When a notification is displayed, it can either be confirmed or declined via a green check or a red cross. This could very nicely be used to trigger actions. Is it possible to create a binary sensor for both "notification_confirm" and "notification_decline"?

Is it possible to set a timeout for notifications in case they are not confirmed nor declined?

Additional Context

Example: NSpanel next to my entrance door: door opens -> magnet switch toggles -> notification pops up: "switch off house?" A) --> confirm on panel --> switch off all lights and music B) --> decline on panel --> do nothing C) --> ignore for 60 seconds --> do nothing

edwardtfn commented 1 month ago

I'm pretty sure the confirm page sends a short_click event to Home Assistant when the Accept button is pressed. It would be super easy to implement something similar with the notification page buttons, as those two pages are very similar.

A sensor is also possible, but it would take more memory and as I'm fighting for each byte currently, I would most likely prefer work with this as a customization, so only users needing this will have it.

Please let me know your thoughts.

KjellVerb commented 1 month ago

With some handling on HA side (arming the correct automation when sending a notification) this would fulfil my usecase.

Do you also have an idea on whether/how you could implement a notification timeout?

KjellVerb commented 1 month ago

Never mind about the timeout, I haven't experimented with timeout_clear yet.

edwardtfn commented 1 month ago

I've implemented this in DEV and it will be part of the next release. Unfortunately for a detail on v4.3 it's quite hard to add this as a customization to the existing version, so I'm afraid you will have to wait for the new release unless you want to play with the big code.

KjellVerb commented 1 month ago

I'm not afraid to try this out myself but I'm a bit lost where to find "the big code", those files are hidden in the main git?

edwardtfn commented 1 month ago

Yeah, the files on github...

When some part of the code is not prepared for customizations, like in this case, we have to copy the entire code from the files in GitHub and replace it entirely rather then just adding to the existing code.

Please try to add only this to your panel's yaml as a customization (which is replacing the main code in this area):

display:
  - id: !extend disp1
    on_touch:
      lambda: |-
        if (!id(is_uploading_tft)) {
          timer_reset_all->execute();
          switch (page_id) {
            case 0:  // Boot
              switch (component_id) {
                case 24:  // Reboot button
                  if (!touch_event) {  // Release
                    arch_restart();
                  }
                  break;
              }
              break;
            case 8:  // Settings
              switch (component_id) {
                case 9:  // Reboot button
                  if (!touch_event) {  // Release
                    arch_restart();
                  }
                  break;
              }
              break;
            case 10:  // light
              switch (component_id) {
                case 32:  // power_button
                  if (!touch_event) {  // Release
                    ha_call_service->execute("light.toggle", "", "", detailed_entity->state.c_str());
                  }
                  break;
              }
              break;
            case 16:  // notification
              switch (component_id) {
                case 7:  // bt_accept
                  ha_button->execute("notification", "bt_accept", touch_event ? "press" : "released");
                  if (!touch_event) {  // Release
                    notification_label->publish_state("");
                    notification_text->publish_state("");
                    notification_unread->turn_off();
                    goto_page->execute("home");
                    set_component_visibility->execute("home.bt_notific", false);
                  }
                break;
                case 8:  // bt_clear
                  ha_button->execute("notification", "bt_clear", touch_event ? "press" : "released");
                  if (!touch_event) {  // Release
                    notification_unread->turn_off();
                    goto_page->execute("home");
                  }
                break;
              }
              break;
            case 22:  // fan
              switch (component_id) {
                case 13:  // bt_oscillate
                  if (!touch_event) {  // Release
                    ha_call_service->execute("fan.oscillate", "oscillating", "toggle", detailed_entity->state.c_str());
                  }
                  break;
                case 14:  // power_button
                  if (!touch_event) {  // Release
                    ha_call_service->execute("fan.toggle", "", "", detailed_entity->state.c_str());
                  }
                  break;
              }
              break;
          }
        }

Please let me know the results.

KjellVerb commented 1 month ago

I really tried to search before asking any obvious questions but I cannot for the life of me find how to capture the ha_button event in a Home Assistant automation...

EDIT: found it!!! For future reference: image

edwardtfn commented 1 month ago

Nice! As you are sharing your findings, I'd like to share a good way to get there...

Go to Developer Tools and select the tab "Events", then select the event esphome.nspanel_ha_blueprint to subscribe and click "Start listening". The system will show all the events coming from those panels. image

Usually an event will always come with your panel's device ID (which is guaranteed to be unique) and the device name (which is probably unique). Those are useful to filter only events from a specific panel in case you have more than one. Then you will see the version of firmware installed on ESPHome (used by the blueprint to do some adaptation to specific versions) and the data specific of your event:

event_type: esphome.nspanel_ha_blueprint
data:
  device_id: dd0a1a9afc1374a33028daf7504e9d6c
  device_name: office_workstation_panel
  esphome_version: 4.4.0.dev3
  type: button_click
  page: notification
  component: bt_accept
  command: released
origin: LOCAL
time_fired: "2024-10-23T21:03:43.738375+00:00"
context:
  id: 01JAXK2KKTCCVV2MEZFN7T26F7
  parent_id: null
  user_id: null

You can use any of those attributes from data to filter your trigger. 😉 Lot's of things happening on the panel will trigger events, as this is the basis of Home Assistant to handle changes.