mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Implement output button (#3109)
This commit is contained in:
		
							
								
								
									
										26
									
								
								esphome/components/output/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								esphome/components/output/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | ||||
| import esphome.codegen as cg | ||||
| import esphome.config_validation as cv | ||||
| from esphome.components import button, output | ||||
| from esphome.const import CONF_ID, CONF_OUTPUT, CONF_DURATION | ||||
| from .. import output_ns | ||||
|  | ||||
| OutputButton = output_ns.class_("OutputButton", button.Button, cg.Component) | ||||
|  | ||||
| CONFIG_SCHEMA = button.BUTTON_SCHEMA.extend( | ||||
|     { | ||||
|         cv.GenerateID(): cv.declare_id(OutputButton), | ||||
|         cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), | ||||
|         cv.Required(CONF_DURATION): cv.positive_time_period_milliseconds, | ||||
|     } | ||||
| ).extend(cv.COMPONENT_SCHEMA) | ||||
|  | ||||
|  | ||||
| async def to_code(config): | ||||
|     var = cg.new_Pvariable(config[CONF_ID]) | ||||
|     cg.add(var.set_duration(config[CONF_DURATION])) | ||||
|  | ||||
|     output_ = await cg.get_variable(config[CONF_OUTPUT]) | ||||
|     cg.add(var.set_output(output_)) | ||||
|  | ||||
|     await cg.register_component(var, config) | ||||
|     await button.register_button(var, config) | ||||
							
								
								
									
										21
									
								
								esphome/components/output/button/output_button.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esphome/components/output/button/output_button.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| #include "output_button.h" | ||||
| #include "esphome/core/log.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace output { | ||||
|  | ||||
| static const char *const TAG = "output.button"; | ||||
|  | ||||
| void OutputButton::dump_config() { | ||||
|   LOG_BUTTON("", "Output Button", this); | ||||
|   ESP_LOGCONFIG(TAG, "  Duration: %.1fs", this->duration_ / 1e3f); | ||||
| } | ||||
| void OutputButton::press_action() { | ||||
|   this->output_->turn_on(); | ||||
|  | ||||
|   // Use a named timeout so that it's automatically cancelled if button is pressed again before it's reset | ||||
|   this->set_timeout("reset", this->duration_, [this]() { this->output_->turn_off(); }); | ||||
| } | ||||
|  | ||||
| }  // namespace output | ||||
| }  // namespace esphome | ||||
							
								
								
									
										25
									
								
								esphome/components/output/button/output_button.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								esphome/components/output/button/output_button.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "esphome/core/component.h" | ||||
| #include "esphome/components/button/button.h" | ||||
| #include "esphome/components/output/binary_output.h" | ||||
|  | ||||
| namespace esphome { | ||||
| namespace output { | ||||
|  | ||||
| class OutputButton : public button::Button, public Component { | ||||
|  public: | ||||
|   void dump_config() override; | ||||
|  | ||||
|   void set_output(BinaryOutput *output) { output_ = output; } | ||||
|   void set_duration(uint32_t duration) { duration_ = duration; } | ||||
|  | ||||
|  protected: | ||||
|   void press_action() override; | ||||
|  | ||||
|   output::BinaryOutput *output_; | ||||
|   uint32_t duration_; | ||||
| }; | ||||
|  | ||||
| }  // namespace output | ||||
| }  // namespace esphome | ||||
| @@ -1352,6 +1352,10 @@ daly_bms: | ||||
|   uart_id: uart1 | ||||
|  | ||||
| button: | ||||
|   - platform: output | ||||
|     id: output_button | ||||
|     output: out | ||||
|     duration: 100ms | ||||
|   - platform: wake_on_lan | ||||
|     target_mac_address: 12:34:56:78:90:ab | ||||
|     name: wol_test_1 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user