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.

55 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. ESP_LOGCONFIG(TAG, "Setting up binary_sensor ...");
  22. ESP_LOGCONFIG(TAG, " Part: %s (id %d)",
  23. (part_ == 1 ? "power button" :
  24. part_ == 2 ? "color button" :
  25. part_ == 3 ? "slider" : "any"),
  26. part_);
  27. front_panel_->add_on_event_callback(
  28. [this](EVENT ev) {
  29. // Filter events by part, when requested.
  30. if (part_ > 0) {
  31. if ((ev & FLAG_PART_MASK) != (part_ << FLAG_PART_SHIFT)) {
  32. return;
  33. }
  34. }
  35. // Publish the new state, based on the touch/release status..
  36. auto on_or_off = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  37. this->publish_state(on_or_off);
  38. }
  39. );
  40. }
  41. protected:
  42. FrontPanelHAL *front_panel_;
  43. EVENT part_;
  44. };
  45. } // namespace bs2
  46. } // namespace yeelight
  47. } // namespace esphome