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.

222 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 yeelight {
  23. namespace bs2 {
  24. static const char *TAG = "yeelight_bs2";
  25. // Same range as supported by the original Yeelight firmware.
  26. static const int HOME_ASSISTANT_MIRED_MIN = 153;
  27. static const int HOME_ASSISTANT_MIRED_MAX = 588;
  28. class YeelightBS2LightOutput : public Component, public light::LightOutput
  29. {
  30. public:
  31. light::LightTraits get_traits() override
  32. {
  33. auto traits = light::LightTraits();
  34. traits.set_supports_rgb(true);
  35. traits.set_supports_color_temperature(true);
  36. traits.set_supports_brightness(true);
  37. traits.set_supports_rgb_white_value(false);
  38. traits.set_supports_color_interlock(true);
  39. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  40. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  41. return traits;
  42. }
  43. void set_red_output(ledc::LEDCOutput *red) {
  44. red_ = red;
  45. }
  46. void set_green_output(ledc::LEDCOutput *green) {
  47. green_ = green;
  48. }
  49. void set_blue_output(ledc::LEDCOutput *blue) {
  50. blue_ = blue;
  51. }
  52. void set_white_output(ledc::LEDCOutput *white) {
  53. white_ = white;
  54. }
  55. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  56. master1_ = master1;
  57. }
  58. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  59. master2_ = master2;
  60. }
  61. void write_state(light::LightState *state) override
  62. {
  63. auto values = state->current_values;
  64. // Power down the light when its state is 'off'.
  65. if (values.get_state() == 0)
  66. {
  67. turn_off_();
  68. #ifdef TRANSITION_TO_OFF_BUGFIX
  69. previous_state_ = -1;
  70. previous_brightness_ = 0;
  71. #endif
  72. return;
  73. }
  74. auto brightness = values.get_brightness();
  75. #ifdef TRANSITION_TO_OFF_BUGFIX
  76. // Remember the brightness that is used when the light is fully ON.
  77. if (values.get_state() == 1) {
  78. previous_brightness_ = brightness;
  79. }
  80. // When transitioning towards zero brightness ...
  81. else if (values.get_state() < previous_state_) {
  82. // ... check if the prevous brightness is the same as the current
  83. // brightness. If yes, then the brightness isn't being scaled ...
  84. if (previous_brightness_ == brightness) {
  85. // ... and we need to do that ourselves.
  86. brightness = values.get_state() * brightness;
  87. }
  88. }
  89. previous_state_ = values.get_state();
  90. #endif
  91. // At the lowest brightness setting, switch to night light mode.
  92. // In the Yeelight integration in Home Assistant, this feature is
  93. // exposed trough a separate switch. I have found that the switch
  94. // is both confusing and made me run into issues when automating
  95. // the lights.
  96. // I don't simply check for a brightness at or below 0.01 (1%),
  97. // because the lowest brightness setting from Home Assistant
  98. // turns up as 0.011765 in here (which is 3/255).
  99. if (brightness < 0.012f && values.get_state() == 1) {
  100. turn_on_in_night_light_mode_();
  101. return;
  102. }
  103. // Leave it to the default tooling to figure out the basics.
  104. // Because of color interlocking, there are two possible outcomes:
  105. // - red, green, blue zero -> white light color temperature mode
  106. // - cwhite, wwhite zero -> RGB mode
  107. float red, green, blue, cwhite, wwhite;
  108. state->current_values_as_rgbww(
  109. &red, &green, &blue, &cwhite, &wwhite, true, false);
  110. if (cwhite > 0 || wwhite > 0) {
  111. turn_on_in_white_mode_(
  112. values.get_color_temperature(), brightness);
  113. }
  114. else {
  115. turn_on_in_rgb_mode_(
  116. values.get_red(), values.get_green(), values.get_blue(),
  117. brightness, values.get_state());
  118. }
  119. }
  120. protected:
  121. ledc::LEDCOutput *red_;
  122. ledc::LEDCOutput *green_;
  123. ledc::LEDCOutput *blue_;
  124. ledc::LEDCOutput *white_;
  125. esphome::gpio::GPIOBinaryOutput *master1_;
  126. esphome::gpio::GPIOBinaryOutput *master2_;
  127. ColorWhiteLight white_light_;
  128. ColorRGBLight rgb_light_;
  129. ColorNightLight night_light_;
  130. #ifdef TRANSITION_TO_OFF_BUGFIX
  131. float previous_state_ = 1;
  132. float previous_brightness_ = -1;
  133. #endif
  134. void turn_off_()
  135. {
  136. red_->set_level(1);
  137. green_->set_level(1);
  138. blue_->set_level(1);
  139. white_->set_level(0);
  140. master2_->turn_off();
  141. master1_->turn_off();
  142. }
  143. void turn_on_in_night_light_mode_()
  144. {
  145. ESP_LOGD(TAG, "Activate Night light feature");
  146. night_light_.set_color(1, 1, 1, 0.01, 1);
  147. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  148. // Drive the LEDs.
  149. master2_->turn_on();
  150. master1_->turn_on();
  151. red_->set_level(night_light_.red);
  152. green_->set_level(night_light_.green);
  153. blue_->set_level(night_light_.blue);
  154. white_->set_level(night_light_.white);
  155. }
  156. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  157. {
  158. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  159. rgb_light_.set_color(red, green, blue, brightness, state);
  160. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  161. // Drive the LEDs.
  162. master2_->turn_on();
  163. master1_->turn_on();
  164. red_->set_level(rgb_light_.red);
  165. green_->set_level(rgb_light_.green);
  166. blue_->set_level(rgb_light_.blue);
  167. white_->turn_off();
  168. }
  169. void turn_on_in_white_mode_(float temperature, float brightness)
  170. {
  171. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  172. temperature, brightness);
  173. white_light_.set_color(temperature, brightness);
  174. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  175. white_light_.red, white_light_.green, white_light_.blue,
  176. white_light_.white);
  177. master2_->turn_on();
  178. master1_->turn_on();
  179. red_->set_level(white_light_.red);
  180. green_->set_level(white_light_.green);
  181. blue_->set_level(white_light_.blue);
  182. white_->set_level(white_light_.white);
  183. }
  184. };
  185. } // namespace bs2
  186. } // namespace yeelight
  187. } // namespace esphome