mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[remote_transmitter] Add digital_write automation (#10069)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -13,6 +13,7 @@ from esphome.const import ( | |||||||
|     CONF_PIN, |     CONF_PIN, | ||||||
|     CONF_RMT_SYMBOLS, |     CONF_RMT_SYMBOLS, | ||||||
|     CONF_USE_DMA, |     CONF_USE_DMA, | ||||||
|  |     CONF_VALUE, | ||||||
|     PlatformFramework, |     PlatformFramework, | ||||||
| ) | ) | ||||||
| from esphome.core import CORE | from esphome.core import CORE | ||||||
| @@ -22,11 +23,17 @@ AUTO_LOAD = ["remote_base"] | |||||||
| CONF_EOT_LEVEL = "eot_level" | CONF_EOT_LEVEL = "eot_level" | ||||||
| CONF_ON_TRANSMIT = "on_transmit" | CONF_ON_TRANSMIT = "on_transmit" | ||||||
| CONF_ON_COMPLETE = "on_complete" | CONF_ON_COMPLETE = "on_complete" | ||||||
|  | CONF_TRANSMITTER_ID = remote_base.CONF_TRANSMITTER_ID | ||||||
|  |  | ||||||
| remote_transmitter_ns = cg.esphome_ns.namespace("remote_transmitter") | remote_transmitter_ns = cg.esphome_ns.namespace("remote_transmitter") | ||||||
| RemoteTransmitterComponent = remote_transmitter_ns.class_( | RemoteTransmitterComponent = remote_transmitter_ns.class_( | ||||||
|     "RemoteTransmitterComponent", remote_base.RemoteTransmitterBase, cg.Component |     "RemoteTransmitterComponent", remote_base.RemoteTransmitterBase, cg.Component | ||||||
| ) | ) | ||||||
|  | DigitalWriteAction = remote_transmitter_ns.class_( | ||||||
|  |     "DigitalWriteAction", | ||||||
|  |     automation.Action, | ||||||
|  |     cg.Parented.template(RemoteTransmitterComponent), | ||||||
|  | ) | ||||||
|  |  | ||||||
| MULTI_CONF = True | MULTI_CONF = True | ||||||
| CONFIG_SCHEMA = cv.Schema( | CONFIG_SCHEMA = cv.Schema( | ||||||
| @@ -63,6 +70,25 @@ CONFIG_SCHEMA = cv.Schema( | |||||||
|     } |     } | ||||||
| ).extend(cv.COMPONENT_SCHEMA) | ).extend(cv.COMPONENT_SCHEMA) | ||||||
|  |  | ||||||
|  | DIGITAL_WRITE_ACTION_SCHEMA = cv.maybe_simple_value( | ||||||
|  |     { | ||||||
|  |         cv.GenerateID(CONF_TRANSMITTER_ID): cv.use_id(RemoteTransmitterComponent), | ||||||
|  |         cv.Required(CONF_VALUE): cv.templatable(cv.boolean), | ||||||
|  |     }, | ||||||
|  |     key=CONF_VALUE, | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @automation.register_action( | ||||||
|  |     "remote_transmitter.digital_write", DigitalWriteAction, DIGITAL_WRITE_ACTION_SCHEMA | ||||||
|  | ) | ||||||
|  | async def digital_write_action_to_code(config, action_id, template_arg, args): | ||||||
|  |     var = cg.new_Pvariable(action_id, template_arg) | ||||||
|  |     await cg.register_parented(var, config[CONF_TRANSMITTER_ID]) | ||||||
|  |     template_ = await cg.templatable(config[CONF_VALUE], args, bool) | ||||||
|  |     cg.add(var.set_value(template_)) | ||||||
|  |     return var | ||||||
|  |  | ||||||
|  |  | ||||||
| async def to_code(config): | async def to_code(config): | ||||||
|     pin = await cg.gpio_pin_expression(config[CONF_PIN]) |     pin = await cg.gpio_pin_expression(config[CONF_PIN]) | ||||||
|   | |||||||
							
								
								
									
										18
									
								
								esphome/components/remote_transmitter/automation.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								esphome/components/remote_transmitter/automation.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "esphome/components/remote_transmitter/remote_transmitter.h" | ||||||
|  | #include "esphome/core/automation.h" | ||||||
|  | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/core/helpers.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | namespace remote_transmitter { | ||||||
|  |  | ||||||
|  | template<typename... Ts> class DigitalWriteAction : public Action<Ts...>, public Parented<RemoteTransmitterComponent> { | ||||||
|  |  public: | ||||||
|  |   TEMPLATABLE_VALUE(bool, value) | ||||||
|  |   void play(Ts... x) override { this->parent_->digital_write(this->value_.value(x...)); } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace remote_transmitter | ||||||
|  | }  // namespace esphome | ||||||
| @@ -30,10 +30,11 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase, | |||||||
|  |  | ||||||
|   void set_carrier_duty_percent(uint8_t carrier_duty_percent) { this->carrier_duty_percent_ = carrier_duty_percent; } |   void set_carrier_duty_percent(uint8_t carrier_duty_percent) { this->carrier_duty_percent_ = carrier_duty_percent; } | ||||||
|  |  | ||||||
|  |   void digital_write(bool value); | ||||||
|  |  | ||||||
| #if defined(USE_ESP32) | #if defined(USE_ESP32) | ||||||
|   void set_with_dma(bool with_dma) { this->with_dma_ = with_dma; } |   void set_with_dma(bool with_dma) { this->with_dma_ = with_dma; } | ||||||
|   void set_eot_level(bool eot_level) { this->eot_level_ = eot_level; } |   void set_eot_level(bool eot_level) { this->eot_level_ = eot_level; } | ||||||
|   void digital_write(bool value); |  | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   Trigger<> *get_transmit_trigger() const { return this->transmit_trigger_; }; |   Trigger<> *get_transmit_trigger() const { return this->transmit_trigger_; }; | ||||||
|   | |||||||
| @@ -73,6 +73,8 @@ void RemoteTransmitterComponent::space_(uint32_t usec) { | |||||||
|   this->target_time_ += usec; |   this->target_time_ += usec; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void RemoteTransmitterComponent::digital_write(bool value) { this->pin_->digital_write(value); } | ||||||
|  |  | ||||||
| void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | ||||||
|   ESP_LOGD(TAG, "Sending remote code"); |   ESP_LOGD(TAG, "Sending remote code"); | ||||||
|   uint32_t on_time, off_time; |   uint32_t on_time, off_time; | ||||||
|   | |||||||
| @@ -75,6 +75,8 @@ void RemoteTransmitterComponent::space_(uint32_t usec) { | |||||||
|   this->target_time_ += usec; |   this->target_time_ += usec; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void RemoteTransmitterComponent::digital_write(bool value) { this->pin_->digital_write(value); } | ||||||
|  |  | ||||||
| void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) { | ||||||
|   ESP_LOGD(TAG, "Sending remote code"); |   ESP_LOGD(TAG, "Sending remote code"); | ||||||
|   uint32_t on_time, off_time; |   uint32_t on_time, off_time; | ||||||
|   | |||||||
| @@ -204,3 +204,9 @@ button: | |||||||
|           command: 0xEC |           command: 0xEC | ||||||
|           rc_code_1: 0x0D |           rc_code_1: 0x0D | ||||||
|           rc_code_2: 0x0D |           rc_code_2: 0x0D | ||||||
|  |   - platform: template | ||||||
|  |     name: Digital Write | ||||||
|  |     on_press: | ||||||
|  |       - remote_transmitter.digital_write: true | ||||||
|  |       - remote_transmitter.digital_write: | ||||||
|  |           value: false | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user