ESPHome  2024.11.0
integration_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/hal.h"
8 
9 namespace esphome {
10 namespace integration {
11 
18 };
19 
24 };
25 
26 class IntegrationSensor : public sensor::Sensor, public Component {
27  public:
28  void setup() override;
29  void dump_config() override;
30  float get_setup_priority() const override { return setup_priority::DATA; }
32  void set_time(IntegrationSensorTime time) { time_ = time; }
33  void set_method(IntegrationMethod method) { method_ = method; }
34  void set_restore(bool restore) { restore_ = restore; }
35  void reset() { this->publish_and_save_(0.0f); }
36 
37  protected:
38  void process_sensor_value_(float value);
39  float get_time_factor_() {
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 
67  bool restore_;
69 
70  uint32_t last_update_;
71  double result_{0.0f};
72  float last_value_{0.0f};
73 };
74 
75 template<typename... Ts> class ResetAction : public Action<Ts...> {
76  public:
77  explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {}
78 
79  void play(Ts... x) override { this->parent_->reset(); }
80 
81  protected:
83 };
84 
85 } // namespace integration
86 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
uint16_t x
Definition: tt21100.cpp:17
bool save(const T *src)
Definition: preferences.h:21
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void set_time(IntegrationSensorTime time)
void set_method(IntegrationMethod method)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
ResetAction(IntegrationSensor *parent)