Tuya Number: allow restoring value of hidden datapoints (#7346)
Some checks are pending
CI / Run script/ci-custom (push) Blocked by required conditions
CI / Create common environment (push) Waiting to run
CI / Check black (push) Blocked by required conditions
CI / Check flake8 (push) Blocked by required conditions
CI / Check pylint (push) Blocked by required conditions
CI / Check pyupgrade (push) Blocked by required conditions
CI / Run pytest (macOS-latest, 3.11) (push) Blocked by required conditions
CI / Run pytest (ubuntu-latest, 3.10) (push) Blocked by required conditions
CI / Run pytest (ubuntu-latest, 3.11) (push) Blocked by required conditions
CI / Run pytest (ubuntu-latest, 3.12) (push) Blocked by required conditions
CI / Run pytest (ubuntu-latest, 3.9) (push) Blocked by required conditions
CI / Run pytest (windows-latest, 3.11) (push) Blocked by required conditions
CI / Check clang-format (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP32 IDF (push) Blocked by required conditions
CI / Run script/clang-tidy for ESP8266 (push) Blocked by required conditions
CI / list-components (push) Blocked by required conditions
CI / Component test ${{ matrix.file }} (push) Blocked by required conditions
CI / Split components for testing into 20 groups maximum (push) Blocked by required conditions
CI / Test split components (push) Blocked by required conditions
CI / CI Status (push) Blocked by required conditions
YAML lint / yamllint (push) Waiting to run

This commit is contained in:
Piotr Szulc 2024-08-30 02:53:34 +02:00 committed by GitHub
parent a5d46ae9e5
commit 721b532d71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 6 deletions

View file

@ -9,6 +9,7 @@ from esphome.const import (
CONF_MULTIPLY,
CONF_STEP,
CONF_INITIAL_VALUE,
CONF_RESTORE_VALUE,
)
from .. import tuya_ns, CONF_TUYA_ID, Tuya, TuyaDatapointType
@ -58,6 +59,7 @@ CONFIG_SCHEMA = cv.All(
DATAPOINT_TYPES, lower=True
),
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
cv.Optional(CONF_RESTORE_VALUE, default=False): cv.boolean,
}
)
),
@ -90,3 +92,4 @@ async def to_code(config):
hidden_init_value := hidden_config.get(CONF_INITIAL_VALUE, None)
) is not None:
cg.add(var.set_datapoint_initial_value(hidden_init_value))
cg.add(var.set_restore_value(hidden_config[CONF_RESTORE_VALUE]))

View file

@ -7,14 +7,28 @@ namespace tuya {
static const char *const TAG = "tuya.number";
void TuyaNumber::setup() {
if (this->restore_value_) {
this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
}
this->parent_->register_listener(this->number_id_, [this](const TuyaDatapoint &datapoint) {
if (datapoint.type == TuyaDatapointType::INTEGER) {
ESP_LOGV(TAG, "MCU reported number %u is: %d", datapoint.id, datapoint.value_int);
this->publish_state(datapoint.value_int / multiply_by_);
float value = datapoint.value_int / multiply_by_;
this->publish_state(value);
if (this->restore_value_)
this->pref_.save(&value);
} else if (datapoint.type == TuyaDatapointType::ENUM) {
ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum);
this->publish_state(datapoint.value_enum);
float value = datapoint.value_enum;
this->publish_state(value);
if (this->restore_value_)
this->pref_.save(&value);
} else {
ESP_LOGW(TAG, "Reported type (%d) is not a number!", static_cast<int>(datapoint.type));
return;
}
if ((this->type_) && (this->type_ != datapoint.type)) {
ESP_LOGW(TAG, "Reported type (%d) different than previously set (%d)!", static_cast<int>(datapoint.type),
static_cast<int>(*this->type_));
@ -23,8 +37,26 @@ void TuyaNumber::setup() {
});
this->parent_->add_on_initialized_callback([this] {
if ((this->initial_value_) && (this->type_)) {
this->control(*this->initial_value_);
if (this->type_) {
float value;
if (!this->restore_value_) {
if (this->initial_value_) {
value = *this->initial_value_;
} else {
return;
}
} else {
if (!this->pref_.load(&value)) {
if (this->initial_value_) {
value = *this->initial_value_;
} else {
value = this->traits.get_min_value();
ESP_LOGW(TAG, "Failed to restore and there is no initial value defined. Setting min_value (%f)", value);
}
}
}
this->control(value);
}
});
}
@ -38,6 +70,9 @@ void TuyaNumber::control(float value) {
this->parent_->set_enum_datapoint_value(this->number_id_, value);
}
this->publish_state(value);
if (this->restore_value_)
this->pref_.save(&value);
}
void TuyaNumber::dump_config() {
@ -52,6 +87,8 @@ void TuyaNumber::dump_config() {
if (this->initial_value_) {
ESP_LOGCONFIG(TAG, " Initial Value: %f", *this->initial_value_);
}
ESP_LOGCONFIG(TAG, " Restore Value: %s", YESNO(this->restore_value_));
}
} // namespace tuya

View file

@ -1,9 +1,10 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/tuya/tuya.h"
#include "esphome/components/number/number.h"
#include "esphome/components/tuya/tuya.h"
#include "esphome/core/component.h"
#include "esphome/core/optional.h"
#include "esphome/core/preferences.h"
namespace esphome {
namespace tuya {
@ -16,6 +17,7 @@ class TuyaNumber : public number::Number, public Component {
void set_write_multiply(float factor) { multiply_by_ = factor; }
void set_datapoint_type(TuyaDatapointType type) { type_ = type; }
void set_datapoint_initial_value(float value) { this->initial_value_ = value; }
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
@ -27,6 +29,9 @@ class TuyaNumber : public number::Number, public Component {
float multiply_by_{1.0};
optional<TuyaDatapointType> type_{};
optional<float> initial_value_{};
bool restore_value_{false};
ESPPreferenceObject pref_;
};
} // namespace tuya