mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 08:55:36 +00:00
[sml] Migrate DataTrigger to callback automation
This commit is contained in:
@@ -4,7 +4,7 @@ from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import uart
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_ON_DATA, CONF_TRIGGER_ID
|
||||
from esphome.const import CONF_ID, CONF_ON_DATA
|
||||
|
||||
CODEOWNERS = ["@alengwenus"]
|
||||
|
||||
@@ -18,24 +18,11 @@ CONF_SML_ID = "sml_id"
|
||||
CONF_OBIS_CODE = "obis_code"
|
||||
CONF_SERVER_ID = "server_id"
|
||||
|
||||
sml_ns = cg.esphome_ns.namespace("sml")
|
||||
|
||||
DataTrigger = sml_ns.class_(
|
||||
"DataTrigger",
|
||||
automation.Trigger.template(
|
||||
cg.std_vector.template(cg.uint8).operator("ref"), cg.bool_
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(Sml),
|
||||
cv.Optional(CONF_ON_DATA): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(DataTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_DATA): automation.validate_automation({}),
|
||||
}
|
||||
).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
||||
@@ -45,9 +32,9 @@ async def to_code(config):
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
||||
for conf in config.get(CONF_ON_DATA, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger,
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_data_callback",
|
||||
[
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("ref").operator("const"),
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "sml.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace sml {
|
||||
|
||||
class DataTrigger : public Trigger<const std::vector<uint8_t> &, bool> {
|
||||
public:
|
||||
explicit DataTrigger(Sml *sml) {
|
||||
sml->add_on_data_callback([this](const std::vector<uint8_t> &data, bool valid) { this->trigger(data, valid); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sml
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user