mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +01:00
Tuya Number: allow to set hidden datapoints (#7024)
Some checks are pending
CI / Check pylint (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 pyupgrade (push) Blocked by required conditions
CI / Run script/ci-custom (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 / Check clang-format (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 / 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
Some checks are pending
CI / Check pylint (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 pyupgrade (push) Blocked by required conditions
CI / Run script/ci-custom (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 / Check clang-format (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 / 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
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
11e155d866
commit
ab620acd4f
4 changed files with 61 additions and 3 deletions
|
@ -15,6 +15,7 @@ CONF_DATAPOINT_TYPE = "datapoint_type"
|
|||
CONF_STATUS_PIN = "status_pin"
|
||||
|
||||
tuya_ns = cg.esphome_ns.namespace("tuya")
|
||||
TuyaDatapointType = tuya_ns.enum("TuyaDatapointType", is_class=True)
|
||||
Tuya = tuya_ns.class_("Tuya", cg.Component, uart.UARTDevice)
|
||||
|
||||
DPTYPE_ANY = "any"
|
||||
|
|
|
@ -8,18 +8,36 @@ from esphome.const import (
|
|||
CONF_MIN_VALUE,
|
||||
CONF_MULTIPLY,
|
||||
CONF_STEP,
|
||||
CONF_INITIAL_VALUE,
|
||||
)
|
||||
from .. import tuya_ns, CONF_TUYA_ID, Tuya
|
||||
from .. import tuya_ns, CONF_TUYA_ID, Tuya, TuyaDatapointType
|
||||
|
||||
DEPENDENCIES = ["tuya"]
|
||||
CODEOWNERS = ["@frankiboy1"]
|
||||
|
||||
CONF_DATAPOINT_HIDDEN = "datapoint_hidden"
|
||||
CONF_DATAPOINT_TYPE = "datapoint_type"
|
||||
|
||||
TuyaNumber = tuya_ns.class_("TuyaNumber", number.Number, cg.Component)
|
||||
|
||||
DATAPOINT_TYPES = {
|
||||
"int": TuyaDatapointType.INTEGER,
|
||||
"uint": TuyaDatapointType.INTEGER,
|
||||
"enum": TuyaDatapointType.ENUM,
|
||||
}
|
||||
|
||||
|
||||
def validate_min_max(config):
|
||||
if config[CONF_MAX_VALUE] <= config[CONF_MIN_VALUE]:
|
||||
max_value = config[CONF_MAX_VALUE]
|
||||
min_value = config[CONF_MIN_VALUE]
|
||||
if max_value <= min_value:
|
||||
raise cv.Invalid("max_value must be greater than min_value")
|
||||
if hidden_config := config.get(CONF_DATAPOINT_HIDDEN):
|
||||
if (initial_value := hidden_config.get(CONF_INITIAL_VALUE, None)) is not None:
|
||||
if (initial_value > max_value) or (initial_value < min_value):
|
||||
raise cv.Invalid(
|
||||
f"{CONF_INITIAL_VALUE} must be a value between {CONF_MAX_VALUE} and {CONF_MIN_VALUE}"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
|
@ -33,6 +51,16 @@ CONFIG_SCHEMA = cv.All(
|
|||
cv.Required(CONF_MIN_VALUE): cv.float_,
|
||||
cv.Required(CONF_STEP): cv.positive_float,
|
||||
cv.Optional(CONF_MULTIPLY, default=1.0): cv.float_,
|
||||
cv.Optional(CONF_DATAPOINT_HIDDEN): cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_DATAPOINT_TYPE): cv.enum(
|
||||
DATAPOINT_TYPES, lower=True
|
||||
),
|
||||
cv.Optional(CONF_INITIAL_VALUE): cv.float_,
|
||||
}
|
||||
)
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA),
|
||||
|
@ -56,3 +84,9 @@ async def to_code(config):
|
|||
cg.add(var.set_tuya_parent(parent))
|
||||
|
||||
cg.add(var.set_number_id(config[CONF_NUMBER_DATAPOINT]))
|
||||
if hidden_config := config.get(CONF_DATAPOINT_HIDDEN):
|
||||
cg.add(var.set_datapoint_type(hidden_config[CONF_DATAPOINT_TYPE]))
|
||||
if (
|
||||
hidden_init_value := hidden_config.get(CONF_INITIAL_VALUE, None)
|
||||
) is not None:
|
||||
cg.add(var.set_datapoint_initial_value(hidden_init_value))
|
||||
|
|
|
@ -15,8 +15,18 @@ void TuyaNumber::setup() {
|
|||
ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum);
|
||||
this->publish_state(datapoint.value_enum);
|
||||
}
|
||||
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_));
|
||||
}
|
||||
this->type_ = datapoint.type;
|
||||
});
|
||||
|
||||
this->parent_->add_on_initialized_callback([this] {
|
||||
if ((this->initial_value_) && (this->type_)) {
|
||||
this->control(*this->initial_value_);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TuyaNumber::control(float value) {
|
||||
|
@ -33,6 +43,15 @@ void TuyaNumber::control(float value) {
|
|||
void TuyaNumber::dump_config() {
|
||||
LOG_NUMBER("", "Tuya Number", this);
|
||||
ESP_LOGCONFIG(TAG, " Number has datapoint ID %u", this->number_id_);
|
||||
if (this->type_) {
|
||||
ESP_LOGCONFIG(TAG, " Datapoint type is %d", static_cast<int>(*this->type_));
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Datapoint type is unknown");
|
||||
}
|
||||
|
||||
if (this->initial_value_) {
|
||||
ESP_LOGCONFIG(TAG, " Initial Value: %f", *this->initial_value_);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tuya
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/tuya/tuya.h"
|
||||
#include "esphome/components/number/number.h"
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tuya {
|
||||
|
@ -13,6 +14,8 @@ class TuyaNumber : public number::Number, public Component {
|
|||
void dump_config() override;
|
||||
void set_number_id(uint8_t number_id) { this->number_id_ = number_id; }
|
||||
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_tuya_parent(Tuya *parent) { this->parent_ = parent; }
|
||||
|
||||
|
@ -22,7 +25,8 @@ class TuyaNumber : public number::Number, public Component {
|
|||
Tuya *parent_;
|
||||
uint8_t number_id_{0};
|
||||
float multiply_by_{1.0};
|
||||
TuyaDatapointType type_{};
|
||||
optional<TuyaDatapointType> type_{};
|
||||
optional<float> initial_value_{};
|
||||
};
|
||||
|
||||
} // namespace tuya
|
||||
|
|
Loading…
Reference in a new issue