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.

70 lines
2.0 KiB

  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include "../common.h"
  5. namespace esphome {
  6. namespace xiaomi {
  7. namespace bslamp2 {
  8. using Order = std::vector<std::string>;
  9. using Presets = std::map<std::string, light::LightCall *>;
  10. using PresetOrder = std::map<std::string, Order>;
  11. using PresetGroups = std::map<std::string, Presets>;
  12. using PresetGroupOrder = Order;
  13. class PresetsContainer : public Component {
  14. public:
  15. explicit PresetsContainer(light::LightState *light) : _light(light) { }
  16. void dump_config() {
  17. if (map_.empty()) {
  18. return;
  19. }
  20. ESP_LOGCONFIG(TAG, "Light Presets:");
  21. for (auto group : group_order_) {
  22. ESP_LOGCONFIG(TAG, " Preset group: %s", group.c_str());
  23. for (auto name : preset_order_[group]) {
  24. ESP_LOGCONFIG(TAG, " Preset: %s", name.c_str());
  25. }
  26. }
  27. }
  28. void add(std::string group, std::string name, float red, float green, float blue) {
  29. auto call = make_preset_slot_(group, name);
  30. call->set_red(red);
  31. call->set_green(green);
  32. call->set_blue(blue);
  33. }
  34. void add(std::string group, std::string name, float color_temperature) {
  35. auto call = make_preset_slot_(group, name);
  36. call->set_color_temperature(color_temperature);
  37. }
  38. protected:
  39. light::LightState *_light;
  40. PresetGroups map_;
  41. PresetGroupOrder group_order_;
  42. PresetOrder preset_order_;
  43. light::LightCall *make_preset_slot_(std::string group, std::string name) {
  44. // Check if the group already exists. If not, then create it.
  45. if (map_.find(group) == map_.end()) {
  46. map_[group] = Presets();
  47. group_order_.push_back(group);
  48. preset_order_[group] = Order();
  49. }
  50. if (map_[group].find(name) == map_[group].end()) {
  51. map_[group][name] = new light::LightCall(_light);
  52. preset_order_[group].push_back(name);
  53. }
  54. return map_[group][name];
  55. }
  56. };
  57. } // namespace bslamp2
  58. } // namespace xiaomi
  59. } // namespace esphome