ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
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
13namespace esphome {
14namespace duty_time_sensor {
15
17 public:
18 void setup() override;
19 void update() override;
20 void loop() override;
21 void dump_config() override;
22
23 void start();
24 void stop();
25 bool is_running() const { return this->last_state_; }
26 void reset() { this->set_value_(0); }
27
28#ifdef USE_BINARY_SENSOR
30#endif
31 void set_lambda(std::function<bool()> &&func) { this->func_ = func; }
33 void set_restore(bool restore) { this->restore_ = restore; }
34
35 protected:
36 void set_value_(uint32_t sec);
37 void process_state_(bool state);
38 void publish_and_save_(uint32_t sec, uint32_t ms);
39
40 std::function<bool()> func_{nullptr};
43
44 uint32_t total_sec_;
45 uint32_t last_time_;
46 uint32_t edge_time_;
47 bool last_state_{false};
49};
50
51template<typename... Ts> class BaseAction : public Action<Ts...>, public Parented<DutyTimeSensor> {};
52
53template<typename... Ts> class StartAction : public BaseAction<Ts...> {
54 void play(Ts... x) override { this->parent_->start(); }
55};
56
57template<typename... Ts> class StopAction : public BaseAction<Ts...> {
58 void play(Ts... x) override { this->parent_->stop(); }
59};
60
61template<typename... Ts> class ResetAction : public BaseAction<Ts...> {
62 void play(Ts... x) override { this->parent_->reset(); }
63};
64
65template<typename... Ts> class RunningCondition : public Condition<Ts...>, public Parented<DutyTimeSensor> {
66 public:
67 explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {}
68
69 protected:
70 bool check(Ts... x) override { return this->parent_->is_running() == this->state_; }
71 bool state_;
72};
73
74} // namespace duty_time_sensor
75} // namespace esphome
virtual void stop()
Definition automation.h:216
Base class for all automation conditions.
Definition automation.h:124
Helper class to easily give an object a parent of type T.
Definition helpers.h:656
This class simplifies creating components that periodically check a state.
Definition component.h:425
Base class for all binary_sensor-type classes.
void publish_and_save_(uint32_t sec, uint32_t ms)
void set_lambda(std::function< bool()> &&func)
void set_sensor(binary_sensor::BinarySensor *sensor)
void set_last_duty_time_sensor(sensor::Sensor *sensor)
RunningCondition(DutyTimeSensor *parent, bool state)
Base-class for all sensors.
Definition sensor.h:59
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:133
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5