mirror of
https://github.com/esphome/esphome.git
synced 2026-09-03 19:46:02 +00:00
[nextion] Migrate triggers to callback automation (#15227)
This commit is contained in:
@@ -5,50 +5,6 @@
|
||||
namespace esphome {
|
||||
namespace nextion {
|
||||
|
||||
class BufferOverflowTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit BufferOverflowTrigger(Nextion *nextion) {
|
||||
nextion->add_buffer_overflow_event_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
class SetupTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit SetupTrigger(Nextion *nextion) {
|
||||
nextion->add_setup_state_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
class SleepTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit SleepTrigger(Nextion *nextion) {
|
||||
nextion->add_sleep_state_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
class WakeTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit WakeTrigger(Nextion *nextion) {
|
||||
nextion->add_wake_state_callback([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
class PageTrigger : public Trigger<uint8_t> {
|
||||
public:
|
||||
explicit PageTrigger(Nextion *nextion) {
|
||||
nextion->add_new_page_callback([this](const uint8_t page_id) { this->trigger(page_id); });
|
||||
}
|
||||
};
|
||||
|
||||
class TouchTrigger : public Trigger<uint8_t, uint8_t, bool> {
|
||||
public:
|
||||
explicit TouchTrigger(Nextion *nextion) {
|
||||
nextion->add_touch_event_callback([this](uint8_t page_id, uint8_t component_id, bool touch_event) {
|
||||
this->trigger(page_id, component_id, touch_event);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class NextionSetBrightnessAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit NextionSetBrightnessAction(Nextion *component) : component_(component) {}
|
||||
|
||||
@@ -2,13 +2,7 @@ from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import display, esp32, uart
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_BRIGHTNESS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_ON_TOUCH,
|
||||
CONF_TRIGGER_ID,
|
||||
)
|
||||
from esphome.const import CONF_BRIGHTNESS, CONF_ID, CONF_LAMBDA, CONF_ON_TOUCH
|
||||
from esphome.core import CORE, TimePeriod
|
||||
|
||||
from . import ( # noqa: F401 pylint: disable=unused-import
|
||||
@@ -55,14 +49,6 @@ def AUTO_LOAD() -> list[str]:
|
||||
NextionSetBrightnessAction = nextion_ns.class_(
|
||||
"NextionSetBrightnessAction", automation.Action
|
||||
)
|
||||
SetupTrigger = nextion_ns.class_("SetupTrigger", automation.Trigger.template())
|
||||
SleepTrigger = nextion_ns.class_("SleepTrigger", automation.Trigger.template())
|
||||
WakeTrigger = nextion_ns.class_("WakeTrigger", automation.Trigger.template())
|
||||
PageTrigger = nextion_ns.class_("PageTrigger", automation.Trigger.template())
|
||||
TouchTrigger = nextion_ns.class_("TouchTrigger", automation.Trigger.template())
|
||||
BufferOverflowTrigger = nextion_ns.class_(
|
||||
"BufferOverflowTrigger", automation.Trigger.template()
|
||||
)
|
||||
|
||||
|
||||
def _validate_tft_upload(config):
|
||||
@@ -101,38 +87,12 @@ CONFIG_SCHEMA = cv.All(
|
||||
),
|
||||
cv.Optional(CONF_MAX_COMMANDS_PER_LOOP): cv.uint16_t,
|
||||
cv.Optional(CONF_MAX_QUEUE_SIZE): cv.positive_int,
|
||||
cv.Optional(CONF_ON_BUFFER_OVERFLOW): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
BufferOverflowTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PAGE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PageTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_SETUP): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SetupTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_SLEEP): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SleepTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_TOUCH): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(TouchTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_WAKE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(WakeTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_BUFFER_OVERFLOW): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_PAGE): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_SETUP): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_SLEEP): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_TOUCH): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_WAKE): automation.validate_automation({}),
|
||||
cv.Optional(CONF_SKIP_CONNECTION_HANDSHAKE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_STARTUP_OVERRIDE_MS, default="8000ms"): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
@@ -273,25 +233,25 @@ async def to_code(config):
|
||||
await display.register_display(var, config)
|
||||
|
||||
for conf in config.get(CONF_ON_SETUP, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
await automation.build_callback_automation(
|
||||
var, "add_setup_state_callback", [], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_SLEEP, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
await automation.build_callback_automation(
|
||||
var, "add_sleep_state_callback", [], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_WAKE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
await automation.build_callback_automation(
|
||||
var, "add_wake_state_callback", [], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_PAGE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(cg.uint8, "x")], conf)
|
||||
|
||||
await automation.build_callback_automation(
|
||||
var, "add_new_page_callback", [(cg.uint8, "x")], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_TOUCH, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger,
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_touch_event_callback",
|
||||
[
|
||||
(cg.uint8, "page_id"),
|
||||
(cg.uint8, "component_id"),
|
||||
@@ -299,7 +259,7 @@ async def to_code(config):
|
||||
],
|
||||
conf,
|
||||
)
|
||||
|
||||
for conf in config.get(CONF_ON_BUFFER_OVERFLOW, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
await automation.build_callback_automation(
|
||||
var, "add_buffer_overflow_event_callback", [], conf
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user