[nextion] Use register_apply_action for set_brightness and the publish actions (#19529)

This commit is contained in:
J. Nick Koston
2026-09-23 16:47:31 -05:00
committed by GitHub
parent ce6a66959a
commit 487ef61d0a
7 changed files with 40 additions and 196 deletions
-96
View File
@@ -1,96 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/string_ref.h"
#include "nextion.h"
namespace esphome::nextion {
template<typename... Ts> class NextionSetBrightnessAction final : public Action<Ts...> {
public:
explicit NextionSetBrightnessAction(Nextion *component) : component_(component) {}
TEMPLATABLE_VALUE(float, brightness)
void play(const Ts &...x) override {
this->component_->set_brightness(this->brightness_.value(x...));
this->component_->set_backlight_brightness(this->brightness_.value(x...));
}
void set_brightness(std::function<void(Ts..., float)> brightness) { this->brightness_ = brightness; }
protected:
Nextion *component_;
};
template<typename... Ts> class NextionPublishFloatAction final : public Action<Ts...> {
public:
explicit NextionPublishFloatAction(NextionComponent *component) : component_(component) {}
TEMPLATABLE_VALUE(float, state)
TEMPLATABLE_VALUE(bool, publish_state)
TEMPLATABLE_VALUE(bool, send_to_nextion)
void play(const Ts &...x) override {
this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
this->send_to_nextion_.value(x...));
}
void set_state(std::function<void(Ts..., float)> state) { this->state_ = state; }
void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
this->send_to_nextion_ = send_to_nextion;
}
protected:
NextionComponent *component_;
};
template<typename... Ts> class NextionPublishTextAction final : public Action<Ts...> {
public:
explicit NextionPublishTextAction(NextionComponent *component) : component_(component) {}
TEMPLATABLE_VALUE(const char *, state)
TEMPLATABLE_VALUE(bool, publish_state)
TEMPLATABLE_VALUE(bool, send_to_nextion)
void play(const Ts &...x) override {
this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
this->send_to_nextion_.value(x...));
}
void set_state(std::function<void(Ts..., const char *)> state) { this->state_ = state; }
void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
this->send_to_nextion_ = send_to_nextion;
}
protected:
NextionComponent *component_;
};
template<typename... Ts> class NextionPublishBoolAction final : public Action<Ts...> {
public:
explicit NextionPublishBoolAction(NextionComponent *component) : component_(component) {}
TEMPLATABLE_VALUE(bool, state)
TEMPLATABLE_VALUE(bool, publish_state)
TEMPLATABLE_VALUE(bool, send_to_nextion)
void play(const Ts &...x) override {
this->component_->set_state(this->state_.value(x...), this->publish_state_.value(x...),
this->send_to_nextion_.value(x...));
}
void set_state(std::function<void(Ts..., bool)> state) { this->state_ = state; }
void set_publish_state(std::function<void(Ts..., bool)> publish_state) { this->publish_state_ = publish_state; }
void set_send_to_nextion(std::function<void(Ts..., bool)> send_to_nextion) {
this->send_to_nextion_ = send_to_nextion;
}
protected:
NextionComponent *component_;
};
} // namespace esphome::nextion
@@ -18,10 +18,6 @@ NextionBinarySensor = nextion_ns.class_(
"NextionBinarySensor", binary_sensor.BinarySensor, cg.PollingComponent
)
NextionPublishBoolAction = nextion_ns.class_(
"NextionPublishBoolAction", automation.Action
)
CONFIG_SCHEMA = cv.All(
binary_sensor.binary_sensor_schema(NextionBinarySensor)
.extend(
@@ -57,9 +53,8 @@ async def to_code(config):
cg.add(hub.register_binarysensor_component(var))
@automation.register_action(
automation.register_apply_action(
"binary_sensor.nextion.publish",
NextionPublishBoolAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionBinarySensor),
@@ -70,19 +65,12 @@ async def to_code(config):
),
}
),
synchronous=True,
automation.ApplyCall(
"set_state({}, {}, {})",
(
(CONF_STATE, cg.bool_),
(CONF_PUBLISH_STATE, cg.bool_),
(CONF_SEND_TO_NEXTION, cg.bool_),
),
),
)
async def sensor_nextion_publish_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_STATE], args, cg.bool_)
cg.add(var.set_state(template_))
template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, cg.bool_)
cg.add(var.set_publish_state(template_))
template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, cg.bool_)
cg.add(var.set_send_to_nextion(template_))
return var
+3 -16
View File
@@ -56,11 +56,6 @@ def AUTO_LOAD() -> list[str]:
return base
NextionSetBrightnessAction = nextion_ns.class_(
"NextionSetBrightnessAction", automation.Action
)
def _deprecated_dump_device_info(value):
_LOGGER.warning(
"'dump_device_info' is deprecated and will be removed in ESPHome 2026.11.0. "
@@ -160,9 +155,8 @@ CONFIG_SCHEMA = cv.All(
)
@automation.register_action(
automation.register_apply_action(
"display.nextion.set_brightness",
NextionSetBrightnessAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(Nextion),
@@ -170,16 +164,9 @@ CONFIG_SCHEMA = cv.All(
},
key=CONF_BRIGHTNESS,
),
synchronous=True,
automation.ApplyField(CONF_BRIGHTNESS, "set_brightness", cg.float_),
automation.ApplyField(CONF_BRIGHTNESS, "set_backlight_brightness", cg.float_),
)
async def nextion_set_brightness_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_BRIGHTNESS], args, cg.float_)
cg.add(var.set_brightness(template_))
return var
_CALLBACK_AUTOMATIONS = (
+9 -21
View File
@@ -21,10 +21,6 @@ CODEOWNERS = ["@senexcrenshaw"]
NextionSensor = nextion_ns.class_("NextionSensor", sensor.Sensor, cg.PollingComponent)
NextionPublishFloatAction = nextion_ns.class_(
"NextionPublishFloatAction", automation.Action
)
def CheckWaveID(value):
value = cv.int_(value)
@@ -97,9 +93,8 @@ async def to_code(config):
cg.add(var.set_wave_max_length(config[CONF_WAVE_MAX_LENGTH]))
@automation.register_action(
automation.register_apply_action(
"sensor.nextion.publish",
NextionPublishFloatAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionSensor),
@@ -110,19 +105,12 @@ async def to_code(config):
),
}
),
synchronous=True,
automation.ApplyCall(
"set_state({}, {}, {})",
(
(CONF_STATE, cg.float_),
(CONF_PUBLISH_STATE, cg.bool_),
(CONF_SEND_TO_NEXTION, cg.bool_),
),
),
)
async def sensor_nextion_publish_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_STATE], args, cg.float_)
cg.add(var.set_state(template_))
template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, cg.bool_)
cg.add(var.set_publish_state(template_))
template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, cg.bool_)
cg.add(var.set_send_to_nextion(template_))
return var
+9 -21
View File
@@ -16,10 +16,6 @@ CODEOWNERS = ["@senexcrenshaw"]
NextionSwitch = nextion_ns.class_("NextionSwitch", switch.Switch, cg.PollingComponent)
NextionPublishBoolAction = nextion_ns.class_(
"NextionPublishBoolAction", automation.Action
)
CONFIG_SCHEMA = cv.All(
switch.switch_schema(NextionSwitch)
.extend(CONFIG_SWITCH_COMPONENT_SCHEMA)
@@ -39,9 +35,8 @@ async def to_code(config):
await setup_component_core_(var, config, ".val")
@automation.register_action(
automation.register_apply_action(
"switch.nextion.publish",
NextionPublishBoolAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionSwitch),
@@ -52,19 +47,12 @@ async def to_code(config):
),
}
),
synchronous=True,
automation.ApplyCall(
"set_state({}, {}, {})",
(
(CONF_STATE, cg.bool_),
(CONF_PUBLISH_STATE, cg.bool_),
(CONF_SEND_TO_NEXTION, cg.bool_),
),
),
)
async def sensor_nextion_publish_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_STATE], args, cg.bool_)
cg.add(var.set_state(template_))
template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, cg.bool_)
cg.add(var.set_publish_state(template_))
template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, cg.bool_)
cg.add(var.set_send_to_nextion(template_))
return var
@@ -13,10 +13,6 @@ NextionTextSensor = nextion_ns.class_(
"NextionTextSensor", text_sensor.TextSensor, cg.PollingComponent
)
NextionPublishTextAction = nextion_ns.class_(
"NextionPublishTextAction", automation.Action
)
CONFIG_SCHEMA = (
text_sensor.text_sensor_schema(NextionTextSensor)
.extend(CONFIG_TEXT_COMPONENT_SCHEMA)
@@ -35,9 +31,8 @@ async def to_code(config):
await setup_component_core_(var, config, ".txt")
@automation.register_action(
automation.register_apply_action(
"text_sensor.nextion.publish",
NextionPublishTextAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionTextSensor),
@@ -48,19 +43,12 @@ async def to_code(config):
),
}
),
synchronous=True,
automation.ApplyCall(
"set_state({}, {}, {})",
(
(CONF_STATE, cg.std_string),
(CONF_PUBLISH_STATE, cg.bool_),
(CONF_SEND_TO_NEXTION, cg.bool_),
),
),
)
async def sensor_nextion_publish_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_STATE], args, cg.const_char_ptr)
cg.add(var.set_state(template_))
template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, cg.bool_)
cg.add(var.set_publish_state(template_))
template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, cg.bool_)
cg.add(var.set_send_to_nextion(template_))
return var
+1
View File
@@ -3,6 +3,7 @@ esphome:
- lambda: 'ESP_LOGD("display","is_connected(): %s", YESNO(id(main_lcd).is_connected()));'
- display.nextion.set_brightness: 80%
- display.nextion.set_brightness: !lambda 'return 0.5;'
# Binary sensor publish action tests
- binary_sensor.nextion.publish: