mirror of
https://github.com/esphome/esphome.git
synced 2025-07-29 14:46:40 +00:00
Tuya Select - Add int_datapoint option (#8393)
This commit is contained in:
parent
5e164b107a
commit
8b65d1673a
@ -1,7 +1,12 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.components import select
|
from esphome.components import select
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_ENUM_DATAPOINT, CONF_OPTIMISTIC, CONF_OPTIONS
|
from esphome.const import (
|
||||||
|
CONF_ENUM_DATAPOINT,
|
||||||
|
CONF_INT_DATAPOINT,
|
||||||
|
CONF_OPTIMISTIC,
|
||||||
|
CONF_OPTIONS,
|
||||||
|
)
|
||||||
|
|
||||||
from .. import CONF_TUYA_ID, Tuya, tuya_ns
|
from .. import CONF_TUYA_ID, Tuya, tuya_ns
|
||||||
|
|
||||||
@ -26,17 +31,19 @@ def ensure_option_map(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = cv.All(
|
||||||
select.select_schema(TuyaSelect)
|
select.select_schema(TuyaSelect)
|
||||||
.extend(
|
.extend(
|
||||||
{
|
{
|
||||||
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
|
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
|
||||||
cv.Required(CONF_ENUM_DATAPOINT): cv.uint8_t,
|
cv.Optional(CONF_ENUM_DATAPOINT): cv.uint8_t,
|
||||||
|
cv.Optional(CONF_INT_DATAPOINT): cv.uint8_t,
|
||||||
cv.Required(CONF_OPTIONS): ensure_option_map,
|
cv.Required(CONF_OPTIONS): ensure_option_map,
|
||||||
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA),
|
||||||
|
cv.has_exactly_one_key(CONF_ENUM_DATAPOINT, CONF_INT_DATAPOINT),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -47,5 +54,8 @@ async def to_code(config):
|
|||||||
cg.add(var.set_select_mappings(list(options_map.keys())))
|
cg.add(var.set_select_mappings(list(options_map.keys())))
|
||||||
parent = await cg.get_variable(config[CONF_TUYA_ID])
|
parent = await cg.get_variable(config[CONF_TUYA_ID])
|
||||||
cg.add(var.set_tuya_parent(parent))
|
cg.add(var.set_tuya_parent(parent))
|
||||||
cg.add(var.set_select_id(config[CONF_ENUM_DATAPOINT]))
|
if enum_datapoint := config.get(CONF_ENUM_DATAPOINT, None) is not None:
|
||||||
|
cg.add(var.set_select_id(enum_datapoint, False))
|
||||||
|
if int_datapoint := config.get(CONF_INT_DATAPOINT, None) is not None:
|
||||||
|
cg.add(var.set_select_id(int_datapoint, True))
|
||||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||||
|
@ -31,7 +31,11 @@ void TuyaSelect::control(const std::string &value) {
|
|||||||
if (idx.has_value()) {
|
if (idx.has_value()) {
|
||||||
uint8_t mapping = this->mappings_.at(idx.value());
|
uint8_t mapping = this->mappings_.at(idx.value());
|
||||||
ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, value.c_str());
|
ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, value.c_str());
|
||||||
this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
|
if (this->is_int_) {
|
||||||
|
this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
|
||||||
|
} else {
|
||||||
|
this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +45,7 @@ void TuyaSelect::control(const std::string &value) {
|
|||||||
void TuyaSelect::dump_config() {
|
void TuyaSelect::dump_config() {
|
||||||
LOG_SELECT("", "Tuya Select", this);
|
LOG_SELECT("", "Tuya Select", this);
|
||||||
ESP_LOGCONFIG(TAG, " Select has datapoint ID %u", this->select_id_);
|
ESP_LOGCONFIG(TAG, " Select has datapoint ID %u", this->select_id_);
|
||||||
|
ESP_LOGCONFIG(TAG, " Data type: %s", this->is_int_ ? "int" : "enum");
|
||||||
ESP_LOGCONFIG(TAG, " Options are:");
|
ESP_LOGCONFIG(TAG, " Options are:");
|
||||||
auto options = this->traits.get_options();
|
auto options = this->traits.get_options();
|
||||||
for (auto i = 0; i < this->mappings_.size(); i++) {
|
for (auto i = 0; i < this->mappings_.size(); i++) {
|
||||||
|
@ -16,7 +16,10 @@ class TuyaSelect : public select::Select, public Component {
|
|||||||
|
|
||||||
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
|
void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
|
||||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||||
void set_select_id(uint8_t select_id) { this->select_id_ = select_id; }
|
void set_select_id(uint8_t select_id, bool is_int) {
|
||||||
|
this->select_id_ = select_id;
|
||||||
|
this->is_int_ = is_int;
|
||||||
|
}
|
||||||
void set_select_mappings(std::vector<uint8_t> mappings) { this->mappings_ = std::move(mappings); }
|
void set_select_mappings(std::vector<uint8_t> mappings) { this->mappings_ = std::move(mappings); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -26,6 +29,7 @@ class TuyaSelect : public select::Select, public Component {
|
|||||||
bool optimistic_ = false;
|
bool optimistic_ = false;
|
||||||
uint8_t select_id_;
|
uint8_t select_id_;
|
||||||
std::vector<uint8_t> mappings_;
|
std::vector<uint8_t> mappings_;
|
||||||
|
bool is_int_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace tuya
|
} // namespace tuya
|
||||||
|
@ -408,6 +408,7 @@ CONF_INITIAL_OPTION = "initial_option"
|
|||||||
CONF_INITIAL_STATE = "initial_state"
|
CONF_INITIAL_STATE = "initial_state"
|
||||||
CONF_INITIAL_VALUE = "initial_value"
|
CONF_INITIAL_VALUE = "initial_value"
|
||||||
CONF_INPUT = "input"
|
CONF_INPUT = "input"
|
||||||
|
CONF_INT_DATAPOINT = "int_datapoint"
|
||||||
CONF_INTEGRATION_TIME = "integration_time"
|
CONF_INTEGRATION_TIME = "integration_time"
|
||||||
CONF_INTENSITY = "intensity"
|
CONF_INTENSITY = "intensity"
|
||||||
CONF_INTERLOCK = "interlock"
|
CONF_INTERLOCK = "interlock"
|
||||||
|
@ -60,12 +60,19 @@ number:
|
|||||||
|
|
||||||
select:
|
select:
|
||||||
- platform: tuya
|
- platform: tuya
|
||||||
id: tuya_select
|
id: tuya_select_enum
|
||||||
enum_datapoint: 42
|
enum_datapoint: 42
|
||||||
options:
|
options:
|
||||||
0: Internal
|
0: Internal
|
||||||
1: Floor
|
1: Floor
|
||||||
2: Both
|
2: Both
|
||||||
|
- platform: tuya
|
||||||
|
id: tuya_select_int
|
||||||
|
int_datapoint: 43
|
||||||
|
options:
|
||||||
|
0: Internal
|
||||||
|
1: Floor
|
||||||
|
2: Both
|
||||||
|
|
||||||
sensor:
|
sensor:
|
||||||
- platform: tuya
|
- platform: tuya
|
||||||
|
Loading…
x
Reference in New Issue
Block a user