You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

  1. #pragma once
  2. #include "../common.h"
  3. #include "../front_panel_hal.h"
  4. #include "esphome/components/binary_sensor/binary_sensor.h"
  5. namespace esphome {
  6. namespace yeelight {
  7. namespace bs2 {
  8. /**
  9. * This class implements a binary sensor for the buttons on the
  10. * Yeelight Bedside Lamp 2.
  11. */
  12. class YeelightBS2Button : public binary_sensor::BinarySensor, public Component {
  13. public:
  14. void set_parent(FrontPanelHAL *front_panel) {
  15. front_panel_ = front_panel;
  16. }
  17. void set_part(int part) {
  18. part_ = part;
  19. }
  20. void setup() {
  21. front_panel_->add_on_event_callback(
  22. [this](EVENT ev) {
  23. // Filter events by part, when requested.
  24. if (part_ > 0) {
  25. if ((ev & FLAG_PART_MASK) != (part_ << FLAG_PART_SHIFT)) {
  26. return;
  27. }
  28. }
  29. // Publish the new state, based on the touch/release status..
  30. auto on_or_off = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  31. this->publish_state(on_or_off);
  32. }
  33. );
  34. }
  35. void dump_config() {
  36. ESP_LOGCONFIG(TAG, "Front panel binary_sensor:");
  37. ESP_LOGCONFIG(TAG, " Part: %s (id %d)",
  38. (part_ == 1 ? "power button" :
  39. part_ == 2 ? "color button" :
  40. part_ == 3 ? "slider" : "any"),
  41. part_);
  42. }
  43. protected:
  44. FrontPanelHAL *front_panel_;
  45. EVENT part_;
  46. };
  47. } // namespace bs2
  48. } // namespace yeelight
  49. } // namespace esphome