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.

69 lines
1.9 KiB

  1. #pragma once
  2. #include "../common.h"
  3. #include "interfaces.h"
  4. #include "esphome/components/light/light_state.h"
  5. namespace esphome {
  6. namespace xiaomi {
  7. namespace bslamp2 {
  8. // Can be replaced with light::LightStateRTCState once pull request
  9. // https://github.com/esphome/esphome/pull/1735 is merged.
  10. struct MyLightStateRTCState {
  11. bool state{false};
  12. float brightness{1.0f};
  13. float red{1.0f};
  14. float green{1.0f};
  15. float blue{1.0f};
  16. float white{1.0f};
  17. float color_temp{1.0f};
  18. uint32_t effect{0};
  19. };
  20. /**
  21. * A custom LightState class for the Xiaomi Bedside Lamp 2.
  22. *
  23. * It is used by the DiscoAction to apply immediate light output
  24. * updates, without saving or publishing the new state.
  25. */
  26. class XiaomiBslamp2LightState : public light::LightState, public LightStateDiscoSupport {
  27. public:
  28. XiaomiBslamp2LightState(XiaomiBslamp2LightOutput *output) : light::LightState(output) { }
  29. void disco_stop() {
  30. MyLightStateRTCState recovered{};
  31. if (this->rtc_.load(&recovered)) {
  32. auto call = make_disco_call(true);
  33. call.set_state(recovered.state);
  34. call.set_brightness_if_supported(recovered.brightness);
  35. call.set_red_if_supported(recovered.red);
  36. call.set_green_if_supported(recovered.green);
  37. call.set_blue_if_supported(recovered.blue);
  38. call.set_white_if_supported(recovered.white);
  39. call.set_color_temperature_if_supported(recovered.color_temp);
  40. if (recovered.effect != 0) {
  41. call.set_effect(recovered.effect);
  42. } else {
  43. call.set_transition_length_if_supported(0);
  44. }
  45. call.perform();
  46. }
  47. }
  48. void disco_apply() {
  49. this->output_->write_state(this);
  50. this->next_write_ = false;
  51. }
  52. light::LightCall make_disco_call(bool save_and_publish) {
  53. auto call = this->make_call();
  54. call.set_save(save_and_publish);
  55. call.set_publish(save_and_publish);
  56. return call;
  57. }
  58. };
  59. } // namespace bslamp2
  60. } // namespace xiaomi
  61. } // namespace esphome