From 31ae3b51234b55f5d9b85ce0b632df94dadc72b6 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Sat, 17 Apr 2021 00:18:50 +0200 Subject: [PATCH] Added an ActivatePresetAction, which will be used to apply presets from automations. --- light/__init__.py | 56 +++++++++++++++++++++++++++++++++------------- light/automation.h | 19 ++++++++++++++++ 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/light/__init__.py b/light/__init__.py index 6581085..6cbd38a 100644 --- a/light/__init__.py +++ b/light/__init__.py @@ -16,24 +16,19 @@ CONF_MASTER2 = "master2" CONF_ON_BRIGHTNESS = "on_brightness" CONF_PRESETS_ID = "presets_id" CONF_PRESETS = "presets" +CONF_NEXT = "next" +CONF_GROUP = "group" +CONF_PRESET = "preset" -# Classes. XiaomiBslamp2LightState = bslamp2_ns.class_("XiaomiBslamp2LightState", light.LightState) XiaomiBslamp2LightOutput = bslamp2_ns.class_("XiaomiBslamp2LightOutput", light.LightOutput) PresetsContainer = bslamp2_ns.class_("PresetsContainer", cg.Component) - -# Trigger. BrightnessTrigger = bslamp2_ns.class_("BrightnessTrigger", automation.Trigger.template()) - -# Actions. -#ActivatePresetGroup = bslamp2_ns.class_("ActivatePresetGroup", automation.Action) -#ActivatePreset = bslamp2_ns.class_("ActivatePreset", automation.Action) -#NextPresetGroup = bslamp2_ns.class_("NextPresetAction", automation.Action) -#NextPreset = bslamp2_ns.class_("NextPresetAction", automation.Action) +ActivatePresetAction = bslamp2_ns.class_("ActivatePresetAction", automation.Action) PRESETS_SCHEMA = cv.Schema({ - str: cv.Schema({ - str: cv.Any( + str.lower: cv.Schema({ + str.lower: cv.Any( cv.Schema({ cv.Optional(CONF_RED, default=0): cv.percentage, cv.Optional(CONF_GREEN, default=0): cv.percentage, @@ -61,10 +56,41 @@ CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend( } ) -#automation.register_action("preset.activate_group", ActivatePresetGroup, cv.string) -#def activate_group_to_code(config, action_id, template_arg, args): -# presets_var = yield cg.get_variable(config[CONF_PRESETS_ID]) -# cg.add(presets_var.activate_group(...)); +def is_preset_group(value): + return value + +def is_preset(value): + return value + +@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) + }) + + )) +) +def preset_activate_to_code(config, action_id, template_arg, args): + presets_var = yield cg.get_variable(config[CONF_PRESETS_ID]) + action_var = cg.new_Pvariable(action_id, template_arg, presets_var) + if CONF_NEXT in config: + cg.add(action_var.set_operation(f"next_{config[CONF_NEXT]}")) + elif CONF_PRESET in config: + cg.add(action_var.set_operation("activate_preset")) + cg.add(action_var.set_group(config[CONF_GROUP])) + cg.add(action_var.set_preset(config[CONF_PRESET])) + else: + cg.add(action_var.set_operation("activate_group")) + cg.add(action_var.set_group(config[CONF_GROUP])) + yield action_var @coroutine def light_output_to_code(config): diff --git a/light/automation.h b/light/automation.h index d463dda..7964de3 100644 --- a/light/automation.h +++ b/light/automation.h @@ -4,6 +4,7 @@ #include "esphome/core/component.h" #include "esphome/core/automation.h" #include "light_output.h" +#include "presets.h" namespace esphome { namespace xiaomi { @@ -28,6 +29,24 @@ protected: float last_brightness_ = -1.0f; }; +template class ActivatePresetAction : public Action { +public: + explicit ActivatePresetAction(PresetsContainer *presets) : presets_(presets) {} + + TEMPLATABLE_VALUE(std::string, operation); + TEMPLATABLE_VALUE(std::string, group); + TEMPLATABLE_VALUE(std::string, preset); + + 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...); + } + +protected: + PresetsContainer *presets_; +}; + } // namespace bslamp2 } // namespace xiaomi } // namespace esphome