Support raw datapoints for tuya components (#1669)

This commit is contained in:
Stefan Rado 2021-06-03 03:10:29 +02:00 committed by GitHub
parent 2b9350ce76
commit 913ac8b7e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View file

@ -31,7 +31,9 @@ void Tuya::dump_config() {
return;
}
for (auto &info : this->datapoints_) {
if (info.type == TuyaDatapointType::BOOLEAN)
if (info.type == TuyaDatapointType::RAW)
ESP_LOGCONFIG(TAG, " Datapoint %u: raw (value: %s)", info.id, hexencode(info.value_raw).c_str());
else if (info.type == TuyaDatapointType::BOOLEAN)
ESP_LOGCONFIG(TAG, " Datapoint %u: switch (value: %s)", info.id, ONOFF(info.value_bool));
else if (info.type == TuyaDatapointType::INTEGER)
ESP_LOGCONFIG(TAG, " Datapoint %u: int value (value: %d)", info.id, info.value_int);
@ -236,12 +238,17 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) {
datapoint.len = data_len;
switch (datapoint.type) {
case TuyaDatapointType::RAW:
datapoint.value_raw = std::vector<uint8_t>(data, data + data_len);
ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, hexencode(datapoint.value_raw).c_str());
break;
case TuyaDatapointType::BOOLEAN:
if (data_len != 1) {
ESP_LOGW(TAG, "Datapoint %u has bad boolean len %zu", datapoint.id, data_len);
return;
}
datapoint.value_bool = data[0];
ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, ONOFF(datapoint.value_bool));
break;
case TuyaDatapointType::INTEGER:
if (data_len != 4) {
@ -249,9 +256,11 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) {
return;
}
datapoint.value_uint = encode_uint32(data[0], data[1], data[2], data[3]);
ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_int);
break;
case TuyaDatapointType::STRING:
datapoint.value_string = std::string(reinterpret_cast<const char *>(data), data_len);
ESP_LOGD(TAG, "Datapoint %u update to %s", datapoint.id, datapoint.value_string.c_str());
break;
case TuyaDatapointType::ENUM:
if (data_len != 1) {
@ -259,6 +268,7 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) {
return;
}
datapoint.value_enum = data[0];
ESP_LOGD(TAG, "Datapoint %u update to %d", datapoint.id, datapoint.value_enum);
break;
case TuyaDatapointType::BITMASK:
switch (data_len) {
@ -275,12 +285,12 @@ void Tuya::handle_datapoint_(const uint8_t *buffer, size_t len) {
ESP_LOGW(TAG, "Datapoint %u has bad bitmask len %zu", datapoint.id, data_len);
return;
}
ESP_LOGD(TAG, "Datapoint %u update to %#08X", datapoint.id, datapoint.value_bitmask);
break;
default:
ESP_LOGW(TAG, "Datapoint %u has unknown type 0x%02hhX", datapoint.id, datapoint.type);
ESP_LOGW(TAG, "Datapoint %u has unknown type %#02hhX", datapoint.id, datapoint.type);
return;
}
ESP_LOGD(TAG, "Datapoint %u update to %u", datapoint.id, datapoint.value_uint);
// Update internal datapoints
bool found = false;

View file

@ -32,6 +32,7 @@ struct TuyaDatapoint {
uint32_t value_bitmask;
};
std::string value_string;
std::vector<uint8_t> value_raw;
};
struct TuyaDatapointListener {