Support for Haier IR protocol added (#5403)

This commit is contained in:
Pavlo Dudnytskyi 2023-10-08 22:49:55 +02:00 committed by GitHub
parent ee4ccf2762
commit 7e7c83b3ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 163 additions and 0 deletions

View file

@ -1558,3 +1558,37 @@ async def aeha_action(var, config, args):
config[CONF_DATA], args, cg.std_vector.template(cg.uint8)
)
cg.add(var.set_data(template_))
# Haier
HaierData, HaierBinarySensor, HaierTrigger, HaierAction, HaierDumper = declare_protocol(
"Haier"
)
HaierAction = ns.class_("HaierAction", RemoteTransmitterActionBase)
HAIER_SCHEMA = cv.Schema(
{
cv.Required(CONF_CODE): cv.All([cv.hex_uint8_t], cv.Length(min=13, max=13)),
}
)
@register_binary_sensor("haier", HaierBinarySensor, HAIER_SCHEMA)
def haier_binary_sensor(var, config):
cg.add(var.set_code(config[CONF_CODE]))
@register_trigger("haier", HaierTrigger, HaierData)
def haier_trigger(var, config):
pass
@register_dumper("haier", HaierDumper)
def haier_dumper(var, config):
pass
@register_action("haier", HaierAction, HAIER_SCHEMA)
async def haier_action(var, config, args):
vec_ = cg.std_vector.template(cg.uint8)
template_ = await cg.templatable(config[CONF_CODE], args, vec_, vec_)
cg.add(var.set_code(template_))

View file

@ -0,0 +1,84 @@
#include "haier_protocol.h"
#include "esphome/core/log.h"
namespace esphome {
namespace remote_base {
static const char *const TAG = "remote.haier";
constexpr uint32_t HEADER_LOW_US = 3100;
constexpr uint32_t HEADER_HIGH_US = 4400;
constexpr uint32_t BIT_MARK_US = 540;
constexpr uint32_t BIT_ONE_SPACE_US = 1650;
constexpr uint32_t BIT_ZERO_SPACE_US = 580;
constexpr unsigned int HAIER_IR_PACKET_BIT_SIZE = 112;
void HaierProtocol::encode_byte_(RemoteTransmitData *dst, uint8_t item) {
for (uint8_t mask = 1 << 7; mask != 0; mask >>= 1) {
if (item & mask) {
dst->space(BIT_ONE_SPACE_US);
} else {
dst->space(BIT_ZERO_SPACE_US);
}
dst->mark(BIT_MARK_US);
}
}
void HaierProtocol::encode(RemoteTransmitData *dst, const HaierData &data) {
dst->set_carrier_frequency(38000);
dst->reserve(5 + ((data.data.size() + 1) * 2));
dst->mark(HEADER_LOW_US);
dst->space(HEADER_LOW_US);
dst->mark(HEADER_LOW_US);
dst->space(HEADER_HIGH_US);
dst->mark(BIT_MARK_US);
uint8_t checksum = 0;
for (uint8_t item : data.data) {
this->encode_byte_(dst, item);
checksum += item;
}
this->encode_byte_(dst, checksum);
}
optional<HaierData> HaierProtocol::decode(RemoteReceiveData src) {
if (!src.expect_item(HEADER_LOW_US, HEADER_LOW_US) || !src.expect_item(HEADER_LOW_US, HEADER_HIGH_US)) {
return {};
}
if (!src.expect_mark(BIT_MARK_US)) {
return {};
}
size_t size = src.size() - src.get_index() - 1;
if (size < HAIER_IR_PACKET_BIT_SIZE * 2)
return {};
size = HAIER_IR_PACKET_BIT_SIZE * 2;
uint8_t checksum = 0;
HaierData out;
while (size > 0) {
uint8_t data = 0;
for (uint8_t mask = 0x80; mask != 0; mask >>= 1) {
if (src.expect_space(BIT_ONE_SPACE_US)) {
data |= mask;
} else if (!src.expect_space(BIT_ZERO_SPACE_US)) {
return {};
}
if (!src.expect_mark(BIT_MARK_US)) {
return {};
}
size -= 2;
}
if (size > 0) {
checksum += data;
out.data.push_back(data);
} else if (checksum != data) {
return {};
}
}
return out;
}
void HaierProtocol::dump(const HaierData &data) {
ESP_LOGI(TAG, "Received Haier: %s", format_hex_pretty(data.data).c_str());
}
} // namespace remote_base
} // namespace esphome

View file

@ -0,0 +1,40 @@
#pragma once
#include "remote_base.h"
#include <vector>
namespace esphome {
namespace remote_base {
struct HaierData {
std::vector<uint8_t> data;
bool operator==(const HaierData &rhs) const { return data == rhs.data; }
};
class HaierProtocol : public RemoteProtocol<HaierData> {
public:
void encode(RemoteTransmitData *dst, const HaierData &data) override;
optional<HaierData> decode(RemoteReceiveData src) override;
void dump(const HaierData &data) override;
protected:
void encode_byte_(RemoteTransmitData *dst, uint8_t item);
};
DECLARE_REMOTE_PROTOCOL(Haier)
template<typename... Ts> class HaierAction : public RemoteTransmitterActionBase<Ts...> {
public:
TEMPLATABLE_VALUE(std::vector<uint8_t>, data)
void set_code(const std::vector<uint8_t> &code) { data_ = code; }
void encode(RemoteTransmitData *dst, Ts... x) override {
HaierData data{};
data.data = this->data_.value(x...);
HaierProtocol().encode(dst, data);
}
};
} // namespace remote_base
} // namespace esphome

View file

@ -2622,6 +2622,11 @@ switch:
0x00,
0xFF,
]
- platform: template
name: Haier
turn_on_action:
remote_transmitter.transmit_haier:
code: [0xA6, 0xDA, 0x00, 0x00, 0x40, 0x40, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x05]
- platform: template
name: Living Room Lights
id: livingroom_lights