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.

79 lines
2.4 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. * This class is used by the ColorTransitionHandler class to inspect if
  24. * an ongoing light color transition is active in the LightState object.
  25. *
  26. * It is also used by the DiscoAction to apply immediate light output
  27. * updates, without saving or publishing the new state.
  28. */
  29. class XiaomiBslamp2LightState : public light::LightState, public LightStateTransformerInspector, public LightStateDiscoSupport {
  30. public:
  31. XiaomiBslamp2LightState(const std::string &name, XiaomiBslamp2LightOutput *output) : light::LightState(name, output) {
  32. output->set_transformer_inspector(this);
  33. }
  34. bool is_active() { return transformer_ != nullptr; }
  35. bool is_transition() { return transformer_->is_transition(); }
  36. light::LightColorValues get_end_values() { return transformer_->get_end_values(); }
  37. float get_progress() { return transformer_->get_progress(); }
  38. void disco_stop() {
  39. MyLightStateRTCState recovered{};
  40. if (this->rtc_.load(&recovered)) {
  41. auto call = make_disco_call(true);
  42. call.set_state(recovered.state);
  43. call.set_brightness_if_supported(recovered.brightness);
  44. call.set_red_if_supported(recovered.red);
  45. call.set_green_if_supported(recovered.green);
  46. call.set_blue_if_supported(recovered.blue);
  47. call.set_white_if_supported(recovered.white);
  48. call.set_color_temperature_if_supported(recovered.color_temp);
  49. if (recovered.effect != 0) {
  50. call.set_effect(recovered.effect);
  51. } else {
  52. call.set_transition_length_if_supported(0);
  53. }
  54. call.perform();
  55. }
  56. }
  57. void disco_apply() {
  58. this->output_->write_state(this);
  59. this->next_write_ = false;
  60. }
  61. light::LightCall make_disco_call(bool save_and_publish) {
  62. auto call = this->make_call();
  63. call.set_save(save_and_publish);
  64. call.set_publish(save_and_publish);
  65. return call;
  66. }
  67. };
  68. } // namespace bslamp2
  69. } // namespace xiaomi
  70. } // namespace esphome