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.

220 lines
7.9 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. // Power down the light when its state is 'off'.
  64. if (values.get_state() == 0)
  65. {
  66. turn_off_();
  67. #ifdef TRANSITION_TO_OFF_BUGFIX
  68. previous_state_ = -1;
  69. previous_brightness_ = 0;
  70. #endif
  71. return;
  72. }
  73. auto brightness = values.get_brightness();
  74. #ifdef TRANSITION_TO_OFF_BUGFIX
  75. // Remember the brightness that is used when the light is fully ON.
  76. if (values.get_state() == 1) {
  77. previous_brightness_ = brightness;
  78. }
  79. // When transitioning towards zero brightness ...
  80. else if (values.get_state() < previous_state_) {
  81. // ... check if the prevous brightness is the same as the current
  82. // brightness. If yes, then the brightness isn't being scaled ...
  83. if (previous_brightness_ == brightness) {
  84. // ... and we need to do that ourselves.
  85. brightness = values.get_state() * brightness;
  86. }
  87. }
  88. previous_state_ = values.get_state();
  89. #endif
  90. // At the lowest brightness setting, switch to night light mode.
  91. // In the Yeelight integration in Home Assistant, this feature is
  92. // exposed trough a separate switch. I have found that the switch
  93. // is both confusing and made me run into issues when automating
  94. // the lights.
  95. // I don't simply check for a brightness at or below 0.01 (1%),
  96. // because the lowest brightness setting from Home Assistant
  97. // turns up as 0.011765 in here (which is 3/255).
  98. if (brightness < 0.012f) {
  99. turn_on_in_night_light_mode_();
  100. return;
  101. }
  102. // Leave it to the default tooling to figure out the basics.
  103. // Because of color interlocking, there are two possible outcomes:
  104. // - red, green, blue zero -> white light color temperature mode
  105. // - cwhite, wwhite zero -> RGB mode
  106. float red, green, blue, cwhite, wwhite;
  107. state->current_values_as_rgbww(
  108. &red, &green, &blue, &cwhite, &wwhite, true, false);
  109. if (cwhite > 0 || wwhite > 0) {
  110. turn_on_in_white_mode_(
  111. values.get_color_temperature(), brightness);
  112. }
  113. else {
  114. turn_on_in_rgb_mode_(
  115. values.get_red(), values.get_green(), values.get_blue(),
  116. brightness, values.get_state());
  117. }
  118. }
  119. protected:
  120. ledc::LEDCOutput *red_;
  121. ledc::LEDCOutput *green_;
  122. ledc::LEDCOutput *blue_;
  123. ledc::LEDCOutput *white_;
  124. esphome::gpio::GPIOBinaryOutput *master1_;
  125. esphome::gpio::GPIOBinaryOutput *master2_;
  126. esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
  127. esphome::rgbww::yeelight_bs2::RGBLight rgb_light_;
  128. esphome::rgbww::yeelight_bs2::NightLight night_light_;
  129. #ifdef TRANSITION_TO_OFF_BUGFIX
  130. float previous_state_ = 1;
  131. float previous_brightness_ = -1;
  132. #endif
  133. void turn_off_()
  134. {
  135. red_->set_level(1);
  136. green_->set_level(1);
  137. blue_->set_level(1);
  138. white_->set_level(0);
  139. master2_->turn_off();
  140. master1_->turn_off();
  141. }
  142. void turn_on_in_night_light_mode_()
  143. {
  144. ESP_LOGD(TAG, "Activate Night light feature");
  145. night_light_.set_color(1, 1, 1, 0.01, 1);
  146. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  147. // Drive the LEDs.
  148. master2_->turn_on();
  149. master1_->turn_on();
  150. red_->set_level(night_light_.red);
  151. green_->set_level(night_light_.green);
  152. blue_->set_level(night_light_.blue);
  153. white_->set_level(night_light_.white);
  154. }
  155. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  156. {
  157. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  158. rgb_light_.set_color(red, green, blue, brightness, state);
  159. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  160. // Drive the LEDs.
  161. master2_->turn_on();
  162. master1_->turn_on();
  163. red_->set_level(rgb_light_.red);
  164. green_->set_level(rgb_light_.green);
  165. blue_->set_level(rgb_light_.blue);
  166. white_->turn_off();
  167. }
  168. void turn_on_in_white_mode_(float temperature, float brightness)
  169. {
  170. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  171. temperature, brightness);
  172. white_light_.set_color(temperature, brightness);
  173. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  174. white_light_.red, white_light_.green, white_light_.blue,
  175. white_light_.white);
  176. master2_->turn_on();
  177. master1_->turn_on();
  178. red_->set_level(white_light_.red);
  179. green_->set_level(white_light_.green);
  180. blue_->set_level(white_light_.blue);
  181. white_->set_level(white_light_.white);
  182. }
  183. };
  184. } // namespace rgbww
  185. } // namespace esphome