ESPHome 2025.5.0
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 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};
50};
51
52template<typename... Ts> class BaseAction : public Action<Ts...>, public Parented<DutyTimeSensor> {};
53
54template<typename... Ts> class StartAction : public BaseAction<Ts...> {
55 void play(Ts... x) override { this->parent_->start(); }
56};
57
58template<typename... Ts> class StopAction : public BaseAction<Ts...> {
59 void play(Ts... x) override { this->parent_->stop(); }
60};
61
62template<typename... Ts> class ResetAction : public BaseAction<Ts...> {
63 void play(Ts... x) override { this->parent_->reset(); }
64};
65
66template<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
virtual void stop()
Definition automation.h:167
Base class for all automation conditions.
Definition automation.h:75
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
This class simplifies creating components that periodically check a state.
Definition component.h:301
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:57
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
bool state
Definition fan.h:0
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5