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.

56 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) { front_panel_ = front_panel; }
  15. void set_for(int part) { for_ = part; }
  16. void setup() {
  17. front_panel_->add_on_event_callback([this](EVENT ev) {
  18. auto part_in_event = ev & FLAG_PART_MASK;
  19. if (for_ == 0 || part_in_event == for_) {
  20. auto new_state = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  21. this->publish_state(new_state);
  22. }
  23. });
  24. }
  25. void dump_config() {
  26. ESP_LOGCONFIG(TAG, "Front panel binary_sensor:");
  27. ESP_LOGCONFIG(TAG, " For: %s", format_part());
  28. }
  29. protected:
  30. FrontPanelHAL *front_panel_;
  31. EVENT for_ = 0;
  32. const char *format_part() {
  33. switch (for_) {
  34. case FLAG_PART_POWER:
  35. return "power button";
  36. case FLAG_PART_COLOR:
  37. return "color button";
  38. case FLAG_PART_SLIDER:
  39. return "slider";
  40. default:
  41. return "ERR";
  42. }
  43. }
  44. };
  45. } // namespace bslamp2
  46. } // namespace xiaomi
  47. } // namespace esphome