ESPHome  2024.11.0
duty_time_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cinttypes>
4 
9 #ifdef USE_BINARY_SENSOR
11 #endif
12 
13 namespace esphome {
14 namespace duty_time_sensor {
15 
17  public:
18  void setup() override;
19  void update() override;
20  void loop() override;
21  void dump_config() override;
22  float get_setup_priority() const override { return setup_priority::DATA; }
23 
24  void start();
25  void stop();
26  bool is_running() const { return this->last_state_; }
27  void reset() { this->set_value_(0); }
28 
29 #ifdef USE_BINARY_SENSOR
31 #endif
32  void set_lambda(std::function<bool()> &&func) { this->func_ = func; }
34  void set_restore(bool restore) { this->restore_ = restore; }
35 
36  protected:
37  void set_value_(uint32_t sec);
38  void process_state_(bool state);
39  void publish_and_save_(uint32_t sec, uint32_t ms);
40 
41  std::function<bool()> func_{nullptr};
44 
45  uint32_t total_sec_;
46  uint32_t last_time_;
47  uint32_t edge_time_;
48  bool last_state_{false};
49  bool restore_;
50 };
51 
52 template<typename... Ts> class BaseAction : public Action<Ts...>, public Parented<DutyTimeSensor> {};
53 
54 template<typename... Ts> class StartAction : public BaseAction<Ts...> {
55  void play(Ts... x) override { this->parent_->start(); }
56 };
57 
58 template<typename... Ts> class StopAction : public BaseAction<Ts...> {
59  void play(Ts... x) override { this->parent_->stop(); }
60 };
61 
62 template<typename... Ts> class ResetAction : public BaseAction<Ts...> {
63  void play(Ts... x) override { this->parent_->reset(); }
64 };
65 
66 template<typename... Ts> class RunningCondition : public Condition<Ts...>, public Parented<DutyTimeSensor> {
67  public:
68  explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {}
69 
70  protected:
71  bool check(Ts... x) override { return this->parent_->is_running() == this->state_; }
72  bool state_;
73 };
74 
75 } // namespace duty_time_sensor
76 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
virtual void stop()
Definition: automation.h:166
void set_last_duty_time_sensor(sensor::Sensor *sensor)
uint16_t x
Definition: tt21100.cpp:17
This class simplifies creating components that periodically check a state.
Definition: component.h:283
RunningCondition(DutyTimeSensor *parent, bool state)
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void set_lambda(std::function< bool()> &&func)
Base class for all automation conditions.
Definition: automation.h:74
void publish_and_save_(uint32_t sec, uint32_t ms)
void set_sensor(binary_sensor::BinarySensor *sensor)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522