ESPHome 2026.5.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 {
10namespace integration {
11
19
25
27 public:
28 void setup() override;
29 void dump_config() override;
30 void set_sensor(Sensor *sensor) { sensor_ = sensor; }
31 void set_time(IntegrationSensorTime time) { time_ = time; }
32 void set_method(IntegrationMethod method) { method_ = method; }
33 void set_restore(bool restore) { restore_ = restore; }
34 void reset() { this->publish_and_save_(0.0f); }
35 void set_value(float value) { this->publish_and_save_(value); }
36
37 protected:
38 void process_sensor_value_(float value);
40 switch (this->time_) {
42 return 1.0f;
44 return 1.0f / 1000.0f;
46 return 1.0f / 60000.0f;
48 return 1.0f / 3600000.0f;
50 return 1.0f / 86400000.0f;
51 default:
52 return 0.0f;
53 }
54 }
55 void publish_and_save_(double result) {
56 this->result_ = result;
57 this->publish_state(result);
58 if (this->restore_) {
59 float result_f = result;
60 this->pref_.save(&result_f);
61 }
62 }
63
69
71 double result_{0.0f};
72 float last_value_{0.0f};
73};
74
75template<typename... Ts> class ResetAction : public Action<Ts...>, public Parented<IntegrationSensor> {
76 public:
77 void play(const Ts &...x) override { this->parent_->reset(); }
78};
79
80template<typename... Ts> class SetValueAction : public Action<Ts...>, public Parented<IntegrationSensor> {
81 public:
82 TEMPLATABLE_VALUE(float, value)
83
84 void play(const Ts &...x) override { this->parent_->set_value(this->value_.value(x...)); }
85};
86
87} // namespace integration
88} // namespace esphome
virtual void play(const Ts &...x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:2013
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
uint16_t x
Definition tt21100.cpp:5