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.

213 lines
6.4 KiB

  1. #pragma once
  2. #include "../common.h"
  3. #include "esphome/core/optional.h"
  4. #include <functional>
  5. #include <vector>
  6. namespace esphome {
  7. namespace xiaomi {
  8. namespace bslamp2 {
  9. class Preset : public Component {
  10. public:
  11. std::string group_name;
  12. std::string name;
  13. Preset *next_preset = nullptr;
  14. explicit Preset(light::LightState *light, std::string group_name, std::string name):
  15. group_name(group_name), name(name), light_state_(light) {}
  16. void set_transition_length(uint32_t t) { transition_length_ = t; }
  17. void set_brightness(float t) { brightness_ = t; }
  18. void set_red(float t) { red_ = t; }
  19. void set_green(float t) { green_ = t; }
  20. void set_blue(float t) { blue_ = t; }
  21. void set_color_temperature(float t) { color_temperature_ = t; }
  22. void set_effect(const std::string &effect) { effect_ = effect; }
  23. void apply() {
  24. ESP_LOGI(TAG, "Activating light preset: %s/%s",
  25. group_name.c_str(), name.c_str());
  26. auto call = light_state_->make_call();
  27. call.set_state(true);
  28. if (transition_length_.has_value()) {
  29. call.set_transition_length(*transition_length_);
  30. }
  31. if (brightness_.has_value()) {
  32. call.set_brightness(*brightness_);
  33. }
  34. if (red_.has_value()) {
  35. call.set_red(*red_);
  36. }
  37. if (green_.has_value()) {
  38. call.set_green(*green_);
  39. }
  40. if (blue_.has_value()) {
  41. call.set_blue(*blue_);
  42. }
  43. if (color_temperature_.has_value()) {
  44. call.set_color_temperature(*color_temperature_);
  45. }
  46. if (effect_.has_value()) {
  47. call.set_effect(*effect_);
  48. }
  49. call.perform();
  50. }
  51. protected:
  52. light::LightState *light_state_;
  53. optional<uint32_t> transition_length_;
  54. optional<float> brightness_;
  55. optional<float> red_;
  56. optional<float> green_;
  57. optional<float> blue_;
  58. optional<float> color_temperature_;
  59. optional<std::string> effect_;
  60. };
  61. class PresetGroup {
  62. public:
  63. std::string name;
  64. PresetGroup *next_group = nullptr;
  65. Preset *first_preset = nullptr;
  66. Preset *last_preset = nullptr;
  67. Preset *active_preset = nullptr;
  68. explicit PresetGroup(std::string g_name) : name(g_name) {}
  69. void add_preset(Preset *p) {
  70. if (first_preset == nullptr) {
  71. first_preset = last_preset = active_preset = p;
  72. } else {
  73. last_preset->next_preset = p;
  74. last_preset = p;
  75. }
  76. }
  77. Preset *get_preset(std::string p_name) {
  78. for (auto p = first_preset; p != nullptr; p = p->next_preset) {
  79. ESP_LOGE(TAG, "CHECK '%s' vs '%s'", p_name.c_str(), p->name.c_str());
  80. if (p->name == p_name) {
  81. return p;
  82. }
  83. }
  84. return nullptr;
  85. }
  86. };
  87. class PresetsContainer : public Component {
  88. public:
  89. PresetGroup *first_group = nullptr;
  90. PresetGroup *last_group = nullptr;
  91. PresetGroup *active_group = nullptr;
  92. void dump_config() {
  93. if (first_group == nullptr) {
  94. return;
  95. }
  96. ESP_LOGCONFIG(TAG, "Light Presets:");
  97. for (auto g = first_group; g != nullptr; g = g->next_group) {
  98. ESP_LOGCONFIG(TAG, " Preset group: %s", g->name.c_str());
  99. for (auto p = g->first_preset; p != nullptr; p = p->next_preset) {
  100. ESP_LOGCONFIG(TAG, " Preset: %s", p->name.c_str());
  101. }
  102. }
  103. }
  104. void add_preset(Preset *preset) {
  105. auto g = make_preset_group_(preset->group_name);
  106. g->add_preset(preset);
  107. }
  108. PresetGroup *get_group(std::string g_name) {
  109. for (auto g = first_group; g != nullptr; g = g->next_group) {
  110. if (g->name == g_name) {
  111. return g;
  112. }
  113. }
  114. return nullptr;
  115. }
  116. void activate_next_group() {
  117. if (active_group == nullptr) {
  118. ESP_LOGW(TAG, "activate_next_group(): no preset groups defined");
  119. return;
  120. }
  121. active_group = active_group->next_group == nullptr
  122. ? first_group : active_group->next_group;
  123. if (active_group->active_preset == nullptr) {
  124. ESP_LOGW(TAG, "activate_next_group(): no presets defined for group %s",
  125. active_group->name.c_str());
  126. return;
  127. }
  128. active_group->active_preset->apply();
  129. }
  130. void activate_next_preset() {
  131. if (active_group == nullptr) {
  132. ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined");
  133. return;
  134. }
  135. auto p = active_group->active_preset;
  136. if (p == nullptr) {
  137. ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s",
  138. active_group->name.c_str());
  139. return;
  140. }
  141. active_group->active_preset = p->next_preset == nullptr
  142. ? active_group->first_preset : p->next_preset;
  143. active_group->active_preset->apply();
  144. }
  145. void activate_group(std::string g_name) {
  146. auto g = get_group(g_name);
  147. if (g == nullptr) {
  148. ESP_LOGE(TAG, "activate_group(%s): preset group does not exist",
  149. g_name.c_str());
  150. return;
  151. }
  152. auto p = g->active_preset;
  153. if (p == nullptr) {
  154. ESP_LOGW(TAG, "activate_group(%s): no presets defined for group",
  155. g_name.c_str());
  156. return;
  157. }
  158. p->apply();
  159. }
  160. void activate_preset(std::string g_name, std::string p_name) {
  161. auto g = get_group(g_name);
  162. if (g == nullptr) {
  163. ESP_LOGE(TAG, "activate_preset(%s, %s): preset group '%s' does not exist",
  164. g_name.c_str(), p_name.c_str(), g_name.c_str());
  165. return;
  166. }
  167. auto p = g->get_preset(p_name);
  168. if (p == nullptr) {
  169. ESP_LOGE(TAG, "activate_preset(%s, %s): preset '%s' does not exist in group '%s'",
  170. g_name.c_str(), p_name.c_str(), p_name.c_str(), g->name.c_str());
  171. return;
  172. }
  173. p->apply();
  174. }
  175. protected:
  176. PresetGroup *make_preset_group_(std::string g_name) {
  177. auto g = get_group(g_name);
  178. if (g == nullptr) {
  179. g = new PresetGroup(g_name);
  180. if (first_group == nullptr) {
  181. first_group = last_group = active_group = g;
  182. } else {
  183. last_group->next_group = g;
  184. last_group = g;
  185. }
  186. }
  187. return g;
  188. }
  189. };
  190. } // namespace bslamp2
  191. } // namespace xiaomi
  192. } // namespace esphome