[integration] Add set method to publish and save sensor value (#13316)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
JiriPrchal
2026-03-04 15:00:09 +01:00
committed by GitHub
parent cba34e770e
commit 37146ff565
3 changed files with 41 additions and 7 deletions
@@ -32,6 +32,7 @@ class IntegrationSensor : public sensor::Sensor, public Component {
void set_method(IntegrationMethod method) { method_ = method; }
void set_restore(bool restore) { restore_ = restore; }
void reset() { this->publish_and_save_(0.0f); }
void set_value(float value) { this->publish_and_save_(value); }
protected:
void process_sensor_value_(float value);
@@ -71,14 +72,16 @@ class IntegrationSensor : public sensor::Sensor, public Component {
float last_value_{0.0f};
};
template<typename... Ts> class ResetAction : public Action<Ts...> {
template<typename... Ts> class ResetAction : public Action<Ts...>, public Parented<IntegrationSensor> {
public:
explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {}
void play(const Ts &...x) override { this->parent_->reset(); }
};
protected:
IntegrationSensor *parent_;
template<typename... Ts> class SetValueAction : public Action<Ts...>, public Parented<IntegrationSensor> {
public:
TEMPLATABLE_VALUE(float, value)
void play(const Ts &...x) override { this->parent_->set_value(this->value_.value(x...)); }
};
} // namespace integration
+23 -2
View File
@@ -9,6 +9,7 @@ from esphome.const import (
CONF_RESTORE,
CONF_SENSOR,
CONF_UNIT_OF_MEASUREMENT,
CONF_VALUE,
)
from esphome.core.entity_helpers import inherit_property_from
@@ -17,6 +18,7 @@ IntegrationSensor = integration_ns.class_(
"IntegrationSensor", sensor.Sensor, cg.Component
)
ResetAction = integration_ns.class_("ResetAction", automation.Action)
SetValueAction = integration_ns.class_("SetValueAction", automation.Action)
IntegrationSensorTime = integration_ns.enum("IntegrationSensorTime")
INTEGRATION_TIMES = {
@@ -111,5 +113,24 @@ async def to_code(config):
),
)
async def sensor_integration_reset_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@automation.register_action(
"sensor.integration.set_value",
SetValueAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(IntegrationSensor),
cv.Required(CONF_VALUE): cv.templatable(cv.float_),
}
),
)
async def sensor_integration_set_value_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_VALUE], args, float)
cg.add(var.set_value(template_))
return var
@@ -1,9 +1,19 @@
esphome:
on_boot:
then:
- sensor.integration.reset:
id: integration_sensor
- sensor.integration.set_value:
id: integration_sensor
value: 100.0
sensor:
- platform: adc
id: my_sensor
pin: ${pin}
attenuation: 12db
- platform: integration
id: integration_sensor
sensor: my_sensor
name: Integration Sensor
time_unit: s