Midea ir templatable code (#4053)

This commit is contained in:
Alex Reid 2022-11-22 18:53:48 -05:00 committed by GitHub
parent ef26677b67
commit 20da03f8c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View File

@ -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

View File

@ -1,9 +1,10 @@
#pragma once
#include <array>
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "remote_base.h"
#include <array>
#include <utility>
namespace esphome {
namespace remote_base {
@ -84,12 +85,23 @@ using MideaDumper = RemoteReceiverDumper<MideaProtocol, MideaData>;
template<typename... Ts> class MideaAction : public RemoteTransmitterActionBase<Ts...> {
TEMPLATABLE_VALUE(std::vector<uint8_t>, code)
void set_code(const std::vector<uint8_t> &code) { code_ = code; }
void set_code_static(std::vector<uint8_t> code) { code_static_ = std::move(code); }
void set_code_template(std::function<std::vector<uint8_t>(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<std::vector<uint8_t>(Ts...)> code_func_{};
std::vector<uint8_t> code_static_{};
};
} // namespace remote_base