mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Tuya Number: allow to set hidden datapoints (#7024)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -15,6 +15,7 @@ CONF_DATAPOINT_TYPE = "datapoint_type" | |||||||
| CONF_STATUS_PIN = "status_pin" | CONF_STATUS_PIN = "status_pin" | ||||||
|  |  | ||||||
| tuya_ns = cg.esphome_ns.namespace("tuya") | tuya_ns = cg.esphome_ns.namespace("tuya") | ||||||
|  | TuyaDatapointType = tuya_ns.enum("TuyaDatapointType", is_class=True) | ||||||
| Tuya = tuya_ns.class_("Tuya", cg.Component, uart.UARTDevice) | Tuya = tuya_ns.class_("Tuya", cg.Component, uart.UARTDevice) | ||||||
|  |  | ||||||
| DPTYPE_ANY = "any" | DPTYPE_ANY = "any" | ||||||
|   | |||||||
| @@ -8,18 +8,36 @@ from esphome.const import ( | |||||||
|     CONF_MIN_VALUE, |     CONF_MIN_VALUE, | ||||||
|     CONF_MULTIPLY, |     CONF_MULTIPLY, | ||||||
|     CONF_STEP, |     CONF_STEP, | ||||||
|  |     CONF_INITIAL_VALUE, | ||||||
| ) | ) | ||||||
| from .. import tuya_ns, CONF_TUYA_ID, Tuya | from .. import tuya_ns, CONF_TUYA_ID, Tuya, TuyaDatapointType | ||||||
|  |  | ||||||
| DEPENDENCIES = ["tuya"] | DEPENDENCIES = ["tuya"] | ||||||
| CODEOWNERS = ["@frankiboy1"] | CODEOWNERS = ["@frankiboy1"] | ||||||
|  |  | ||||||
|  | CONF_DATAPOINT_HIDDEN = "datapoint_hidden" | ||||||
|  | CONF_DATAPOINT_TYPE = "datapoint_type" | ||||||
|  |  | ||||||
| TuyaNumber = tuya_ns.class_("TuyaNumber", number.Number, cg.Component) | 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): | 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") |         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 |     return config | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -33,6 +51,16 @@ CONFIG_SCHEMA = cv.All( | |||||||
|             cv.Required(CONF_MIN_VALUE): cv.float_, |             cv.Required(CONF_MIN_VALUE): cv.float_, | ||||||
|             cv.Required(CONF_STEP): cv.positive_float, |             cv.Required(CONF_STEP): cv.positive_float, | ||||||
|             cv.Optional(CONF_MULTIPLY, default=1.0): cv.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), |     .extend(cv.COMPONENT_SCHEMA), | ||||||
| @@ -56,3 +84,9 @@ async def to_code(config): | |||||||
|     cg.add(var.set_tuya_parent(parent)) |     cg.add(var.set_tuya_parent(parent)) | ||||||
|  |  | ||||||
|     cg.add(var.set_number_id(config[CONF_NUMBER_DATAPOINT])) |     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); |       ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum); | ||||||
|       this->publish_state(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->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) { | void TuyaNumber::control(float value) { | ||||||
| @@ -33,6 +43,15 @@ void TuyaNumber::control(float value) { | |||||||
| void TuyaNumber::dump_config() { | void TuyaNumber::dump_config() { | ||||||
|   LOG_NUMBER("", "Tuya Number", this); |   LOG_NUMBER("", "Tuya Number", this); | ||||||
|   ESP_LOGCONFIG(TAG, "  Number has datapoint ID %u", this->number_id_); |   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 | }  // namespace tuya | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "esphome/components/tuya/tuya.h" | #include "esphome/components/tuya/tuya.h" | ||||||
| #include "esphome/components/number/number.h" | #include "esphome/components/number/number.h" | ||||||
|  | #include "esphome/core/optional.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace tuya { | namespace tuya { | ||||||
| @@ -13,6 +14,8 @@ class TuyaNumber : public number::Number, public Component { | |||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   void set_number_id(uint8_t number_id) { this->number_id_ = number_id; } |   void set_number_id(uint8_t number_id) { this->number_id_ = number_id; } | ||||||
|   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_initial_value(float value) { this->initial_value_ = value; } | ||||||
|  |  | ||||||
|   void set_tuya_parent(Tuya *parent) { this->parent_ = parent; } |   void set_tuya_parent(Tuya *parent) { this->parent_ = parent; } | ||||||
|  |  | ||||||
| @@ -22,7 +25,8 @@ class TuyaNumber : public number::Number, public Component { | |||||||
|   Tuya *parent_; |   Tuya *parent_; | ||||||
|   uint8_t number_id_{0}; |   uint8_t number_id_{0}; | ||||||
|   float multiply_by_{1.0}; |   float multiply_by_{1.0}; | ||||||
|   TuyaDatapointType type_{}; |   optional<TuyaDatapointType> type_{}; | ||||||
|  |   optional<float> initial_value_{}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace tuya | }  // namespace tuya | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user