mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	wip
This commit is contained in:
		| @@ -13,28 +13,48 @@ enum TemplateValveRestoreMode { | ||||
|   VALVE_RESTORE_AND_CALL, | ||||
| }; | ||||
|  | ||||
| class TemplateValve : public valve::Valve, public Component { | ||||
| template<typename F> class TemplateValveBase : public valve::Valve, public Component { | ||||
|  public: | ||||
|   TemplateValve(); | ||||
|   TemplateValveBase() | ||||
|       : open_trigger_(new Trigger<>()), | ||||
|         close_trigger_(new Trigger<>()), | ||||
|         stop_trigger_(new Trigger<>()), | ||||
|         toggle_trigger_(new Trigger<>()), | ||||
|         position_trigger_(new Trigger<float>()) {} | ||||
|  | ||||
|   void set_state_lambda(std::function<optional<float>()> &&f); | ||||
|   Trigger<> *get_open_trigger() const; | ||||
|   Trigger<> *get_close_trigger() const; | ||||
|   Trigger<> *get_stop_trigger() const; | ||||
|   Trigger<> *get_toggle_trigger() const; | ||||
|   Trigger<float> *get_position_trigger() const; | ||||
|   void set_optimistic(bool optimistic); | ||||
|   void set_assumed_state(bool assumed_state); | ||||
|   void set_has_stop(bool has_stop); | ||||
|   void set_has_position(bool has_position); | ||||
|   void set_has_toggle(bool has_toggle); | ||||
|   void set_restore_mode(TemplateValveRestoreMode restore_mode) { restore_mode_ = restore_mode; } | ||||
|   void loop() override { | ||||
|     bool changed = false; | ||||
|  | ||||
|     if (this->state_f_.has_value()) { | ||||
|       auto s = (*this->state_f_)(); | ||||
|       if (s.has_value()) { | ||||
|         auto pos = clamp(*s, 0.0f, 1.0f); | ||||
|         if (pos != this->position) { | ||||
|           this->position = pos; | ||||
|           changed = true; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     if (changed) | ||||
|       this->publish_state(); | ||||
|   } | ||||
|  | ||||
|   void setup() override; | ||||
|   void loop() override; | ||||
|   void dump_config() override; | ||||
|   float get_setup_priority() const override { return setup_priority::HARDWARE; } | ||||
|  | ||||
|   float get_setup_priority() const override; | ||||
|   Trigger<> *get_open_trigger() const { return this->open_trigger_; } | ||||
|   Trigger<> *get_close_trigger() const { return this->close_trigger_; } | ||||
|   Trigger<> *get_stop_trigger() const { return this->stop_trigger_; } | ||||
|   Trigger<> *get_toggle_trigger() const { return this->toggle_trigger_; } | ||||
|   Trigger<float> *get_position_trigger() const { return this->position_trigger_; } | ||||
|   void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } | ||||
|   void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; } | ||||
|   void set_has_stop(bool has_stop) { this->has_stop_ = has_stop; } | ||||
|   void set_has_position(bool has_position) { this->has_position_ = has_position; } | ||||
|   void set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; } | ||||
|   void set_restore_mode(TemplateValveRestoreMode restore_mode) { restore_mode_ = restore_mode; } | ||||
|  | ||||
|  protected: | ||||
|   void control(const valve::ValveCall &call) override; | ||||
| @@ -42,7 +62,7 @@ class TemplateValve : public valve::Valve, public Component { | ||||
|   void stop_prev_trigger_(); | ||||
|  | ||||
|   TemplateValveRestoreMode restore_mode_{VALVE_NO_RESTORE}; | ||||
|   optional<std::function<optional<float>()>> state_f_; | ||||
|   optional<F> state_f_; | ||||
|   bool assumed_state_{false}; | ||||
|   bool optimistic_{false}; | ||||
|   Trigger<> *open_trigger_; | ||||
| @@ -56,5 +76,20 @@ class TemplateValve : public valve::Valve, public Component { | ||||
|   bool has_position_{false}; | ||||
| }; | ||||
|  | ||||
| class TemplateValve : public TemplateValveBase<std::function<optional<float>()>> { | ||||
|  public: | ||||
|   void set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; } | ||||
| }; | ||||
|  | ||||
| /** Optimized template valve for stateless lambdas (no capture). | ||||
|  * | ||||
|  * Uses function pointers instead of std::function to reduce memory overhead. | ||||
|  * Memory: 4 bytes (function pointer on 32-bit) vs 32 bytes (std::function) per lambda. | ||||
|  */ | ||||
| class StatelessTemplateValve : public TemplateValveBase<optional<float> (*)()> { | ||||
|  public: | ||||
|   explicit StatelessTemplateValve(optional<float> (*f)()) { this->state_f_ = f; } | ||||
| }; | ||||
|  | ||||
| }  // namespace template_ | ||||
| }  // namespace esphome | ||||
|   | ||||
		Reference in New Issue
	
	Block a user