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.

173 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", active_group_->name.c_str());
  83. return;
  84. }
  85. ESP_LOGW(TAG, "activate_next_group(): activating %s/%s",
  86. active_group_->name.c_str(),
  87. active_group_->active_preset->name.c_str());
  88. active_group_->active_preset->apply();
  89. }
  90. void activate_next_preset() {
  91. if (active_group_ == nullptr) {
  92. ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined");
  93. return;
  94. }
  95. auto p = active_group_->active_preset;
  96. if (p == nullptr) {
  97. ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s", active_group_->name.c_str());
  98. return;
  99. }
  100. active_group_->active_preset = p->next_preset == nullptr
  101. ? active_group_->first_preset : p->next_preset;
  102. ESP_LOGW(TAG, "activate_next_preset(): activating %s/%s",
  103. active_group_->name.c_str(),
  104. active_group_->active_preset->name.c_str());
  105. active_group_->active_preset->apply();
  106. }
  107. void activate_group(std::string g_name) {
  108. ESP_LOGI(TAG, "Activate group %s", g_name.c_str());
  109. }
  110. void activate_preset(std::string g_name, std::string p_name) {
  111. ESP_LOGI(TAG, "Activate preset %s/%s", g_name.c_str(), p_name.c_str());
  112. }
  113. protected:
  114. light::LightState *_light;
  115. PresetGroup *first_group_ = nullptr;
  116. PresetGroup *last_group_ = nullptr;
  117. PresetGroup *active_group_ = nullptr;
  118. PresetGroup *make_preset_group_(std::string g_name) {
  119. auto g = get_group_(g_name);
  120. if (g == nullptr) {
  121. g = new PresetGroup(g_name);
  122. if (first_group_ == nullptr) {
  123. first_group_ = last_group_ = active_group_ = g;
  124. } else {
  125. last_group_->next_group = g;
  126. last_group_ = g;
  127. }
  128. }
  129. return g;
  130. }
  131. PresetGroup *get_group_(std::string g_name) {
  132. for (auto g = first_group_; g != nullptr; g = g->next_group) {
  133. if (g->name == g_name) {
  134. return g;
  135. }
  136. }
  137. return nullptr;
  138. }
  139. light::LightCall *make_preset_call_(std::string g_name, std::string p_name) {
  140. auto g = make_preset_group_(g_name);
  141. auto p = g->make_preset(p_name);
  142. auto c = new light::LightCall(_light);
  143. p->set_light_call(c);
  144. return c;
  145. }
  146. };
  147. } // namespace bslamp2
  148. } // namespace xiaomi
  149. } // namespace esphome