ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
integration_sensor.h
Go to the documentation of this file.
1#pragma once
2
6#include "esphome/core/hal.h"
8
9namespace esphome::integration {
10
18
24
26 public:
27 void setup() override;
28 void dump_config() override;
29 void set_sensor(Sensor *sensor) { sensor_ = sensor; }
30 void set_time(IntegrationSensorTime time) { time_ = time; }
31 void set_method(IntegrationMethod method) { method_ = method; }
32 void set_restore(bool restore) { restore_ = restore; }
33 void reset() { this->publish_and_save_(0.0f); }
34 void set_value(float value) { this->publish_and_save_(value); }
35
36 protected:
37 void process_sensor_value_(float value);
39 switch (this->time_) {
41 return 1.0f;
43 return 1.0f / 1000.0f;
45 return 1.0f / 60000.0f;
47 return 1.0f / 3600000.0f;
49 return 1.0f / 86400000.0f;
50 default:
51 return 0.0f;
52 }
53 }
54 void publish_and_save_(double result) {
55 this->result_ = result;
56 this->publish_state(result);
57 if (this->restore_) {
58 float result_f = result;
59 this->pref_.save(&result_f);
60 }
61 }
62
68
70 double result_{0.0f};
71 float last_value_{0.0f};
72};
73
74template<typename... Ts> class ResetAction : public Action<Ts...>, public Parented<IntegrationSensor> {
75 public:
76 void play(const Ts &...x) override { this->parent_->reset(); }
77};
78
79template<typename... Ts> class SetValueAction : public Action<Ts...>, public Parented<IntegrationSensor> {
80 public:
81 TEMPLATABLE_VALUE(float, value)
82
83 void play(const Ts &...x) override { this->parent_->set_value(this->value_.value(x...)); }
84};
85
86} // namespace esphome::integration
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
void set_time(IntegrationSensorTime time)
void set_method(IntegrationMethod method)
void play(const Ts &...x) override
TEMPLATABLE_VALUE(float, value) void play(const Ts &...x) override
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
static void uint32_t
uint16_t x
Definition tt21100.cpp:5