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.

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