mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 08:55:36 +00:00
[modbus_controller] Migrate triggers to callback automation
This commit is contained in:
@@ -5,14 +5,7 @@ import esphome.codegen as cg
|
||||
from esphome.components import modbus
|
||||
from esphome.components.const import CONF_ENABLED
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_NAME,
|
||||
CONF_OFFSET,
|
||||
CONF_TRIGGER_ID,
|
||||
)
|
||||
from esphome.const import CONF_ADDRESS, CONF_ID, CONF_LAMBDA, CONF_NAME, CONF_OFFSET
|
||||
from esphome.cpp_helpers import logging
|
||||
|
||||
from .const import (
|
||||
@@ -135,17 +128,6 @@ CPP_TYPE_REGISTER_MAP = {
|
||||
"FP32_R": cg.float_,
|
||||
}
|
||||
|
||||
ModbusCommandSentTrigger = modbus_controller_ns.class_(
|
||||
"ModbusCommandSentTrigger", automation.Trigger.template(cg.int_, cg.int_)
|
||||
)
|
||||
|
||||
ModbusOnlineTrigger = modbus_controller_ns.class_(
|
||||
"ModbusOnlineTrigger", automation.Trigger.template(cg.int_, cg.int_)
|
||||
)
|
||||
|
||||
ModbusOfflineTrigger = modbus_controller_ns.class_(
|
||||
"ModbusOfflineTrigger", automation.Trigger.template(cg.int_, cg.int_)
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -182,23 +164,9 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(
|
||||
CONF_SERVER_REGISTERS,
|
||||
): cv.ensure_list(ModbusServerRegisterSchema),
|
||||
cv.Optional(CONF_ON_COMMAND_SENT): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
|
||||
ModbusCommandSentTrigger
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_ONLINE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOnlineTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_OFFLINE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ModbusOfflineTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_COMMAND_SENT): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_ONLINE): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_OFFLINE): automation.validate_automation({}),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
@@ -363,19 +331,25 @@ async def to_code(config):
|
||||
cg.add(var.add_server_register(server_register_var))
|
||||
await register_modbus_device(var, config)
|
||||
for conf in config.get(CONF_ON_COMMAND_SENT, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_command_sent_callback",
|
||||
[(cg.int_, "function_code"), (cg.int_, "address")],
|
||||
conf,
|
||||
)
|
||||
for conf in config.get(CONF_ON_ONLINE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_online_callback",
|
||||
[(cg.int_, "function_code"), (cg.int_, "address")],
|
||||
conf,
|
||||
)
|
||||
for conf in config.get(CONF_ON_OFFLINE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(cg.int_, "function_code"), (cg.int_, "address")], conf
|
||||
await automation.build_callback_automation(
|
||||
var,
|
||||
"add_on_offline_callback",
|
||||
[(cg.int_, "function_code"), (cg.int_, "address")],
|
||||
conf,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/modbus_controller/modbus_controller.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace modbus_controller {
|
||||
|
||||
class ModbusCommandSentTrigger : public Trigger<int, int> {
|
||||
public:
|
||||
ModbusCommandSentTrigger(ModbusController *a_modbuscontroller) {
|
||||
a_modbuscontroller->add_on_command_sent_callback(
|
||||
[this](int function_code, int address) { this->trigger(function_code, address); });
|
||||
}
|
||||
};
|
||||
|
||||
class ModbusOnlineTrigger : public Trigger<int, int> {
|
||||
public:
|
||||
ModbusOnlineTrigger(ModbusController *a_modbuscontroller) {
|
||||
a_modbuscontroller->add_on_online_callback(
|
||||
[this](int function_code, int address) { this->trigger(function_code, address); });
|
||||
}
|
||||
};
|
||||
|
||||
class ModbusOfflineTrigger : public Trigger<int, int> {
|
||||
public:
|
||||
ModbusOfflineTrigger(ModbusController *a_modbuscontroller) {
|
||||
a_modbuscontroller->add_on_offline_callback(
|
||||
[this](int function_code, int address) { this->trigger(function_code, address); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace modbus_controller
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user