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.

267 lines
9.6 KiB

3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #include "light_output.h"
  3. // What seems to be a bug in ESPHome transitioning: when turning on
  4. // the device, the brightness is scaled along with the state (which
  5. // runs from 0 to 1), but when turning off the device, the brightness
  6. // is kept the same while the state goes down from 1 to 0. As a result
  7. // when turning off the lamp with a transition time of 1s, the light
  8. // stays on for 1s and then turn itself off abruptly.
  9. //
  10. // Reported the issue + fix at:
  11. // https://github.com/esphome/esphome/pull/1643
  12. //
  13. // A work-around for this issue can be enabled using the following
  14. // define. Note that the code provides a forward-compatible fix, so
  15. // having this define active with a fixed ESPHome version should
  16. // not be a problem.
  17. #define TRANSITION_TO_OFF_BUGFIX
  18. namespace esphome {
  19. namespace yeelight {
  20. namespace bs2 {
  21. static const char *TAG = "yeelight_bs2";
  22. // Same range as supported by the original Yeelight firmware.
  23. static const int HOME_ASSISTANT_MIRED_MIN = 153;
  24. static const int HOME_ASSISTANT_MIRED_MAX = 588;
  25. class LightStateDataExposer {
  26. public:
  27. virtual bool has_active_transformer() = 0;
  28. virtual light::LightColorValues get_transformer_values() = 0;
  29. virtual light::LightColorValues get_transformer_end_values() = 0;
  30. virtual float get_transformer_progress() = 0;
  31. };
  32. class YeelightBS2LightOutput : public Component, public light::LightOutput {
  33. public:
  34. light::LightTraits get_traits() override
  35. {
  36. auto traits = light::LightTraits();
  37. traits.set_supports_rgb(true);
  38. traits.set_supports_color_temperature(true);
  39. traits.set_supports_brightness(true);
  40. traits.set_supports_rgb_white_value(false);
  41. traits.set_supports_color_interlock(true);
  42. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  43. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  44. return traits;
  45. }
  46. void set_red_output(ledc::LEDCOutput *red) {
  47. red_ = red;
  48. }
  49. void set_green_output(ledc::LEDCOutput *green) {
  50. green_ = green;
  51. }
  52. void set_blue_output(ledc::LEDCOutput *blue) {
  53. blue_ = blue;
  54. }
  55. void set_white_output(ledc::LEDCOutput *white) {
  56. white_ = white;
  57. }
  58. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  59. master1_ = master1;
  60. }
  61. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  62. master2_ = master2;
  63. }
  64. void set_light_state_data_exposer(LightStateDataExposer *exposer) {
  65. state_exposer_ = exposer;
  66. }
  67. void write_state(light::LightState *state)
  68. {
  69. // Experimental access to protected LightState data.
  70. if (state_exposer_->has_active_transformer()) {
  71. auto progress = state_exposer_->get_transformer_progress();
  72. auto s = state_exposer_->get_transformer_values();
  73. auto t = state_exposer_->get_transformer_end_values();
  74. //ESP_LOGD(TAG, "TRFRM %f vals [%f,%f,%f,%f,%f] new [%f,%f,%f,%f,%f]",
  75. // progress,
  76. // s.get_red(), s.get_green(), s.get_blue(),
  77. // s.get_brightness(), s.get_color_temperature(),
  78. // t.get_red(), t.get_green(), t.get_blue(),
  79. // t.get_brightness(), t.get_color_temperature());
  80. }
  81. auto values = state->current_values;
  82. // Power down the light when its state is 'off'.
  83. if (values.get_state() == 0)
  84. {
  85. turn_off_();
  86. #ifdef TRANSITION_TO_OFF_BUGFIX
  87. previous_state_ = -1;
  88. previous_brightness_ = 0;
  89. #endif
  90. return;
  91. }
  92. auto brightness = values.get_brightness();
  93. #ifdef TRANSITION_TO_OFF_BUGFIX
  94. // Remember the brightness that is used when the light is fully ON.
  95. if (values.get_state() == 1) {
  96. previous_brightness_ = brightness;
  97. }
  98. // When transitioning towards zero brightness ...
  99. else if (values.get_state() < previous_state_) {
  100. // ... check if the prevous brightness is the same as the current
  101. // brightness. If yes, then the brightness isn't being scaled ...
  102. if (previous_brightness_ == brightness) {
  103. // ... and we need to do that ourselves.
  104. brightness = values.get_state() * brightness;
  105. }
  106. }
  107. previous_state_ = values.get_state();
  108. #endif
  109. // At the lowest brightness setting, switch to night light mode.
  110. // In the Yeelight integration in Home Assistant, this feature is
  111. // exposed trough a separate switch. I have found that the switch
  112. // is both confusing and made me run into issues when automating
  113. // the lights.
  114. // I don't simply check for a brightness at or below 0.01 (1%),
  115. // because the lowest brightness setting from Home Assistant
  116. // turns up as 0.011765 in here (which is 3/255).
  117. if (brightness < 0.012f && values.get_state() == 1) {
  118. turn_on_in_night_light_mode_();
  119. return;
  120. }
  121. // Leave it to the default tooling to figure out the basics.
  122. // Because of color interlocking, there are two possible outcomes:
  123. // - red, green, blue zero -> white light color temperature mode
  124. // - cwhite, wwhite zero -> RGB mode
  125. float red, green, blue, cwhite, wwhite;
  126. state->current_values_as_rgbww(
  127. &red, &green, &blue, &cwhite, &wwhite, true, false);
  128. if (cwhite > 0 || wwhite > 0) {
  129. turn_on_in_white_mode_(
  130. values.get_color_temperature(), brightness);
  131. }
  132. else {
  133. turn_on_in_rgb_mode_(
  134. values.get_red(), values.get_green(), values.get_blue(),
  135. brightness, values.get_state());
  136. }
  137. }
  138. protected:
  139. ledc::LEDCOutput *red_;
  140. ledc::LEDCOutput *green_;
  141. ledc::LEDCOutput *blue_;
  142. ledc::LEDCOutput *white_;
  143. esphome::gpio::GPIOBinaryOutput *master1_;
  144. esphome::gpio::GPIOBinaryOutput *master2_;
  145. LightStateDataExposer *state_exposer_;
  146. ColorWhiteLight white_light_;
  147. ColorRGBLight rgb_light_;
  148. ColorNightLight night_light_;
  149. #ifdef TRANSITION_TO_OFF_BUGFIX
  150. float previous_state_ = 1;
  151. float previous_brightness_ = -1;
  152. #endif
  153. void turn_off_()
  154. {
  155. red_->set_level(1);
  156. green_->set_level(1);
  157. blue_->set_level(1);
  158. white_->set_level(0);
  159. master2_->turn_off();
  160. master1_->turn_off();
  161. }
  162. void turn_on_in_night_light_mode_()
  163. {
  164. ESP_LOGD(TAG, "Activate Night light feature");
  165. night_light_.set_color(1, 1, 1, 0.01, 1);
  166. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", night_light_.red, night_light_.green, night_light_.blue, night_light_.white);
  167. // Drive the LEDs.
  168. master2_->turn_on();
  169. master1_->turn_on();
  170. red_->set_level(night_light_.red);
  171. green_->set_level(night_light_.green);
  172. blue_->set_level(night_light_.blue);
  173. white_->set_level(night_light_.white);
  174. }
  175. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  176. {
  177. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  178. rgb_light_.set_color(red, green, blue, brightness, state);
  179. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", rgb_light_.red, rgb_light_.green, rgb_light_.blue);
  180. // Drive the LEDs.
  181. master2_->turn_on();
  182. master1_->turn_on();
  183. red_->set_level(rgb_light_.red);
  184. green_->set_level(rgb_light_.green);
  185. blue_->set_level(rgb_light_.blue);
  186. white_->turn_off();
  187. }
  188. void turn_on_in_white_mode_(float temperature, float brightness)
  189. {
  190. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  191. temperature, brightness);
  192. white_light_.set_color(temperature, brightness);
  193. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  194. white_light_.red, white_light_.green, white_light_.blue,
  195. white_light_.white);
  196. master2_->turn_on();
  197. master1_->turn_on();
  198. red_->set_level(white_light_.red);
  199. green_->set_level(white_light_.green);
  200. blue_->set_level(white_light_.blue);
  201. white_->set_level(white_light_.white);
  202. }
  203. };
  204. class YeelightBS2LightState : public light::LightState, public LightStateDataExposer
  205. {
  206. public:
  207. YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) {
  208. output->set_light_state_data_exposer(this);
  209. }
  210. bool has_active_transformer() {
  211. return this->transformer_ != nullptr;
  212. }
  213. light::LightColorValues get_transformer_values() {
  214. return this->transformer_->get_values();
  215. }
  216. light::LightColorValues get_transformer_end_values() {
  217. return this->transformer_->get_end_values();
  218. }
  219. float get_transformer_progress() {
  220. return this->transformer_->get_progress();
  221. }
  222. };
  223. } // namespace bs2
  224. } // namespace yeelight
  225. } // namespace esphome