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.

236 lines
8.7 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. // The PWM frequencies as used by the original device
  28. // for driving the LED circuitry.
  29. const float RGB_PWM_FREQUENCY = 3000.0f;
  30. // I measured 10kHz for this channel, but making this 10000.0f results
  31. // in the blue channel failing. So possibly this is the actual
  32. // frequency to use (it's the frequency that provides a 13 bit
  33. // bith depth to the PWM channel).
  34. const float WHITE_PWM_FREQUENCY = 9765.0f;
  35. class YeelightBS2LightOutput : public Component, public light::LightOutput
  36. {
  37. public:
  38. light::LightTraits get_traits() override
  39. {
  40. auto traits = light::LightTraits();
  41. traits.set_supports_rgb(true);
  42. traits.set_supports_color_temperature(true);
  43. traits.set_supports_brightness(true);
  44. traits.set_supports_rgb_white_value(false);
  45. traits.set_supports_color_interlock(true);
  46. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  47. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  48. return traits;
  49. }
  50. void set_red_output(ledc::LEDCOutput *red) {
  51. red_ = red;
  52. red_->set_frequency(RGB_PWM_FREQUENCY);
  53. }
  54. void set_green_output(ledc::LEDCOutput *green) {
  55. green_ = green;
  56. green_->set_frequency(RGB_PWM_FREQUENCY);
  57. }
  58. void set_blue_output(ledc::LEDCOutput *blue) {
  59. blue_ = blue;
  60. blue_->set_frequency(RGB_PWM_FREQUENCY);
  61. }
  62. void set_white_output(ledc::LEDCOutput *white) {
  63. white_ = white;
  64. white_->set_frequency(WHITE_PWM_FREQUENCY);
  65. }
  66. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  67. master1_ = master1;
  68. }
  69. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  70. master2_ = master2;
  71. }
  72. void write_state(light::LightState *state) override
  73. {
  74. auto values = state->current_values;
  75. ESP_LOGD(TAG, "write_state: STATE %f, RGB %f %f %f, BRI %f, TEMP %f",
  76. values.get_state(),
  77. values.get_red(), values.get_green(), values.get_blue(),
  78. values.get_brightness(), values.get_color_temperature());
  79. // Power down the light when its state is 'off'.
  80. if (values.get_state() == 0)
  81. {
  82. turn_off_();
  83. #ifdef TRANSITION_TO_OFF_BUGFIX
  84. previous_state_ = -1;
  85. previous_brightness_ = 0;
  86. #endif
  87. return;
  88. }
  89. auto brightness = values.get_brightness();
  90. #ifdef TRANSITION_TO_OFF_BUGFIX
  91. // Remember the brightness that is used when the light is fully ON.
  92. if (values.get_state() == 1) {
  93. previous_brightness_ = brightness;
  94. }
  95. // When transitioning towards zero brightness ...
  96. else if (values.get_state() < previous_state_) {
  97. // ... check if the prevous brightness is the same as the current
  98. // brightness. If yes, then the brightness isn't being scaled ...
  99. if (previous_brightness_ == brightness) {
  100. // ... and we need to do that ourselves.
  101. brightness = values.get_state() * brightness;
  102. }
  103. }
  104. previous_state_ = values.get_state();
  105. #endif
  106. // Leave it to the default tooling to figure out the basics.
  107. // Because of the color interlocking, there are two possible outcomes:
  108. // - red, green, blue zero -> the light is in color temperature mode
  109. // - cwhite, wwhite zero -> the light is in RGB mode
  110. float red, green, blue, cwhite, wwhite;
  111. state->current_values_as_rgbww(&red, &green, &blue, &cwhite, &wwhite, true, false);
  112. if (cwhite > 0 || wwhite > 0)
  113. {
  114. turn_on_in_white_mode_(values.get_color_temperature(), brightness);
  115. }
  116. else if (
  117. values.get_red() == 1 &&
  118. values.get_green() == 1 &&
  119. values.get_blue() == 1 &&
  120. brightness < 0.012f) {
  121. turn_on_in_night_light_mode_();
  122. }
  123. else
  124. {
  125. // The RGB mode does not use the RGB values as determined by
  126. // current_values_as_rgbww(). The device has LED driving circuitry
  127. // that takes care of the required brightness curve while ramping up
  128. // the brightness. Therefore, the actual RGB values are passed here.
  129. turn_on_in_rgb_mode_(
  130. values.get_red(), values.get_green(), values.get_blue(),
  131. brightness, values.get_state());
  132. }
  133. }
  134. protected:
  135. ledc::LEDCOutput *red_;
  136. ledc::LEDCOutput *green_;
  137. ledc::LEDCOutput *blue_;
  138. ledc::LEDCOutput *white_;
  139. esphome::gpio::GPIOBinaryOutput *master1_;
  140. esphome::gpio::GPIOBinaryOutput *master2_;
  141. esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
  142. esphome::rgbww::yeelight_bs2::RGBLight rgb_light_;
  143. esphome::rgbww::yeelight_bs2::NightLight night_light_;
  144. #ifdef TRANSITION_TO_OFF_BUGFIX
  145. float previous_state_ = 1;
  146. float previous_brightness_ = -1;
  147. #endif
  148. void turn_off_()
  149. {
  150. red_->set_level(1);
  151. green_->set_level(1);
  152. blue_->set_level(1);
  153. white_->set_level(0);
  154. master2_->turn_off();
  155. master1_->turn_off();
  156. }
  157. void turn_on_in_night_light_mode_()
  158. {
  159. ESP_LOGD(TAG, "Activate Night light feature");
  160. night_light_.set_color(1, 1, 1, 0.01, 1);
  161. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  162. // Drive the LEDs.
  163. master2_->turn_on();
  164. master1_->turn_on();
  165. red_->set_level(night_light_.red);
  166. green_->set_level(night_light_.green);
  167. blue_->set_level(night_light_.blue);
  168. white_->set_level(night_light_.white);
  169. }
  170. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  171. {
  172. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  173. rgb_light_.set_color(red, green, blue, brightness, state);
  174. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  175. // Drive the LEDs.
  176. master2_->turn_on();
  177. master1_->turn_on();
  178. red_->set_level(rgb_light_.red);
  179. green_->set_level(rgb_light_.green);
  180. blue_->set_level(rgb_light_.blue);
  181. white_->set_level(rgb_light_.white);
  182. }
  183. void turn_on_in_white_mode_(float temperature, float brightness)
  184. {
  185. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  186. temperature, brightness);
  187. white_light_.set_color(temperature, brightness);
  188. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  189. white_light_.red, white_light_.green, white_light_.blue,
  190. white_light_.white);
  191. master2_->turn_on();
  192. master1_->turn_on();
  193. red_->set_level(white_light_.red);
  194. green_->set_level(white_light_.green);
  195. blue_->set_level(white_light_.blue);
  196. white_->set_level(white_light_.white);
  197. }
  198. };
  199. } // namespace rgbww
  200. } // namespace esphome