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.

215 lines
7.6 KiB

3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/components/ledc/ledc_output.h"
  4. #include "esphome/components/light/light_output.h"
  5. #include "esphome/components/gpio/output/gpio_binary_output.h"
  6. // What seems to be a bug in ESPHome transitioning: when turning on
  7. // the device, the brightness is scaled along with the state (which
  8. // runs from 0 to 1), but when turning off the device, the brightness
  9. // is kept the same while the state goes down from 1 to 0. As a result
  10. // when turning off the lamp with a transition time of 1s, the light
  11. // stays on for 1s and then turn itself off abruptly.
  12. //
  13. // Reported the issue + fix at:
  14. // https://github.com/esphome/esphome/pull/1643
  15. //
  16. // A work-around for this issue can be enabled using the following
  17. // define. Note that the code provides a forward-compatible fix, so
  18. // having this define active with a fixed ESPHome version should
  19. // not be a problem.
  20. #define TRANSITION_TO_OFF_BUGFIX
  21. namespace esphome {
  22. namespace rgbww {
  23. static const char *TAG = "yeelight_bs2.light";
  24. // Same range as supported by the original Yeelight firmware.
  25. static const int HOME_ASSISTANT_MIRED_MIN = 153;
  26. static const int HOME_ASSISTANT_MIRED_MAX = 588;
  27. class YeelightBS2LightOutput : public Component, public light::LightOutput
  28. {
  29. public:
  30. light::LightTraits get_traits() override
  31. {
  32. auto traits = light::LightTraits();
  33. traits.set_supports_rgb(true);
  34. traits.set_supports_color_temperature(true);
  35. traits.set_supports_brightness(true);
  36. traits.set_supports_rgb_white_value(false);
  37. traits.set_supports_color_interlock(true);
  38. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  39. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  40. return traits;
  41. }
  42. void set_red_output(ledc::LEDCOutput *red) {
  43. red_ = red;
  44. }
  45. void set_green_output(ledc::LEDCOutput *green) {
  46. green_ = green;
  47. }
  48. void set_blue_output(ledc::LEDCOutput *blue) {
  49. blue_ = blue;
  50. }
  51. void set_white_output(ledc::LEDCOutput *white) {
  52. white_ = white;
  53. }
  54. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  55. master1_ = master1;
  56. }
  57. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  58. master2_ = master2;
  59. }
  60. void write_state(light::LightState *state) override
  61. {
  62. auto values = state->current_values;
  63. ESP_LOGD(TAG, "write_state: STATE %f, RGB %f %f %f, BRI %f, TEMP %f",
  64. values.get_state(),
  65. values.get_red(), values.get_green(), values.get_blue(),
  66. values.get_brightness(), values.get_color_temperature());
  67. // Power down the light when its state is 'off'.
  68. if (values.get_state() == 0)
  69. {
  70. turn_off_();
  71. #ifdef TRANSITION_TO_OFF_BUGFIX
  72. previous_state_ = -1;
  73. previous_brightness_ = 0;
  74. #endif
  75. return;
  76. }
  77. auto brightness = values.get_brightness();
  78. #ifdef TRANSITION_TO_OFF_BUGFIX
  79. // Remember the brightness that is used when the light is fully ON.
  80. if (values.get_state() == 1) {
  81. previous_brightness_ = brightness;
  82. }
  83. // When transitioning towards zero brightness ...
  84. else if (values.get_state() < previous_state_) {
  85. // ... check if the prevous brightness is the same as the current
  86. // brightness. If yes, then the brightness isn't being scaled ...
  87. if (previous_brightness_ == brightness) {
  88. // ... and we need to do that ourselves.
  89. brightness = values.get_state() * brightness;
  90. }
  91. }
  92. previous_state_ = values.get_state();
  93. #endif
  94. // Leave it to the default tooling to figure out the basics.
  95. // Because of color interlocking, there are two possible outcomes:
  96. // - red, green, blue zero -> white light color temperature mode
  97. // - cwhite, wwhite zero -> RGB mode
  98. float red, green, blue, cwhite, wwhite;
  99. state->current_values_as_rgbww(
  100. &red, &green, &blue, &cwhite, &wwhite, true, false);
  101. if (cwhite > 0 || wwhite > 0) {
  102. turn_on_in_white_mode_(
  103. values.get_color_temperature(), brightness);
  104. }
  105. else if (brightness < 0.012f) {
  106. turn_on_in_night_light_mode_();
  107. }
  108. else {
  109. turn_on_in_rgb_mode_(
  110. values.get_red(), values.get_green(), values.get_blue(),
  111. brightness, values.get_state());
  112. }
  113. }
  114. protected:
  115. ledc::LEDCOutput *red_;
  116. ledc::LEDCOutput *green_;
  117. ledc::LEDCOutput *blue_;
  118. ledc::LEDCOutput *white_;
  119. esphome::gpio::GPIOBinaryOutput *master1_;
  120. esphome::gpio::GPIOBinaryOutput *master2_;
  121. esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
  122. esphome::rgbww::yeelight_bs2::RGBLight rgb_light_;
  123. esphome::rgbww::yeelight_bs2::NightLight night_light_;
  124. #ifdef TRANSITION_TO_OFF_BUGFIX
  125. float previous_state_ = 1;
  126. float previous_brightness_ = -1;
  127. #endif
  128. void turn_off_()
  129. {
  130. red_->set_level(1);
  131. green_->set_level(1);
  132. blue_->set_level(1);
  133. white_->set_level(0);
  134. master2_->turn_off();
  135. master1_->turn_off();
  136. }
  137. void turn_on_in_night_light_mode_()
  138. {
  139. ESP_LOGD(TAG, "Activate Night light feature");
  140. night_light_.set_color(1, 1, 1, 0.01, 1);
  141. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  142. // Drive the LEDs.
  143. master2_->turn_on();
  144. master1_->turn_on();
  145. red_->set_level(night_light_.red);
  146. green_->set_level(night_light_.green);
  147. blue_->set_level(night_light_.blue);
  148. white_->set_level(night_light_.white);
  149. }
  150. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  151. {
  152. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  153. rgb_light_.set_color(red, green, blue, brightness, state);
  154. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  155. // Drive the LEDs.
  156. master2_->turn_on();
  157. master1_->turn_on();
  158. red_->set_level(rgb_light_.red);
  159. green_->set_level(rgb_light_.green);
  160. blue_->set_level(rgb_light_.blue);
  161. white_->turn_off();
  162. }
  163. void turn_on_in_white_mode_(float temperature, float brightness)
  164. {
  165. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  166. temperature, brightness);
  167. white_light_.set_color(temperature, brightness);
  168. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  169. white_light_.red, white_light_.green, white_light_.blue,
  170. white_light_.white);
  171. master2_->turn_on();
  172. master1_->turn_on();
  173. red_->set_level(white_light_.red);
  174. green_->set_level(white_light_.green);
  175. blue_->set_level(white_light_.blue);
  176. white_->set_level(white_light_.white);
  177. }
  178. };
  179. } // namespace rgbww
  180. } // namespace esphome