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.

175 lines
5.1 KiB

  1. #pragma once
  2. #include "../common.h"
  3. namespace esphome {
  4. namespace xiaomi {
  5. namespace bslamp2 {
  6. class Preset {
  7. public:
  8. std::string name;
  9. Preset *next_preset = nullptr;
  10. light::LightCall *light_call;
  11. explicit Preset(std::string name) : name(name) {}
  12. void set_light_call(light::LightCall *light_call) {
  13. this->light_call = light_call;
  14. }
  15. void apply() {
  16. light_call->perform();
  17. }
  18. };
  19. class PresetGroup {
  20. public:
  21. std::string name;
  22. PresetGroup *next_group = nullptr;
  23. Preset *first_preset = nullptr;
  24. Preset *last_preset = nullptr;
  25. Preset *active_preset = nullptr;
  26. explicit PresetGroup(std::string g_name) : name(g_name) {}
  27. Preset *make_preset(std::string p_name) {
  28. auto p = get_preset(p_name);
  29. if (p == nullptr) {
  30. p = new Preset(p_name);
  31. if (first_preset == nullptr) {
  32. first_preset = last_preset = active_preset = p;
  33. } else {
  34. last_preset->next_preset = p;
  35. last_preset = p;
  36. }
  37. }
  38. return p;
  39. }
  40. Preset *get_preset(std::string p_name) {
  41. for (auto p = first_preset; p != nullptr; p = p->next_preset) {
  42. if (p->name == p_name) {
  43. return p;
  44. }
  45. }
  46. return nullptr;
  47. }
  48. };
  49. class PresetsContainer : public Component {
  50. public:
  51. explicit PresetsContainer(light::LightState *light) : _light(light) { }
  52. void dump_config() {
  53. if (first_group_ == nullptr) {
  54. return;
  55. }
  56. ESP_LOGCONFIG(TAG, "Light Presets:");
  57. for (auto g = first_group_; g != nullptr; g = g->next_group) {
  58. ESP_LOGCONFIG(TAG, " Preset group: %s", g->name.c_str());
  59. for (auto p = g->first_preset; p != nullptr; p = p->next_preset) {
  60. ESP_LOGCONFIG(TAG, " Preset: %s", p->name.c_str());
  61. }
  62. }
  63. }
  64. void add(std::string g_name, std::string p_name, float r, float g, float b) {
  65. auto call = make_preset_call_(g_name, p_name);
  66. call->set_red(r);
  67. call->set_green(g);
  68. call->set_blue(b);
  69. }
  70. void add(std::string g_name, std::string p_name, float t) {
  71. auto call = make_preset_call_(g_name, p_name);
  72. call->set_color_temperature(t);
  73. }
  74. void activate_next_group() {
  75. if (active_group_ == nullptr) {
  76. ESP_LOGW(TAG, "activate_next_group(): no preset groups defined");
  77. return;
  78. }
  79. active_group_ = active_group_->next_group == nullptr
  80. ? first_group_ : active_group_->next_group;
  81. if (active_group_->active_preset == nullptr) {
  82. ESP_LOGW(TAG, "activate_next_group(): no presets defined for group %s",
  83. active_group_->name.c_str());
  84. return;
  85. }
  86. ESP_LOGW(TAG, "activate_next_group(): activating %s/%s",
  87. active_group_->name.c_str(),
  88. active_group_->active_preset->name.c_str());
  89. active_group_->active_preset->apply();
  90. }
  91. void activate_next_preset() {
  92. if (active_group_ == nullptr) {
  93. ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined");
  94. return;
  95. }
  96. auto p = active_group_->active_preset;
  97. if (p == nullptr) {
  98. ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s",
  99. active_group_->name.c_str());
  100. return;
  101. }
  102. active_group_->active_preset = p->next_preset == nullptr
  103. ? active_group_->first_preset : p->next_preset;
  104. ESP_LOGW(TAG, "activate_next_preset(): activating %s/%s",
  105. active_group_->name.c_str(),
  106. active_group_->active_preset->name.c_str());
  107. active_group_->active_preset->apply();
  108. }
  109. void activate_group(std::string g_name) {
  110. ESP_LOGI(TAG, "Activate group %s", g_name.c_str());
  111. }
  112. void activate_preset(std::string g_name, std::string p_name) {
  113. ESP_LOGI(TAG, "Activate preset %s/%s", g_name.c_str(), p_name.c_str());
  114. }
  115. protected:
  116. light::LightState *_light;
  117. PresetGroup *first_group_ = nullptr;
  118. PresetGroup *last_group_ = nullptr;
  119. PresetGroup *active_group_ = nullptr;
  120. PresetGroup *make_preset_group_(std::string g_name) {
  121. auto g = get_group_(g_name);
  122. if (g == nullptr) {
  123. g = new PresetGroup(g_name);
  124. if (first_group_ == nullptr) {
  125. first_group_ = last_group_ = active_group_ = g;
  126. } else {
  127. last_group_->next_group = g;
  128. last_group_ = g;
  129. }
  130. }
  131. return g;
  132. }
  133. PresetGroup *get_group_(std::string g_name) {
  134. for (auto g = first_group_; g != nullptr; g = g->next_group) {
  135. if (g->name == g_name) {
  136. return g;
  137. }
  138. }
  139. return nullptr;
  140. }
  141. light::LightCall *make_preset_call_(std::string g_name, std::string p_name) {
  142. auto g = make_preset_group_(g_name);
  143. auto p = g->make_preset(p_name);
  144. auto c = new light::LightCall(_light);
  145. p->set_light_call(c);
  146. return c;
  147. }
  148. };
  149. } // namespace bslamp2
  150. } // namespace xiaomi
  151. } // namespace esphome