Browse Source

Added an ActivatePresetAction, which will be used to apply presets from automations.

pull/9/head
Maurice Makaay 3 years ago
parent
commit
31ae3b5123
2 changed files with 60 additions and 15 deletions
  1. +41
    -15
      light/__init__.py
  2. +19
    -0
      light/automation.h

+ 41
- 15
light/__init__.py View File

@ -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):


+ 19
- 0
light/automation.h View File

@ -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<typename... Ts> class ActivatePresetAction : public Action<Ts...> {
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

Loading…
Cancel
Save