mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	specify data types
This commit is contained in:
		| @@ -83,7 +83,8 @@ void HttpRequestUpdate::update_task(void *params) { | ||||
|     container.reset();  // Release ownership of the container's shared_ptr | ||||
|  | ||||
|     valid = json::parse_json(response, [this_update](JsonObject root) -> bool { | ||||
|       if (!root["name"].is<JsonVariant>() || !root["version"].is<JsonVariant>() || !root["builds"].is<JsonVariant>()) { | ||||
|       if (!root["name"].is<const char *>() || !root["version"].is<const char *>() || | ||||
|           !root["builds"].is<const char *>()) { | ||||
|         ESP_LOGE(TAG, "Manifest does not contain required fields"); | ||||
|         return false; | ||||
|       } | ||||
| @@ -91,26 +92,26 @@ void HttpRequestUpdate::update_task(void *params) { | ||||
|       this_update->update_info_.latest_version = root["version"].as<std::string>(); | ||||
|  | ||||
|       for (auto build : root["builds"].as<JsonArray>()) { | ||||
|         if (!build["chipFamily"].is<JsonVariant>()) { | ||||
|         if (!build["chipFamily"].is<const char *>()) { | ||||
|           ESP_LOGE(TAG, "Manifest does not contain required fields"); | ||||
|           return false; | ||||
|         } | ||||
|         if (build["chipFamily"] == ESPHOME_VARIANT) { | ||||
|           if (!build["ota"].is<JsonVariant>()) { | ||||
|           if (!build["ota"].is<JsonObject>()) { | ||||
|             ESP_LOGE(TAG, "Manifest does not contain required fields"); | ||||
|             return false; | ||||
|           } | ||||
|           JsonObject ota = build["ota"].as<JsonObject>(); | ||||
|           if (!ota["path"].is<JsonVariant>() || !ota["md5"].is<JsonVariant>()) { | ||||
|           if (!ota["path"].is<const char *>() || !ota["md5"].is<const char *>()) { | ||||
|             ESP_LOGE(TAG, "Manifest does not contain required fields"); | ||||
|             return false; | ||||
|           } | ||||
|           this_update->update_info_.firmware_url = ota["path"].as<std::string>(); | ||||
|           this_update->update_info_.md5 = ota["md5"].as<std::string>(); | ||||
|  | ||||
|           if (ota["summary"].is<JsonVariant>()) | ||||
|           if (ota["summary"].is<const char *>()) | ||||
|             this_update->update_info_.summary = ota["summary"].as<std::string>(); | ||||
|           if (ota["release_url"].is<JsonVariant>()) | ||||
|           if (ota["release_url"].is<const char *>()) | ||||
|             this_update->update_info_.release_url = ota["release_url"].as<std::string>(); | ||||
|  | ||||
|           return true; | ||||
|   | ||||
| @@ -73,7 +73,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) { | ||||
| } | ||||
|  | ||||
| void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonObject root) { | ||||
|   if (root["state"].is<JsonVariant>()) { | ||||
|   if (root["state"].is<const char *>()) { | ||||
|     auto val = parse_on_off(root["state"]); | ||||
|     switch (val) { | ||||
|       case PARSE_ON: | ||||
| @@ -90,40 +90,40 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   if (root["brightness"].is<JsonVariant>()) { | ||||
|   if (root["brightness"].is<unsigned char>()) { | ||||
|     call.set_brightness(float(root["brightness"]) / 255.0f); | ||||
|   } | ||||
|  | ||||
|   if (root["color"].is<JsonVariant>()) { | ||||
|   if (root["color"].is<JsonObject>()) { | ||||
|     JsonObject color = root["color"]; | ||||
|     // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness. | ||||
|     float max_rgb = 0.0f; | ||||
|     if (color["r"].is<JsonVariant>()) { | ||||
|     if (color["r"].is<unsigned char>()) { | ||||
|       float r = float(color["r"]) / 255.0f; | ||||
|       max_rgb = fmaxf(max_rgb, r); | ||||
|       call.set_red(r); | ||||
|     } | ||||
|     if (color["g"].is<JsonVariant>()) { | ||||
|     if (color["g"].is<unsigned char>()) { | ||||
|       float g = float(color["g"]) / 255.0f; | ||||
|       max_rgb = fmaxf(max_rgb, g); | ||||
|       call.set_green(g); | ||||
|     } | ||||
|     if (color["b"].is<JsonVariant>()) { | ||||
|     if (color["b"].is<unsigned char>()) { | ||||
|       float b = float(color["b"]) / 255.0f; | ||||
|       max_rgb = fmaxf(max_rgb, b); | ||||
|       call.set_blue(b); | ||||
|     } | ||||
|     if (color["r"].is<JsonVariant>() || color["g"].is<JsonVariant>() || color["b"].is<JsonVariant>()) { | ||||
|     if (color["r"].is<unsigned char>() || color["g"].is<unsigned char>() || color["b"].is<unsigned char>()) { | ||||
|       call.set_color_brightness(max_rgb); | ||||
|     } | ||||
|  | ||||
|     if (color["c"].is<JsonVariant>()) { | ||||
|     if (color["c"].is<unsigned char>()) { | ||||
|       call.set_cold_white(float(color["c"]) / 255.0f); | ||||
|     } | ||||
|     if (color["w"].is<JsonVariant>()) { | ||||
|     if (color["w"].is<unsigned short>()) { | ||||
|       // the HA scheme is ambiguous here, the same key is used for white channel in RGBW and warm | ||||
|       // white channel in RGBWW. | ||||
|       if (color["c"].is<JsonVariant>()) { | ||||
|       if (color["c"].is<unsigned short>()) { | ||||
|         call.set_warm_white(float(color["w"]) / 255.0f); | ||||
|       } else { | ||||
|         call.set_white(float(color["w"]) / 255.0f); | ||||
| @@ -131,11 +131,11 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   if (root["white_value"].is<JsonVariant>()) {  // legacy API | ||||
|   if (root["white_value"].is<unsigned short>()) {  // legacy API | ||||
|     call.set_white(float(root["white_value"]) / 255.0f); | ||||
|   } | ||||
|  | ||||
|   if (root["color_temp"].is<JsonVariant>()) { | ||||
|   if (root["color_temp"].is<unsigned short>()) { | ||||
|     call.set_color_temperature(float(root["color_temp"])); | ||||
|   } | ||||
| } | ||||
| @@ -143,17 +143,17 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO | ||||
| void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject root) { | ||||
|   LightJSONSchema::parse_color_json(state, call, root); | ||||
|  | ||||
|   if (root["flash"].is<JsonVariant>()) { | ||||
|   if (root["flash"].is<unsigned int>()) { | ||||
|     auto length = uint32_t(float(root["flash"]) * 1000); | ||||
|     call.set_flash_length(length); | ||||
|   } | ||||
|  | ||||
|   if (root["transition"].is<JsonVariant>()) { | ||||
|   if (root["transition"].is<unsigned short>()) { | ||||
|     auto length = uint32_t(float(root["transition"]) * 1000); | ||||
|     call.set_transition_length(length); | ||||
|   } | ||||
|  | ||||
|   if (root["effect"].is<JsonVariant>()) { | ||||
|   if (root["effect"].is<const char *>()) { | ||||
|     const char *effect = root["effect"]; | ||||
|     call.set_effect(effect); | ||||
|   } | ||||
|   | ||||
| @@ -20,13 +20,13 @@ MQTTDateComponent::MQTTDateComponent(DateEntity *date) : date_(date) {} | ||||
| void MQTTDateComponent::setup() { | ||||
|   this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) { | ||||
|     auto call = this->date_->make_call(); | ||||
|     if (root["year"].is<JsonVariant>()) { | ||||
|     if (root["year"].is<unsigned short>()) { | ||||
|       call.set_year(root["year"]); | ||||
|     } | ||||
|     if (root["month"].is<JsonVariant>()) { | ||||
|     if (root["month"].is<unsigned char>()) { | ||||
|       call.set_month(root["month"]); | ||||
|     } | ||||
|     if (root["day"].is<JsonVariant>()) { | ||||
|     if (root["day"].is<unsigned short>()) { | ||||
|       call.set_day(root["day"]); | ||||
|     } | ||||
|     call.perform(); | ||||
|   | ||||
| @@ -20,22 +20,22 @@ MQTTDateTimeComponent::MQTTDateTimeComponent(DateTimeEntity *datetime) : datetim | ||||
| void MQTTDateTimeComponent::setup() { | ||||
|   this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) { | ||||
|     auto call = this->datetime_->make_call(); | ||||
|     if (root["year"].is<JsonVariant>()) { | ||||
|     if (root["year"].is<unsigned short>()) { | ||||
|       call.set_year(root["year"]); | ||||
|     } | ||||
|     if (root["month"].is<JsonVariant>()) { | ||||
|     if (root["month"].is<unsigned char>()) { | ||||
|       call.set_month(root["month"]); | ||||
|     } | ||||
|     if (root["day"].is<JsonVariant>()) { | ||||
|     if (root["day"].is<unsigned char>()) { | ||||
|       call.set_day(root["day"]); | ||||
|     } | ||||
|     if (root["hour"].is<JsonVariant>()) { | ||||
|     if (root["hour"].is<unsigned short>()) { | ||||
|       call.set_hour(root["hour"]); | ||||
|     } | ||||
|     if (root["minute"].is<JsonVariant>()) { | ||||
|     if (root["minute"].is<unsigned short>()) { | ||||
|       call.set_minute(root["minute"]); | ||||
|     } | ||||
|     if (root["second"].is<JsonVariant>()) { | ||||
|     if (root["second"].is<unsigned short>()) { | ||||
|       call.set_second(root["second"]); | ||||
|     } | ||||
|     call.perform(); | ||||
|   | ||||
| @@ -20,13 +20,13 @@ MQTTTimeComponent::MQTTTimeComponent(TimeEntity *time) : time_(time) {} | ||||
| void MQTTTimeComponent::setup() { | ||||
|   this->subscribe_json(this->get_command_topic_(), [this](const std::string &topic, JsonObject root) { | ||||
|     auto call = this->time_->make_call(); | ||||
|     if (root["hour"].is<JsonVariant>()) { | ||||
|     if (root["hour"].is<unsigned short>()) { | ||||
|       call.set_hour(root["hour"]); | ||||
|     } | ||||
|     if (root["minute"].is<JsonVariant>()) { | ||||
|     if (root["minute"].is<unsigned short>()) { | ||||
|       call.set_minute(root["minute"]); | ||||
|     } | ||||
|     if (root["second"].is<JsonVariant>()) { | ||||
|     if (root["second"].is<unsigned short>()) { | ||||
|       call.set_second(root["second"]); | ||||
|     } | ||||
|     call.perform(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user