mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	wip
This commit is contained in:
		| @@ -20,6 +20,9 @@ from esphome.const import ( | ||||
| from .. import template_ns | ||||
|  | ||||
| TemplateValve = template_ns.class_("TemplateValve", valve.Valve, cg.Component) | ||||
| StatelessTemplateValve = template_ns.class_( | ||||
|     "StatelessTemplateValve", valve.Valve, cg.Component | ||||
| ) | ||||
|  | ||||
| TemplateValvePublishAction = template_ns.class_( | ||||
|     "TemplateValvePublishAction", automation.Action, cg.Parented.template(TemplateValve) | ||||
| @@ -62,13 +65,22 @@ CONFIG_SCHEMA = ( | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = await valve.new_valve(config) | ||||
|     await cg.register_component(var, config) | ||||
|     if lambda_config := config.get(CONF_LAMBDA): | ||||
|         # Use new_lambda_pvariable to create either TemplateValve or StatelessTemplateValve | ||||
|         template_ = await cg.process_lambda( | ||||
|             lambda_config, [], return_type=cg.optional.template(float) | ||||
|         ) | ||||
|         cg.add(var.set_state_lambda(template_)) | ||||
|         var = automation.new_lambda_pvariable( | ||||
|             config[CONF_ID], template_, StatelessTemplateValve | ||||
|         ) | ||||
|         # Manually register as valve since we didn't use new_valve | ||||
|         await valve.register_valve(var, config) | ||||
|         await cg.register_component(var, config) | ||||
|     else: | ||||
|         # No lambda - just create the base template valve | ||||
|         var = await valve.new_valve(config) | ||||
|         await cg.register_component(var, config) | ||||
|  | ||||
|     if open_action_config := config.get(CONF_OPEN_ACTION): | ||||
|         await automation.build_automation( | ||||
|             var.get_open_trigger(), [], open_action_config | ||||
|   | ||||
| @@ -8,14 +8,8 @@ using namespace esphome::valve; | ||||
|  | ||||
| static const char *const TAG = "template.valve"; | ||||
|  | ||||
| TemplateValve::TemplateValve() | ||||
|     : open_trigger_(new Trigger<>()), | ||||
|       close_trigger_(new Trigger<>), | ||||
|       stop_trigger_(new Trigger<>()), | ||||
|       toggle_trigger_(new Trigger<>()), | ||||
|       position_trigger_(new Trigger<float>()) {} | ||||
|  | ||||
| void TemplateValve::setup() { | ||||
| // Template instantiations | ||||
| template<typename F> void TemplateValveBase<F>::setup() { | ||||
|   switch (this->restore_mode_) { | ||||
|     case VALVE_NO_RESTORE: | ||||
|       break; | ||||
| @@ -35,35 +29,7 @@ void TemplateValve::setup() { | ||||
|   } | ||||
| } | ||||
|  | ||||
| void TemplateValve::loop() { | ||||
|   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 TemplateValve::set_optimistic(bool optimistic) { this->optimistic_ = optimistic; } | ||||
| void TemplateValve::set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; } | ||||
| void TemplateValve::set_state_lambda(std::function<optional<float>()> &&f) { this->state_f_ = f; } | ||||
| float TemplateValve::get_setup_priority() const { return setup_priority::HARDWARE; } | ||||
|  | ||||
| Trigger<> *TemplateValve::get_open_trigger() const { return this->open_trigger_; } | ||||
| Trigger<> *TemplateValve::get_close_trigger() const { return this->close_trigger_; } | ||||
| Trigger<> *TemplateValve::get_stop_trigger() const { return this->stop_trigger_; } | ||||
| Trigger<> *TemplateValve::get_toggle_trigger() const { return this->toggle_trigger_; } | ||||
|  | ||||
| void TemplateValve::dump_config() { | ||||
| template<typename F> void TemplateValveBase<F>::dump_config() { | ||||
|   LOG_VALVE("", "Template Valve", this); | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "  Has position: %s\n" | ||||
| @@ -71,7 +37,7 @@ void TemplateValve::dump_config() { | ||||
|                 YESNO(this->has_position_), YESNO(this->optimistic_)); | ||||
| } | ||||
|  | ||||
| void TemplateValve::control(const ValveCall &call) { | ||||
| template<typename F> void TemplateValveBase<F>::control(const ValveCall &call) { | ||||
|   if (call.get_stop()) { | ||||
|     this->stop_prev_trigger_(); | ||||
|     this->stop_trigger_->trigger(); | ||||
| @@ -106,7 +72,7 @@ void TemplateValve::control(const ValveCall &call) { | ||||
|   this->publish_state(); | ||||
| } | ||||
|  | ||||
| ValveTraits TemplateValve::get_traits() { | ||||
| template<typename F> valve::ValveTraits TemplateValveBase<F>::get_traits() { | ||||
|   auto traits = ValveTraits(); | ||||
|   traits.set_is_assumed_state(this->assumed_state_); | ||||
|   traits.set_supports_stop(this->has_stop_); | ||||
| @@ -115,18 +81,15 @@ ValveTraits TemplateValve::get_traits() { | ||||
|   return traits; | ||||
| } | ||||
|  | ||||
| Trigger<float> *TemplateValve::get_position_trigger() const { return this->position_trigger_; } | ||||
|  | ||||
| void TemplateValve::set_has_stop(bool has_stop) { this->has_stop_ = has_stop; } | ||||
| void TemplateValve::set_has_toggle(bool has_toggle) { this->has_toggle_ = has_toggle; } | ||||
| void TemplateValve::set_has_position(bool has_position) { this->has_position_ = has_position; } | ||||
|  | ||||
| void TemplateValve::stop_prev_trigger_() { | ||||
| template<typename F> void TemplateValveBase<F>::stop_prev_trigger_() { | ||||
|   if (this->prev_command_trigger_ != nullptr) { | ||||
|     this->prev_command_trigger_->stop_action(); | ||||
|     this->prev_command_trigger_ = nullptr; | ||||
|   } | ||||
| } | ||||
|  | ||||
| template class TemplateValveBase<std::function<optional<float>()>>; | ||||
| template class TemplateValveBase<optional<float> (*)()>; | ||||
|  | ||||
| }  // namespace template_ | ||||
| }  // namespace esphome | ||||
|   | ||||
		Reference in New Issue
	
	Block a user