From 20da03f8c6c0a6b3c50d4902a6481dc689116c71 Mon Sep 17 00:00:00 2001 From: Alex Reid Date: Tue, 22 Nov 2022 18:53:48 -0500 Subject: [PATCH] Midea ir templatable code (#4053) --- esphome/components/remote_base/__init__.py | 15 +++++++++++---- .../components/remote_base/midea_protocol.h | 18 +++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/esphome/components/remote_base/__init__.py b/esphome/components/remote_base/__init__.py index e4d1e115e7..b979a050db 100644 --- a/esphome/components/remote_base/__init__.py +++ b/esphome/components/remote_base/__init__.py @@ -1308,9 +1308,11 @@ MideaData, MideaBinarySensor, MideaTrigger, MideaAction, MideaDumper = declare_p MideaAction = ns.class_("MideaAction", RemoteTransmitterActionBase) MIDEA_SCHEMA = cv.Schema( { - cv.Required(CONF_CODE): cv.All( - [cv.Any(cv.hex_uint8_t, cv.uint8_t)], - cv.Length(min=5, max=5), + cv.Required(CONF_CODE): cv.templatable( + cv.All( + [cv.Any(cv.hex_uint8_t, cv.uint8_t)], + cv.Length(min=5, max=5), + ) ), } ) @@ -1337,7 +1339,12 @@ def midea_dumper(var, config): MIDEA_SCHEMA, ) async def midea_action(var, config, args): - cg.add(var.set_code(config[CONF_CODE])) + code_ = config[CONF_CODE] + if cg.is_template(code_): + template_ = await cg.templatable(code_, args, cg.std_vector.template(cg.uint8)) + cg.add(var.set_code_template(template_)) + else: + cg.add(var.set_code_static(code_)) # AEHA diff --git a/esphome/components/remote_base/midea_protocol.h b/esphome/components/remote_base/midea_protocol.h index 135a93b36d..2bcaa879c4 100644 --- a/esphome/components/remote_base/midea_protocol.h +++ b/esphome/components/remote_base/midea_protocol.h @@ -1,9 +1,10 @@ #pragma once -#include #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include "remote_base.h" +#include +#include namespace esphome { namespace remote_base { @@ -84,12 +85,23 @@ using MideaDumper = RemoteReceiverDumper; template class MideaAction : public RemoteTransmitterActionBase { TEMPLATABLE_VALUE(std::vector, code) - void set_code(const std::vector &code) { code_ = code; } + void set_code_static(std::vector code) { code_static_ = std::move(code); } + void set_code_template(std::function(Ts...)> func) { this->code_func_ = func; } + void encode(RemoteTransmitData *dst, Ts... x) override { - MideaData data = this->code_.value(x...); + MideaData data; + if (!this->code_static_.empty()) { + data = MideaData(this->code_static_); + } else { + data = MideaData(this->code_func_(x...)); + } data.finalize(); MideaProtocol().encode(dst, data); } + + protected: + std::function(Ts...)> code_func_{}; + std::vector code_static_{}; }; } // namespace remote_base