mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 21:59:59 +08:00
[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:
@@ -32,6 +32,7 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
|||||||
void set_method(IntegrationMethod method) { method_ = method; }
|
void set_method(IntegrationMethod method) { method_ = method; }
|
||||||
void set_restore(bool restore) { restore_ = restore; }
|
void set_restore(bool restore) { restore_ = restore; }
|
||||||
void reset() { this->publish_and_save_(0.0f); }
|
void reset() { this->publish_and_save_(0.0f); }
|
||||||
|
void set_value(float value) { this->publish_and_save_(value); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void process_sensor_value_(float value);
|
void process_sensor_value_(float value);
|
||||||
@@ -71,14 +72,16 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
|||||||
float last_value_{0.0f};
|
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:
|
public:
|
||||||
explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {}
|
|
||||||
|
|
||||||
void play(const Ts &...x) override { this->parent_->reset(); }
|
void play(const Ts &...x) override { this->parent_->reset(); }
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
template<typename... Ts> class SetValueAction : public Action<Ts...>, public Parented<IntegrationSensor> {
|
||||||
IntegrationSensor *parent_;
|
public:
|
||||||
|
TEMPLATABLE_VALUE(float, value)
|
||||||
|
|
||||||
|
void play(const Ts &...x) override { this->parent_->set_value(this->value_.value(x...)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace integration
|
} // namespace integration
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from esphome.const import (
|
|||||||
CONF_RESTORE,
|
CONF_RESTORE,
|
||||||
CONF_SENSOR,
|
CONF_SENSOR,
|
||||||
CONF_UNIT_OF_MEASUREMENT,
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
|
CONF_VALUE,
|
||||||
)
|
)
|
||||||
from esphome.core.entity_helpers import inherit_property_from
|
from esphome.core.entity_helpers import inherit_property_from
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ IntegrationSensor = integration_ns.class_(
|
|||||||
"IntegrationSensor", sensor.Sensor, cg.Component
|
"IntegrationSensor", sensor.Sensor, cg.Component
|
||||||
)
|
)
|
||||||
ResetAction = integration_ns.class_("ResetAction", automation.Action)
|
ResetAction = integration_ns.class_("ResetAction", automation.Action)
|
||||||
|
SetValueAction = integration_ns.class_("SetValueAction", automation.Action)
|
||||||
|
|
||||||
IntegrationSensorTime = integration_ns.enum("IntegrationSensorTime")
|
IntegrationSensorTime = integration_ns.enum("IntegrationSensorTime")
|
||||||
INTEGRATION_TIMES = {
|
INTEGRATION_TIMES = {
|
||||||
@@ -111,5 +113,24 @@ async def to_code(config):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
async def sensor_integration_reset_to_code(config, action_id, template_arg, args):
|
async def sensor_integration_reset_to_code(config, action_id, template_arg, args):
|
||||||
paren = await cg.get_variable(config[CONF_ID])
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
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:
|
sensor:
|
||||||
- platform: adc
|
- platform: adc
|
||||||
id: my_sensor
|
id: my_sensor
|
||||||
pin: ${pin}
|
pin: ${pin}
|
||||||
attenuation: 12db
|
attenuation: 12db
|
||||||
- platform: integration
|
- platform: integration
|
||||||
|
id: integration_sensor
|
||||||
sensor: my_sensor
|
sensor: my_sensor
|
||||||
name: Integration Sensor
|
name: Integration Sensor
|
||||||
time_unit: s
|
time_unit: s
|
||||||
|
|||||||
Reference in New Issue
Block a user