ESPHome  2021.10.2
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; }
31  void set_min_save_interval(uint32_t min_interval) { this->min_save_interval_ = min_interval; }
32  void set_sensor(Sensor *sensor) { sensor_ = sensor; }
33  void set_time(IntegrationSensorTime time) { time_ = time; }
34  void set_method(IntegrationMethod method) { method_ = method; }
35  void set_restore(bool restore) { restore_ = restore; }
36  void reset() { this->publish_and_save_(0.0f); }
37 
38  protected:
39  void process_sensor_value_(float value);
40  float get_time_factor_() {
41  switch (this->time_) {
43  return 1.0f;
45  return 1.0f / 1000.0f;
47  return 1.0f / 60000.0f;
49  return 1.0f / 3600000.0f;
51  return 1.0f / 86400000.0f;
52  default:
53  return 0.0f;
54  }
55  }
56  void publish_and_save_(double result) {
57  this->result_ = result;
58  this->publish_state(result);
59  float result_f = result;
60  const uint32_t now = millis();
61  if (now - this->last_save_ < this->min_save_interval_)
62  return;
63  this->last_save_ = now;
64  this->rtc_.save(&result_f);
65  }
66  std::string unit_of_measurement() override;
67  int8_t accuracy_decimals() override { return this->sensor_->get_accuracy_decimals() + 2; }
68 
72  bool restore_;
74 
75  uint32_t last_save_{0};
76  uint32_t min_save_interval_{0};
77  uint32_t last_update_;
78  double result_{0.0f};
79  float last_value_{0.0f};
80 };
81 
82 template<typename... Ts> class ResetAction : public Action<Ts...> {
83  public:
84  explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {}
85 
86  void play(Ts... x) override { this->parent_->reset(); }
87 
88  protected:
90 };
91 
92 } // namespace integration
93 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
void set_min_save_interval(uint32_t min_interval)
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:21
bool save(const T *src)
Definition: preferences.h:20
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:58
void set_time(IntegrationSensorTime time)
void set_method(IntegrationMethod method)
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
Definition: sensor.cpp:34
Base-class for all sensors.
Definition: sensor.h:47
ResetAction(IntegrationSensor *parent)