From b28bf11c35a4a863899d9dc19fcb803e3492ca2b Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Sat, 17 Apr 2021 11:30:05 +0200 Subject: [PATCH] The activate_preset action now supports simple string values. New actions are: * `preset.activate: next_group` * `preset.activate: next_preset` * `preset.activate: ` * `preset.activate: .` --- light/__init__.py | 45 +++++++++++++++++++++++++++++++++------------ light/presets.h | 6 ++++-- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/light/__init__.py b/light/__init__.py index 6cbd38a..3d13342 100644 --- a/light/__init__.py +++ b/light/__init__.py @@ -62,21 +62,42 @@ def is_preset_group(value): def is_preset(value): return value +def maybe_simple_preset_action(schema): + def validator(value): + if isinstance(value, dict): + return schema(value) + value = value.lower() + conf = {} + if value == "next_group": + conf[CONF_NEXT] = CONF_GROUP + elif value == "next_preset": + conf[CONF_NEXT] = CONF_PRESET + elif "." not in value: + conf[CONF_GROUP] = value + else: + group, preset = value.split(".", 2) + conf[CONF_GROUP] = group + conf[CONF_PRESET] = preset + return schema(conf) + + return validator + @automation.register_action( "preset.activate", ActivatePresetAction, - cv.Schema(cv.Any( - cv.Schema({ - cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer), - cv.Required(CONF_GROUP): is_preset_group, - cv.Optional(CONF_PRESET): is_preset - }), - cv.Schema({ - cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer), - cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True) - }) - - )) + cv.Schema( + maybe_simple_preset_action(cv.Any( + cv.Schema({ + cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer), + cv.Required(CONF_GROUP): is_preset_group, + cv.Optional(CONF_PRESET): is_preset + }), + cv.Schema({ + cv.GenerateID(CONF_PRESETS_ID): cv.use_id(PresetsContainer), + cv.Required(CONF_NEXT): cv.one_of(CONF_GROUP, CONF_PRESET, lower=True) + }) + )) + ) ) def preset_activate_to_code(config, action_id, template_arg, args): presets_var = yield cg.get_variable(config[CONF_PRESETS_ID]) diff --git a/light/presets.h b/light/presets.h index 806914c..cfcf055 100644 --- a/light/presets.h +++ b/light/presets.h @@ -94,7 +94,8 @@ public: 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()); + 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", @@ -110,7 +111,8 @@ public: } 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()); + 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