From 0d90e8456becd5ea061f1dcd6b66bca64722cef7 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Sat, 17 Apr 2021 02:42:43 +0200 Subject: [PATCH] Automation actions "next preset group" and "next preset" implemented. --- doc/example.yaml | 34 +++++---- light/automation.h | 15 +++- light/presets.h | 171 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 169 insertions(+), 51 deletions(-) diff --git a/doc/example.yaml b/doc/example.yaml index 25956e4..3060f51 100644 --- a/doc/example.yaml +++ b/doc/example.yaml @@ -96,16 +96,16 @@ light: update_interval: 3s presets: - rgb: - - red_bright: { red: 1 } - - green: { green: 1 } - - blue_dimmed: { blue: 1 } - - yellow: { red: 1, green: 1 } - - something: { red: 0.2, green: 0.4, blue: 1 } + - red_bright: { red: 100% } + - green: { green: 100% } + - blue_dimmed: { blue: 100% } + - yellow: { red: 100%, green: 100% } + - purple: { red: 100%, blue: 100% } - white: - - warm: { color_temperature: 588 } - - luke: { color_temperature: 400 } - - chilly: { color_temperature: 275 } - cold: { color_temperature: 153 } + - chilly: { color_temperature: 275 } + - luke: { color_temperature: 400 } + - warm: { color_temperature: 587 } # This text sensor propagates the currently active light mode. # The possible light modes are: "off", "rgb", "white" and "night". @@ -151,13 +151,17 @@ binary_sensor: - platform: xiaomi_bslamp2 id: ${id_color_button} part: color button - on_press: - then: - - light.turn_on: - id: ${id_light} - red: !lambda return random_float(); - green: !lambda return random_float(); - blue: !lambda return random_float(); + on_multi_click: + - timing: + - ON for at most 0.6s + then: + - preset.activate: + next: preset + - timing: + - ON for at least 0.6s + then: + - preset.activate: + next: group # This sensor component publishes touch events for the front panel slider. # The published value represents the level at which the slider was touched. diff --git a/light/automation.h b/light/automation.h index 7964de3..2460798 100644 --- a/light/automation.h +++ b/light/automation.h @@ -39,8 +39,19 @@ public: void play(Ts... x) override { auto operation = this->operation_.value(x...); - auto group = this->group_.optional_value(x...); - auto preset = this->preset_.optional_value(x...); + + if (operation == "next_group") { + presets_->activate_next_group(); + } else if (operation == "next_preset") { + presets_->activate_next_preset(); + } else if (operation == "activate_group") { + auto group = this->group_.value(x...); + presets_->activate_group(group); + } else if (operation == "activate_preset") { + auto group = this->group_.value(x...); + auto preset = this->preset_.value(x...); + presets_->activate_preset(group, preset); + } } protected: diff --git a/light/presets.h b/light/presets.h index c597bdf..806914c 100644 --- a/light/presets.h +++ b/light/presets.h @@ -1,67 +1,170 @@ #pragma once -#include -#include #include "../common.h" namespace esphome { namespace xiaomi { namespace bslamp2 { -using Order = std::vector; -using Presets = std::map; -using PresetOrder = std::map; -using PresetGroups = std::map; -using PresetGroupOrder = Order; +class Preset { +public: + std::string name; + Preset *next_preset = nullptr; + light::LightCall *light_call; + + explicit Preset(std::string name) : name(name) {} + + void set_light_call(light::LightCall *light_call) { + this->light_call = light_call; + } + + void apply() { + light_call->perform(); + } +}; + +class PresetGroup { +public: + std::string name; + PresetGroup *next_group = nullptr; + Preset *first_preset = nullptr; + Preset *last_preset = nullptr; + Preset *active_preset = nullptr; + + explicit PresetGroup(std::string g_name) : name(g_name) {} + + Preset *make_preset(std::string p_name) { + auto p = get_preset(p_name); + if (p == nullptr) { + p = new Preset(p_name); + if (first_preset == nullptr) { + first_preset = last_preset = active_preset = p; + } else { + last_preset->next_preset = p; + last_preset = p; + } + } + return p; + } + + Preset *get_preset(std::string p_name) { + for (auto p = first_preset; p != nullptr; p = p->next_preset) { + if (p->name == p_name) { + return p; + } + } + return nullptr; + } +}; class PresetsContainer : public Component { public: explicit PresetsContainer(light::LightState *light) : _light(light) { } void dump_config() { - if (map_.empty()) { + if (first_group_ == nullptr) { return; } ESP_LOGCONFIG(TAG, "Light Presets:"); - for (auto group : group_order_) { - ESP_LOGCONFIG(TAG, " Preset group: %s", group.c_str()); - for (auto name : preset_order_[group]) { - ESP_LOGCONFIG(TAG, " Preset: %s", name.c_str()); + for (auto g = first_group_; g != nullptr; g = g->next_group) { + ESP_LOGCONFIG(TAG, " Preset group: %s", g->name.c_str()); + for (auto p = g->first_preset; p != nullptr; p = p->next_preset) { + ESP_LOGCONFIG(TAG, " Preset: %s", p->name.c_str()); } } } - void add(std::string group, std::string name, float red, float green, float blue) { - auto call = make_preset_slot_(group, name); - call->set_red(red); - call->set_green(green); - call->set_blue(blue); + void add(std::string g_name, std::string p_name, float r, float g, float b) { + auto call = make_preset_call_(g_name, p_name); + call->set_red(r); + call->set_green(g); + call->set_blue(b); + } + + void add(std::string g_name, std::string p_name, float t) { + auto call = make_preset_call_(g_name, p_name); + call->set_color_temperature(t); + } + + void activate_next_group() { + if (active_group_ == nullptr) { + ESP_LOGW(TAG, "activate_next_group(): no preset groups defined"); + return; + } + active_group_ = active_group_->next_group == nullptr + ? first_group_ : active_group_->next_group; + if (active_group_->active_preset == nullptr) { + ESP_LOGW(TAG, "activate_next_group(): no presets defined for group %s", active_group_->name.c_str()); + return; + } + ESP_LOGW(TAG, "activate_next_group(): activating %s/%s", + active_group_->name.c_str(), + active_group_->active_preset->name.c_str()); + active_group_->active_preset->apply(); + } + + void activate_next_preset() { + if (active_group_ == nullptr) { + ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined"); + return; + } + auto p = active_group_->active_preset; + if (p == nullptr) { + ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s", active_group_->name.c_str()); + return; + } + active_group_->active_preset = p->next_preset == nullptr + ? active_group_->first_preset : p->next_preset; + ESP_LOGW(TAG, "activate_next_preset(): activating %s/%s", + active_group_->name.c_str(), + active_group_->active_preset->name.c_str()); + active_group_->active_preset->apply(); + } + + void activate_group(std::string g_name) { + ESP_LOGI(TAG, "Activate group %s", g_name.c_str()); } - void add(std::string group, std::string name, float color_temperature) { - auto call = make_preset_slot_(group, name); - call->set_color_temperature(color_temperature); + void activate_preset(std::string g_name, std::string p_name) { + ESP_LOGI(TAG, "Activate preset %s/%s", g_name.c_str(), p_name.c_str()); } protected: light::LightState *_light; - PresetGroups map_; - PresetGroupOrder group_order_; - PresetOrder preset_order_; - - light::LightCall *make_preset_slot_(std::string group, std::string name) { - // Check if the group already exists. If not, then create it. - if (map_.find(group) == map_.end()) { - map_[group] = Presets(); - group_order_.push_back(group); - preset_order_[group] = Order(); + PresetGroup *first_group_ = nullptr; + PresetGroup *last_group_ = nullptr; + PresetGroup *active_group_ = nullptr; + + PresetGroup *make_preset_group_(std::string g_name) { + auto g = get_group_(g_name); + if (g == nullptr) { + g = new PresetGroup(g_name); + if (first_group_ == nullptr) { + first_group_ = last_group_ = active_group_ = g; + } else { + last_group_->next_group = g; + last_group_ = g; + } } - if (map_[group].find(name) == map_[group].end()) { - map_[group][name] = new light::LightCall(_light); - preset_order_[group].push_back(name); + return g; + } + + PresetGroup *get_group_(std::string g_name) { + for (auto g = first_group_; g != nullptr; g = g->next_group) { + if (g->name == g_name) { + return g; + } } + return nullptr; + } + - return map_[group][name]; + light::LightCall *make_preset_call_(std::string g_name, std::string p_name) { + auto g = make_preset_group_(g_name); + auto p = g->make_preset(p_name); + auto c = new light::LightCall(_light); + p->set_light_call(c); + return c; } };