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.

53 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 xiaomi {
  7. namespace bslamp2 {
  8. /**
  9. * This class implements a binary sensor for the touch buttons
  10. * and touch slider on the front panel of the Xiaomi Mijia Bedside Lamp 2.
  11. */
  12. class XiaomiBslamp2TouchBinarySensor : public binary_sensor::BinarySensor, public Component {
  13. public:
  14. void set_parent(FrontPanelHAL *front_panel) {
  15. front_panel_ = front_panel;
  16. }
  17. void set_for(int part) {
  18. for_ = part;
  19. }
  20. void setup() {
  21. front_panel_->add_on_event_callback(
  22. [this](EVENT ev) {
  23. auto part_in_event = ev & FLAG_PART_MASK;
  24. if (for_ == 0 || part_in_event == for_) {
  25. auto is_touch = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  26. this->publish_state(is_touch);
  27. }
  28. }
  29. );
  30. }
  31. void dump_config() {
  32. ESP_LOGCONFIG(TAG, "Front panel binary_sensor:");
  33. ESP_LOGCONFIG(TAG, " For: %s",
  34. for_ & FLAG_PART_POWER ? "power button" :
  35. for_ & FLAG_PART_COLOR ? "color button" :
  36. for_ & FLAG_PART_SLIDER ? "slider" : "ERR");
  37. }
  38. protected:
  39. FrontPanelHAL *front_panel_;
  40. EVENT match_ = 0;
  41. EVENT for_ = 0;
  42. };
  43. } // namespace bslamp2
  44. } // namespace xiaomi
  45. } // namespace esphome