Browse Source

The activate_preset action now supports simple string values.

New actions are:

* `preset.activate: next_group`
* `preset.activate: next_preset`
* `preset.activate: <group name>`
* `preset.activate: <group name>.<preset name>`
pull/9/head
Maurice Makaay 3 years ago
parent
commit
b28bf11c35
2 changed files with 37 additions and 14 deletions
  1. +33
    -12
      light/__init__.py
  2. +4
    -2
      light/presets.h

+ 33
- 12
light/__init__.py View File

@ -62,21 +62,42 @@ def is_preset_group(value):
def is_preset(value): def is_preset(value):
return 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( @automation.register_action(
"preset.activate", "preset.activate",
ActivatePresetAction, 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): def preset_activate_to_code(config, action_id, template_arg, args):
presets_var = yield cg.get_variable(config[CONF_PRESETS_ID]) presets_var = yield cg.get_variable(config[CONF_PRESETS_ID])


+ 4
- 2
light/presets.h View File

@ -94,7 +94,8 @@ public:
active_group_ = active_group_->next_group == nullptr active_group_ = active_group_->next_group == nullptr
? first_group_ : active_group_->next_group; ? first_group_ : active_group_->next_group;
if (active_group_->active_preset == nullptr) { 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; return;
} }
ESP_LOGW(TAG, "activate_next_group(): activating %s/%s", ESP_LOGW(TAG, "activate_next_group(): activating %s/%s",
@ -110,7 +111,8 @@ public:
} }
auto p = active_group_->active_preset; auto p = active_group_->active_preset;
if (p == nullptr) { 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; return;
} }
active_group_->active_preset = p->next_preset == nullptr active_group_->active_preset = p->next_preset == nullptr


Loading…
Cancel
Save