mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 07:20:24 +00:00
[sensor] Use register_apply_action for sensor.template.publish (#19507)
This commit is contained in:
@@ -256,7 +256,6 @@ SensorPtr = Sensor.operator("ptr")
|
||||
ValueRangeTrigger = sensor_ns.class_(
|
||||
"ValueRangeTrigger", automation.Trigger.template(cg.float_), cg.Component
|
||||
)
|
||||
SensorPublishAction = sensor_ns.class_("SensorPublishAction", automation.Action)
|
||||
|
||||
# Filters
|
||||
Filter = sensor_ns.class_("Filter")
|
||||
|
||||
@@ -20,17 +20,6 @@ class SensorRawStateTrigger final : public Trigger<float> {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class SensorPublishAction final : public Action<Ts...> {
|
||||
public:
|
||||
SensorPublishAction(Sensor *sensor) : sensor_(sensor) {}
|
||||
TEMPLATABLE_VALUE(float, state)
|
||||
|
||||
void play(const Ts &...x) override { this->sensor_->publish_state(this->state_.value(x...)); }
|
||||
|
||||
protected:
|
||||
Sensor *sensor_;
|
||||
};
|
||||
|
||||
class ValueRangeTrigger final : public Trigger<float>, public Component {
|
||||
public:
|
||||
explicit ValueRangeTrigger(Sensor *parent) : parent_(parent) {}
|
||||
|
||||
@@ -49,20 +49,13 @@ async def to_code(config):
|
||||
cg.add(var.set_template(template_))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
automation.register_apply_action(
|
||||
"sensor.template.publish",
|
||||
sensor.SensorPublishAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(sensor.Sensor),
|
||||
cv.Required(CONF_STATE): cv.templatable(cv.float_),
|
||||
}
|
||||
),
|
||||
synchronous=True,
|
||||
automation.ApplyField(CONF_STATE, "publish_state", cg.float_),
|
||||
)
|
||||
async def sensor_template_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_))
|
||||
return var
|
||||
|
||||
@@ -5,6 +5,17 @@ sensor:
|
||||
id: source_sensor
|
||||
lambda: return 42.0;
|
||||
update_interval: 1s
|
||||
on_value:
|
||||
- sensor.template.publish:
|
||||
id: publish_target
|
||||
state: 42.0
|
||||
- sensor.template.publish:
|
||||
id: publish_target
|
||||
state: !lambda "return x * 2.0;"
|
||||
|
||||
- platform: template
|
||||
name: "Publish Target"
|
||||
id: publish_target
|
||||
|
||||
# Streaming filters (window_size == send_every) - uses StreamingFilter base class
|
||||
- platform: copy
|
||||
|
||||
Reference in New Issue
Block a user