ESPHome  2024.11.0
demo_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace demo {
9 
10 class DemoSensor : public sensor::Sensor, public PollingComponent {
11  public:
12  void update() override {
13  float val = random_float();
14  bool increasing = this->get_state_class() == sensor::STATE_CLASS_TOTAL_INCREASING;
15  if (increasing) {
16  float base = std::isnan(this->state) ? 0.0f : this->state;
17  this->publish_state(base + val * 10);
18  } else {
19  if (val < 0.1) {
20  this->publish_state(NAN);
21  } else {
22  this->publish_state(val * 100);
23  }
24  }
25  }
26 };
27 
28 } // namespace demo
29 } // namespace esphome
mopeka_std_values val[4]
This class simplifies creating components that periodically check a state.
Definition: component.h:283
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void update() override
Definition: demo_sensor.h:12
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
StateClass get_state_class()
Get the state class, using the manual override if set.
Definition: sensor.cpp:33
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
float random_float()
Return a random float between 0 and 1.
Definition: helpers.cpp:215