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.

223 lines
8.0 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 the color interlocking, there are two possible outcomes:
  96. // - red, green, blue zero -> the light is in color temperature mode
  97. // - cwhite, wwhite zero -> the light is in RGB mode
  98. float red, green, blue, cwhite, wwhite;
  99. state->current_values_as_rgbww(&red, &green, &blue, &cwhite, &wwhite, true, false);
  100. if (cwhite > 0 || wwhite > 0)
  101. {
  102. turn_on_in_white_mode_(values.get_color_temperature(), brightness);
  103. }
  104. else if (
  105. values.get_red() == 1 &&
  106. values.get_green() == 1 &&
  107. values.get_blue() == 1 &&
  108. brightness < 0.012f) {
  109. turn_on_in_night_light_mode_();
  110. }
  111. else
  112. {
  113. // The RGB mode does not use the RGB values as determined by
  114. // current_values_as_rgbww(). The device has LED driving circuitry
  115. // that takes care of the required brightness curve while ramping up
  116. // the brightness. Therefore, the actual RGB values are passed here.
  117. turn_on_in_rgb_mode_(
  118. values.get_red(), values.get_green(), values.get_blue(),
  119. brightness, values.get_state());
  120. }
  121. }
  122. protected:
  123. ledc::LEDCOutput *red_;
  124. ledc::LEDCOutput *green_;
  125. ledc::LEDCOutput *blue_;
  126. ledc::LEDCOutput *white_;
  127. esphome::gpio::GPIOBinaryOutput *master1_;
  128. esphome::gpio::GPIOBinaryOutput *master2_;
  129. esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
  130. esphome::rgbww::yeelight_bs2::RGBLight rgb_light_;
  131. esphome::rgbww::yeelight_bs2::NightLight night_light_;
  132. #ifdef TRANSITION_TO_OFF_BUGFIX
  133. float previous_state_ = 1;
  134. float previous_brightness_ = -1;
  135. #endif
  136. void turn_off_()
  137. {
  138. red_->set_level(1);
  139. green_->set_level(1);
  140. blue_->set_level(1);
  141. white_->set_level(0);
  142. master2_->turn_off();
  143. master1_->turn_off();
  144. }
  145. void turn_on_in_night_light_mode_()
  146. {
  147. ESP_LOGD(TAG, "Activate Night light feature");
  148. night_light_.set_color(1, 1, 1, 0.01, 1);
  149. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  150. // Drive the LEDs.
  151. master2_->turn_on();
  152. master1_->turn_on();
  153. red_->set_level(night_light_.red);
  154. green_->set_level(night_light_.green);
  155. blue_->set_level(night_light_.blue);
  156. white_->set_level(night_light_.white);
  157. }
  158. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  159. {
  160. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  161. rgb_light_.set_color(red, green, blue, brightness, state);
  162. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  163. // Drive the LEDs.
  164. master2_->turn_on();
  165. master1_->turn_on();
  166. red_->set_level(rgb_light_.red);
  167. green_->set_level(rgb_light_.green);
  168. blue_->set_level(rgb_light_.blue);
  169. white_->turn_off();
  170. }
  171. void turn_on_in_white_mode_(float temperature, float brightness)
  172. {
  173. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  174. temperature, brightness);
  175. white_light_.set_color(temperature, brightness);
  176. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  177. white_light_.red, white_light_.green, white_light_.blue,
  178. white_light_.white);
  179. master2_->turn_on();
  180. master1_->turn_on();
  181. red_->set_level(white_light_.red);
  182. green_->set_level(white_light_.green);
  183. blue_->set_level(white_light_.blue);
  184. white_->set_level(white_light_.white);
  185. }
  186. };
  187. } // namespace rgbww
  188. } // namespace esphome