[opentherm] Build before_send and before_process_response with build_callback_automation (#20450)

This commit is contained in:
J. Nick Koston
2026-10-09 15:42:55 -10:00
committed by GitHub
parent fdc163a30d
commit 02bfa07819
3 changed files with 16 additions and 53 deletions
+14 -30
View File
@@ -5,7 +5,7 @@ import esphome.codegen as cg
from esphome.components import sensor
from esphome.components.esp32 import include_builtin_idf_component
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_TRIGGER_ID, PLATFORM_ESP32, PLATFORM_ESP8266
from esphome.const import CONF_ID, PLATFORM_ESP32, PLATFORM_ESP8266
from esphome.core import CORE
from . import const, generate, schema, validate
@@ -27,14 +27,16 @@ CONF_SYNC_MODE = "sync_mode"
CONF_BEFORE_SEND = "before_send"
CONF_BEFORE_PROCESS_RESPONSE = "before_process_response"
# Triggers
BeforeSendTrigger = generate.opentherm_ns.class_(
"BeforeSendTrigger",
automation.Trigger.template(generate.OpenthermData.operator("ref")),
)
BeforeProcessResponseTrigger = generate.opentherm_ns.class_(
"BeforeProcessResponseTrigger",
automation.Trigger.template(generate.OpenthermData.operator("ref")),
_DATA_ARGS = [(generate.OpenthermData.operator("ref"), "x")]
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(
CONF_BEFORE_SEND, "add_on_before_send_callback", _DATA_ARGS
),
automation.CallbackAutomation(
CONF_BEFORE_PROCESS_RESPONSE,
"add_on_before_process_response_callback",
_DATA_ARGS,
),
)
CONFIG_SCHEMA = cv.All(
@@ -51,17 +53,9 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_SUMMER_MODE_ACTIVE, False): cv.boolean,
cv.Optional(CONF_DHW_BLOCK, False): cv.boolean,
cv.Optional(CONF_SYNC_MODE, False): cv.boolean,
cv.Optional(CONF_BEFORE_SEND): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(BeforeSendTrigger),
}
),
cv.Optional(CONF_BEFORE_SEND): automation.validate_automation({}),
cv.Optional(CONF_BEFORE_PROCESS_RESPONSE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
BeforeProcessResponseTrigger
),
}
{}
),
}
)
@@ -132,14 +126,4 @@ async def to_code(config: dict[str, Any]) -> None:
generate.define_setting_readers(const.SETTING, settings)
generate.add_messages(var, settings, schema.SETTINGS)
for conf in config.get(CONF_BEFORE_SEND, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(generate.OpenthermData.operator("ref"), "x")], conf
)
for conf in config.get(CONF_BEFORE_PROCESS_RESPONSE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(generate.OpenthermData.operator("ref"), "x")], conf
)
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
-23
View File
@@ -1,23 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "hub.h"
#include "opentherm.h"
namespace esphome::opentherm {
class BeforeSendTrigger final : public Trigger<OpenthermData &> {
public:
BeforeSendTrigger(OpenthermHub *hub) {
hub->add_on_before_send_callback([this](OpenthermData &x) { this->trigger(x); });
}
};
class BeforeProcessResponseTrigger final : public Trigger<OpenthermData &> {
public:
BeforeProcessResponseTrigger(OpenthermHub *hub) {
hub->add_on_before_process_response_callback([this](OpenthermData &x) { this->trigger(x); });
}
};
} // namespace esphome::opentherm
+2
View File
@@ -25,6 +25,8 @@ opentherm:
then:
- lambda: |-
ESP_LOGW("OT", ">> Sending message %d", x.id);
if (x.id == 0)
x.valueHB |= 0x01;
before_process_response:
then:
- lambda: |-