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.

210 lines
6.3 KiB

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