Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nextion] Add publish actions #7646

Merged
merged 8 commits into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esphome/components/nextion/__init__.py
Original file line number Diff line number Diff line change
@@ -6,3 +6,5 @@
nextion_ref = Nextion.operator("ref")

CONF_NEXTION_ID = "nextion_id"
CONF_PUBLISH_STATE = "publish_state"
CONF_SEND_TO_NEXTION = "send_to_nextion"
75 changes: 72 additions & 3 deletions esphome/components/nextion/automation.h
Original file line number Diff line number Diff line change
@@ -5,6 +5,13 @@
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) {
@@ -42,11 +49,73 @@ class TouchTrigger : public Trigger<uint8_t, uint8_t, bool> {
}
};

class BufferOverflowTrigger : public Trigger<> {
template<typename... Ts> class NextionPublishFloatAction : public Action<Ts...> {
public:
explicit BufferOverflowTrigger(Nextion *nextion) {
nextion->add_buffer_overflow_event_callback([this]() { this->trigger(); });
explicit NextionPublishFloatAction(NextionComponent *component) : component_(component) {}

TEMPLATABLE_VALUE(float, state)
TEMPLATABLE_VALUE(bool, publish_state)
TEMPLATABLE_VALUE(bool, send_to_nextion)

void play(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 : 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(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 : 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(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 nextion
45 changes: 43 additions & 2 deletions esphome/components/nextion/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor

from esphome.const import CONF_COMPONENT_ID, CONF_PAGE_ID, CONF_ID
from .. import nextion_ns, CONF_NEXTION_ID
from esphome.const import (
CONF_ID,
CONF_STATE,
CONF_COMPONENT_ID,
CONF_PAGE_ID,
)

from .. import nextion_ns, CONF_NEXTION_ID, CONF_PUBLISH_STATE, CONF_SEND_TO_NEXTION


from ..base_component import (
@@ -19,6 +26,10 @@
"NextionBinarySensor", binary_sensor.BinarySensor, cg.PollingComponent
)

NextionPublishBoolAction = nextion_ns.class_(
"NextionPublishBoolAction", automation.Action
)

CONFIG_SCHEMA = cv.All(
binary_sensor.binary_sensor_schema(NextionBinarySensor)
.extend(
@@ -52,3 +63,33 @@ async def to_code(config):
if CONF_COMPONENT_NAME in config or CONF_VARIABLE_NAME in config:
await setup_component_core_(var, config, ".val")
cg.add(hub.register_binarysensor_component(var))


@automation.register_action(
"binary_sensor.nextion.publish",
NextionPublishBoolAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionBinarySensor),
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
cv.Optional(CONF_PUBLISH_STATE, default="true"): cv.templatable(cv.boolean),
cv.Optional(CONF_SEND_TO_NEXTION, default="true"): cv.templatable(
cv.boolean
),
}
),
)
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, bool)
cg.add(var.set_state(template_))

template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, bool)
cg.add(var.set_publish_state(template_))

template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, bool)
cg.add(var.set_send_to_nextion(template_))

return var
43 changes: 38 additions & 5 deletions esphome/components/nextion/sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor

from esphome.const import (
CONF_ID,
CONF_COMPONENT_ID,
)
from .. import nextion_ns, CONF_NEXTION_ID
from esphome.const import CONF_ID, CONF_COMPONENT_ID, CONF_STATE

from .. import nextion_ns, CONF_NEXTION_ID, CONF_PUBLISH_STATE, CONF_SEND_TO_NEXTION

from ..base_component import (
setup_component_core_,
@@ -25,6 +24,10 @@

NextionSensor = nextion_ns.class_("NextionSensor", sensor.Sensor, cg.PollingComponent)

NextionPublishFloatAction = nextion_ns.class_(
"NextionPublishFloatAction", automation.Action
)


def CheckWaveID(value):
value = cv.int_(value)
@@ -95,3 +98,33 @@ async def to_code(config):

if CONF_WAVE_MAX_LENGTH in config:
cg.add(var.set_wave_max_length(config[CONF_WAVE_MAX_LENGTH]))


@automation.register_action(
"sensor.nextion.publish",
NextionPublishFloatAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionSensor),
cv.Required(CONF_STATE): cv.templatable(cv.float_),
cv.Optional(CONF_PUBLISH_STATE, default="true"): cv.templatable(cv.boolean),
cv.Optional(CONF_SEND_TO_NEXTION, default="true"): cv.templatable(
cv.boolean
),
}
),
)
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, float)
cg.add(var.set_state(template_))

template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, bool)
cg.add(var.set_publish_state(template_))

template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, bool)
cg.add(var.set_send_to_nextion(template_))

return var
40 changes: 38 additions & 2 deletions esphome/components/nextion/switch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import switch

from esphome.const import CONF_ID
from .. import nextion_ns, CONF_NEXTION_ID
from esphome.const import CONF_ID, CONF_STATE

from .. import nextion_ns, CONF_NEXTION_ID, CONF_PUBLISH_STATE, CONF_SEND_TO_NEXTION

from ..base_component import (
setup_component_core_,
@@ -16,6 +18,10 @@

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)
@@ -33,3 +39,33 @@ async def to_code(config):
cg.add(hub.register_switch_component(var))

await setup_component_core_(var, config, ".val")


@automation.register_action(
"switch.nextion.publish",
NextionPublishBoolAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionSwitch),
cv.Required(CONF_STATE): cv.templatable(cv.boolean),
cv.Optional(CONF_PUBLISH_STATE, default="true"): cv.templatable(cv.boolean),
cv.Optional(CONF_SEND_TO_NEXTION, default="true"): cv.templatable(
cv.boolean
),
}
),
)
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, bool)
cg.add(var.set_state(template_))

template_ = await cg.templatable(config[CONF_PUBLISH_STATE], args, bool)
cg.add(var.set_publish_state(template_))

template_ = await cg.templatable(config[CONF_SEND_TO_NEXTION], args, bool)
cg.add(var.set_send_to_nextion(template_))

return var
39 changes: 37 additions & 2 deletions esphome/components/nextion/text_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from esphome import automation
from esphome.components import text_sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
from esphome.const import CONF_ID, CONF_STATE

from .. import nextion_ns, CONF_NEXTION_ID
from .. import nextion_ns, CONF_NEXTION_ID, CONF_PUBLISH_STATE, CONF_SEND_TO_NEXTION

from ..base_component import (
setup_component_core_,
@@ -16,6 +17,10 @@
"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)
@@ -32,3 +37,33 @@ async def to_code(config):
cg.add(hub.register_textsensor_component(var))

await setup_component_core_(var, config, ".txt")


@automation.register_action(
"text_sensor.nextion.publish",
NextionPublishTextAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NextionTextSensor),
cv.Required(CONF_STATE): cv.templatable(cv.string_strict),
cv.Optional(CONF_PUBLISH_STATE, default="true"): cv.templatable(cv.boolean),
cv.Optional(CONF_SEND_TO_NEXTION, default="true"): cv.templatable(
cv.boolean
),
}
),
)
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
Loading
Oops, something went wrong.