mirror of
https://github.com/esphome/esphome.git
synced 2025-01-04 18:47:43 +01:00
Tuya Number: allow restoring value of hidden datapoints (#7346)
This commit is contained in:
parent
a5d46ae9e5
commit
721b532d71
@ -9,6 +9,7 @@ from esphome.const import (
|
|||||||
CONF_MULTIPLY,
|
CONF_MULTIPLY,
|
||||||
CONF_STEP,
|
CONF_STEP,
|
||||||
CONF_INITIAL_VALUE,
|
CONF_INITIAL_VALUE,
|
||||||
|
CONF_RESTORE_VALUE,
|
||||||
)
|
)
|
||||||
from .. import tuya_ns, CONF_TUYA_ID, Tuya, TuyaDatapointType
|
from .. import tuya_ns, CONF_TUYA_ID, Tuya, TuyaDatapointType
|
||||||
|
|
||||||
@ -58,6 +59,7 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
DATAPOINT_TYPES, lower=True
|
DATAPOINT_TYPES, lower=True
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
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)
|
hidden_init_value := hidden_config.get(CONF_INITIAL_VALUE, None)
|
||||||
) is not None:
|
) is not None:
|
||||||
cg.add(var.set_datapoint_initial_value(hidden_init_value))
|
cg.add(var.set_datapoint_initial_value(hidden_init_value))
|
||||||
|
cg.add(var.set_restore_value(hidden_config[CONF_RESTORE_VALUE]))
|
||||||
|
@ -7,14 +7,28 @@ namespace tuya {
|
|||||||
static const char *const TAG = "tuya.number";
|
static const char *const TAG = "tuya.number";
|
||||||
|
|
||||||
void TuyaNumber::setup() {
|
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) {
|
this->parent_->register_listener(this->number_id_, [this](const TuyaDatapoint &datapoint) {
|
||||||
if (datapoint.type == TuyaDatapointType::INTEGER) {
|
if (datapoint.type == TuyaDatapointType::INTEGER) {
|
||||||
ESP_LOGV(TAG, "MCU reported number %u is: %d", datapoint.id, datapoint.value_int);
|
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) {
|
} else if (datapoint.type == TuyaDatapointType::ENUM) {
|
||||||
ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_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)) {
|
if ((this->type_) && (this->type_ != datapoint.type)) {
|
||||||
ESP_LOGW(TAG, "Reported type (%d) different than previously set (%d)!", static_cast<int>(datapoint.type),
|
ESP_LOGW(TAG, "Reported type (%d) different than previously set (%d)!", static_cast<int>(datapoint.type),
|
||||||
static_cast<int>(*this->type_));
|
static_cast<int>(*this->type_));
|
||||||
@ -23,8 +37,26 @@ void TuyaNumber::setup() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this->parent_->add_on_initialized_callback([this] {
|
this->parent_->add_on_initialized_callback([this] {
|
||||||
if ((this->initial_value_) && (this->type_)) {
|
if (this->type_) {
|
||||||
this->control(*this->initial_value_);
|
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->parent_->set_enum_datapoint_value(this->number_id_, value);
|
||||||
}
|
}
|
||||||
this->publish_state(value);
|
this->publish_state(value);
|
||||||
|
|
||||||
|
if (this->restore_value_)
|
||||||
|
this->pref_.save(&value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TuyaNumber::dump_config() {
|
void TuyaNumber::dump_config() {
|
||||||
@ -52,6 +87,8 @@ void TuyaNumber::dump_config() {
|
|||||||
if (this->initial_value_) {
|
if (this->initial_value_) {
|
||||||
ESP_LOGCONFIG(TAG, " Initial Value: %f", *this->initial_value_);
|
ESP_LOGCONFIG(TAG, " Initial Value: %f", *this->initial_value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGCONFIG(TAG, " Restore Value: %s", YESNO(this->restore_value_));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace tuya
|
} // namespace tuya
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
|
||||||
#include "esphome/components/tuya/tuya.h"
|
|
||||||
#include "esphome/components/number/number.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/optional.h"
|
||||||
|
#include "esphome/core/preferences.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace tuya {
|
namespace tuya {
|
||||||
@ -16,6 +17,7 @@ class TuyaNumber : public number::Number, public Component {
|
|||||||
void set_write_multiply(float factor) { multiply_by_ = factor; }
|
void set_write_multiply(float factor) { multiply_by_ = factor; }
|
||||||
void set_datapoint_type(TuyaDatapointType type) { type_ = type; }
|
void set_datapoint_type(TuyaDatapointType type) { type_ = type; }
|
||||||
void set_datapoint_initial_value(float value) { this->initial_value_ = value; }
|
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; }
|
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};
|
float multiply_by_{1.0};
|
||||||
optional<TuyaDatapointType> type_{};
|
optional<TuyaDatapointType> type_{};
|
||||||
optional<float> initial_value_{};
|
optional<float> initial_value_{};
|
||||||
|
bool restore_value_{false};
|
||||||
|
|
||||||
|
ESPPreferenceObject pref_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace tuya
|
} // namespace tuya
|
||||||
|
Loading…
Reference in New Issue
Block a user