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.

52 lines
1.4 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_front_panel_hal(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 id: %d", part_);
  23. front_panel_->add_on_event_callback(
  24. [this](EVENT ev) {
  25. // Filter events by part, when requested.
  26. ESP_LOGI(TAG, "FILTER event=%d, part=%d", ev, part_);
  27. if (part_ > 0) {
  28. if ((ev & FLAG_PART_MASK) != (part_ << FLAG_PART_SHIFT)) {
  29. return;
  30. }
  31. }
  32. // Publish the new state, based on the touch/release status..
  33. auto on_or_off = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  34. this->publish_state(on_or_off);
  35. }
  36. );
  37. }
  38. protected:
  39. FrontPanelHAL *front_panel_;
  40. EVENT part_;
  41. };
  42. } // namespace bs2
  43. } // namespace yeelight
  44. } // namespace esphome