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.

63 lines
1.7 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 include_part(int part) {
  18. match_ = match_ | part;
  19. }
  20. void setup() {
  21. front_panel_->add_on_event_callback(
  22. [this](EVENT ev) {
  23. auto part = ev & FLAG_PART_MASK;
  24. auto is_touch = (ev & FLAG_TYPE_MASK) == FLAG_TYPE_TOUCH;
  25. if (is_touch) {
  26. active_ = active_ | part;
  27. } else {
  28. active_ = active_ & ~part;
  29. }
  30. this->publish_state(active_ == match_);
  31. }
  32. );
  33. }
  34. void dump_config() {
  35. ESP_LOGCONFIG(TAG, "Front panel binary_sensor:");
  36. ESP_LOGCONFIG(TAG, " Part(s):");
  37. if ((match_ & FLAG_PART_POWER) == FLAG_PART_POWER) {
  38. ESP_LOGCONFIG(TAG, " - Power button");
  39. }
  40. if ((match_ & FLAG_PART_COLOR) == FLAG_PART_COLOR) {
  41. ESP_LOGCONFIG(TAG, " - Color button");
  42. }
  43. if ((match_ & FLAG_PART_SLIDER) == FLAG_PART_SLIDER) {
  44. ESP_LOGCONFIG(TAG, " - Slider");
  45. }
  46. }
  47. protected:
  48. FrontPanelHAL *front_panel_;
  49. EVENT match_ = 0;
  50. EVENT active_ = 0;
  51. };
  52. } // namespace bslamp2
  53. } // namespace xiaomi
  54. } // namespace esphome