mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[micro_wake_word] Use the apply helpers for the start, stop and model automations (#19620)
This commit is contained in:
@@ -6,7 +6,6 @@ import re
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from esphome import automation, external_files, git
|
||||
from esphome.automation import register_action, register_condition
|
||||
from esphome.bundle import add_bundle_file
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, microphone, ota, psram
|
||||
@@ -58,17 +57,6 @@ micro_wake_word_ns = cg.esphome_ns.namespace("micro_wake_word")
|
||||
|
||||
MicroWakeWord = micro_wake_word_ns.class_("MicroWakeWord", cg.Component)
|
||||
|
||||
DisableModelAction = micro_wake_word_ns.class_("DisableModelAction", automation.Action)
|
||||
EnableModelAction = micro_wake_word_ns.class_("EnableModelAction", automation.Action)
|
||||
StartAction = micro_wake_word_ns.class_("StartAction", automation.Action)
|
||||
StopAction = micro_wake_word_ns.class_("StopAction", automation.Action)
|
||||
|
||||
ModelIsEnabledCondition = micro_wake_word_ns.class_(
|
||||
"ModelIsEnabledCondition", automation.Condition
|
||||
)
|
||||
IsRunningCondition = micro_wake_word_ns.class_(
|
||||
"IsRunningCondition", automation.Condition
|
||||
)
|
||||
|
||||
WakeWordModel = micro_wake_word_ns.class_("WakeWordModel")
|
||||
|
||||
@@ -619,23 +607,19 @@ async def to_code(config):
|
||||
|
||||
MICRO_WAKE_WORD_ACTION_SCHEMA = cv.Schema({cv.GenerateID(): cv.use_id(MicroWakeWord)})
|
||||
|
||||
|
||||
@register_action(
|
||||
automation.register_apply_action(
|
||||
"micro_wake_word.start",
|
||||
StartAction,
|
||||
MICRO_WAKE_WORD_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("start()"),
|
||||
)
|
||||
@register_action(
|
||||
"micro_wake_word.stop", StopAction, MICRO_WAKE_WORD_ACTION_SCHEMA, synchronous=True
|
||||
automation.register_apply_action(
|
||||
"micro_wake_word.stop",
|
||||
MICRO_WAKE_WORD_ACTION_SCHEMA,
|
||||
automation.ApplyCall("stop()"),
|
||||
)
|
||||
@register_condition(
|
||||
"micro_wake_word.is_running", IsRunningCondition, MICRO_WAKE_WORD_ACTION_SCHEMA
|
||||
automation.register_apply_condition(
|
||||
"micro_wake_word.is_running", MICRO_WAKE_WORD_ACTION_SCHEMA, "is_running()"
|
||||
)
|
||||
async def micro_wake_word_action_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
|
||||
MICRO_WAKE_WORLD_MODEL_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||
@@ -644,24 +628,18 @@ MICRO_WAKE_WORLD_MODEL_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@register_action(
|
||||
automation.register_apply_action(
|
||||
"micro_wake_word.enable_model",
|
||||
EnableModelAction,
|
||||
MICRO_WAKE_WORLD_MODEL_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("enable()"),
|
||||
)
|
||||
@register_action(
|
||||
automation.register_apply_action(
|
||||
"micro_wake_word.disable_model",
|
||||
DisableModelAction,
|
||||
MICRO_WAKE_WORLD_MODEL_ACTION_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.ApplyCall("disable()"),
|
||||
)
|
||||
@register_condition(
|
||||
automation.register_apply_condition(
|
||||
"micro_wake_word.model_is_enabled",
|
||||
ModelIsEnabledCondition,
|
||||
MICRO_WAKE_WORLD_MODEL_ACTION_SCHEMA,
|
||||
"is_enabled()",
|
||||
)
|
||||
async def model_action(config, action_id, template_arg, args):
|
||||
parent = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, parent)
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "micro_wake_word.h"
|
||||
#include "streaming_model.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
namespace esphome::micro_wake_word {
|
||||
|
||||
template<typename... Ts> class StartAction final : public Action<Ts...>, public Parented<MicroWakeWord> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->start(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class StopAction final : public Action<Ts...>, public Parented<MicroWakeWord> {
|
||||
public:
|
||||
void play(const Ts &...x) override { this->parent_->stop(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class IsRunningCondition final : public Condition<Ts...>, public Parented<MicroWakeWord> {
|
||||
public:
|
||||
bool check(const Ts &...x) override { return this->parent_->is_running(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class EnableModelAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit EnableModelAction(WakeWordModel *wake_word_model) : wake_word_model_(wake_word_model) {}
|
||||
void play(const Ts &...x) override { this->wake_word_model_->enable(); }
|
||||
|
||||
protected:
|
||||
WakeWordModel *wake_word_model_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class DisableModelAction final : public Action<Ts...> {
|
||||
public:
|
||||
explicit DisableModelAction(WakeWordModel *wake_word_model) : wake_word_model_(wake_word_model) {}
|
||||
void play(const Ts &...x) override { this->wake_word_model_->disable(); }
|
||||
|
||||
protected:
|
||||
WakeWordModel *wake_word_model_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class ModelIsEnabledCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
explicit ModelIsEnabledCondition(WakeWordModel *wake_word_model) : wake_word_model_(wake_word_model) {}
|
||||
bool check(const Ts &...x) override { return this->wake_word_model_->is_enabled(); }
|
||||
|
||||
protected:
|
||||
WakeWordModel *wake_word_model_;
|
||||
};
|
||||
|
||||
} // namespace esphome::micro_wake_word
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user