Add support for string-type Tuya datapoints (#1488)

This commit is contained in:
Paul Nicholls 2021-01-26 17:44:10 +13:00 committed by Jesse Hills
parent b860a317b9
commit ea019a057b
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 18 additions and 1 deletions

View File

@ -42,6 +42,8 @@ void Tuya::dump_config() {
ESP_LOGCONFIG(TAG, " Datapoint %d: switch (value: %s)", info.id, ONOFF(info.value_bool));
else if (info.type == TuyaDatapointType::INTEGER)
ESP_LOGCONFIG(TAG, " Datapoint %d: int value (value: %d)", info.id, info.value_int);
else if (info.type == TuyaDatapointType::STRING)
ESP_LOGCONFIG(TAG, " Datapoint %d: string value (value: %s)", info.id, info.value_string.c_str());
else if (info.type == TuyaDatapointType::ENUM)
ESP_LOGCONFIG(TAG, " Datapoint %d: enum (value: %d)", info.id, info.value_enum);
else if (info.type == TuyaDatapointType::BITMASK)
@ -283,6 +285,9 @@ 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]);
break;
case TuyaDatapointType::STRING:
datapoint.value_string = std::string(reinterpret_cast<const char *>(data), data_len);
break;
case TuyaDatapointType::ENUM:
if (data_len != 1)
return;
@ -339,7 +344,13 @@ void Tuya::set_datapoint_value(TuyaDatapoint datapoint) {
ESP_LOGV(TAG, "Datapoint %u set to %u", datapoint.id, datapoint.value_uint);
for (auto &other : this->datapoints_) {
if (other.id == datapoint.id) {
if (other.value_uint == datapoint.value_uint) {
// String value is stored outside the union; must be checked separately.
if (datapoint.type == TuyaDatapointType::STRING) {
if (other.value_string == datapoint.value_string) {
ESP_LOGV(TAG, "Not sending unchanged value");
return;
}
} else if (other.value_uint == datapoint.value_uint) {
ESP_LOGV(TAG, "Not sending unchanged value");
return;
}
@ -359,6 +370,11 @@ void Tuya::set_datapoint_value(TuyaDatapoint datapoint) {
data.push_back(datapoint.value_uint >> 8);
data.push_back(datapoint.value_uint >> 0);
break;
case TuyaDatapointType::STRING:
for (char const &c : datapoint.value_string) {
data.push_back(c);
}
break;
case TuyaDatapointType::ENUM:
data.push_back(datapoint.value_enum);
break;

View File

@ -30,6 +30,7 @@ struct TuyaDatapoint {
uint8_t value_enum;
uint16_t value_bitmask;
};
std::string value_string;
};
struct TuyaDatapointListener {