1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-07 05:42:20 +01:00

json keys

This commit is contained in:
J. Nick Koston
2025-09-05 11:02:14 -05:00
parent 6d70417cae
commit ae3f4ad919

View File

@@ -262,8 +262,8 @@ void DeferredUpdateEventSourceList::on_client_connect_(WebServer *ws, DeferredUp
#ifdef USE_WEBSERVER_SORTING
for (auto &group : ws->sorting_groups_) {
message = json::build_json([group](JsonObject root) {
root["name"] = group.second.name;
root["sorting_weight"] = group.second.weight;
root[F("name")] = group.second.name;
root[F("sorting_weight")] = group.second.weight;
});
// up to 31 groups should be able to be queued initially without defer
@@ -299,15 +299,15 @@ void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_
std::string WebServer::get_config_json() {
return json::build_json([this](JsonObject root) {
root["title"] = App.get_friendly_name().empty() ? App.get_name() : App.get_friendly_name();
root["comment"] = App.get_comment();
root[F("title")] = App.get_friendly_name().empty() ? App.get_name() : App.get_friendly_name();
root[F("comment")] = App.get_comment();
#if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA)
root["ota"] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal
root[F("ota")] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal
#else
root["ota"] = true;
root[F("ota")] = true;
#endif
root["log"] = this->expose_log_;
root["lang"] = "en";
root[F("log")] = this->expose_log_;
root[F("lang")] = "en";
});
}
@@ -411,14 +411,14 @@ void WebServer::handle_js_request(AsyncWebServerRequest *request) {
// Helper functions to reduce code size by avoiding macro expansion
static void set_json_id(JsonObject &root, EntityBase *obj, const std::string &id, JsonDetail start_config) {
root["id"] = id;
root[F("id")] = id;
if (start_config == DETAIL_ALL) {
root["name"] = obj->get_name();
root["icon"] = obj->get_icon();
root["entity_category"] = obj->get_entity_category();
root[F("name")] = obj->get_name();
root[F("icon")] = obj->get_icon();
root[F("entity_category")] = obj->get_entity_category();
bool is_disabled = obj->is_disabled_by_default();
if (is_disabled)
root["is_disabled_by_default"] = is_disabled;
root[F("is_disabled_by_default")] = is_disabled;
}
}
@@ -426,14 +426,14 @@ template<typename T>
static void set_json_value(JsonObject &root, EntityBase *obj, const std::string &id, const T &value,
JsonDetail start_config) {
set_json_id(root, obj, id, start_config);
root["value"] = value;
root[F("value")] = value;
}
template<typename T>
static void set_json_icon_state_value(JsonObject &root, EntityBase *obj, const std::string &id,
const std::string &state, const T &value, JsonDetail start_config) {
set_json_value(root, obj, id, value, start_config);
root["state"] = state;
root[F("state")] = state;
}
// Helper to get request detail parameter
@@ -481,7 +481,7 @@ std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
if (!obj->get_unit_of_measurement().empty())
root["uom"] = obj->get_unit_of_measurement();
root[F("uom")] = obj->get_unit_of_measurement();
}
});
}
@@ -589,7 +589,7 @@ std::string WebServer::switch_json(switch_::Switch *obj, bool value, JsonDetail
return json::build_json([this, obj, value, start_config](JsonObject root) {
set_json_icon_state_value(root, obj, "switch-" + obj->get_object_id(), value ? "ON" : "OFF", value, start_config);
if (start_config == DETAIL_ALL) {
root["assumed_state"] = obj->assumed_state();
root[F("assumed_state")] = obj->assumed_state();
this->add_sorting_info_(root, obj);
}
});
@@ -732,11 +732,11 @@ std::string WebServer::fan_json(fan::Fan *obj, JsonDetail start_config) {
start_config);
const auto traits = obj->get_traits();
if (traits.supports_speed()) {
root["speed_level"] = obj->speed;
root["speed_count"] = traits.supported_speed_count();
root[F("speed_level")] = obj->speed;
root[F("speed_count")] = traits.supported_speed_count();
}
if (obj->get_traits().supports_oscillation())
root["oscillation"] = obj->oscillating;
root[F("oscillation")] = obj->oscillating;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -802,11 +802,11 @@ std::string WebServer::light_all_json_generator(WebServer *web_server, void *sou
std::string WebServer::light_json(light::LightState *obj, JsonDetail start_config) {
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_id(root, obj, "light-" + obj->get_object_id(), start_config);
root["state"] = obj->remote_values.is_on() ? "ON" : "OFF";
root[F("state")] = obj->remote_values.is_on() ? "ON" : "OFF";
light::LightJSONSchema::dump_json(*obj, root);
if (start_config == DETAIL_ALL) {
JsonArray opt = root["effects"].to<JsonArray>();
JsonArray opt = root[F("effects")].to<JsonArray>();
opt.add("None");
for (auto const &option : obj->get_effects()) {
opt.add(option->get_name());
@@ -875,12 +875,12 @@ std::string WebServer::cover_json(cover::Cover *obj, JsonDetail start_config) {
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_icon_state_value(root, obj, "cover-" + obj->get_object_id(), obj->is_fully_closed() ? "CLOSED" : "OPEN",
obj->position, start_config);
root["current_operation"] = cover::cover_operation_to_str(obj->current_operation);
root[F("current_operation")] = cover::cover_operation_to_str(obj->current_operation);
if (obj->get_traits().get_supports_position())
root["position"] = obj->position;
root[F("position")] = obj->position;
if (obj->get_traits().get_supports_tilt())
root["tilt"] = obj->tilt;
root[F("tilt")] = obj->tilt;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -930,26 +930,26 @@ std::string WebServer::number_json(number::Number *obj, float value, JsonDetail
return json::build_json([this, obj, value, start_config](JsonObject root) {
set_json_id(root, obj, "number-" + obj->get_object_id(), start_config);
if (start_config == DETAIL_ALL) {
root["min_value"] =
root[F("min_value")] =
value_accuracy_to_string(obj->traits.get_min_value(), step_to_accuracy_decimals(obj->traits.get_step()));
root["max_value"] =
root[F("max_value")] =
value_accuracy_to_string(obj->traits.get_max_value(), step_to_accuracy_decimals(obj->traits.get_step()));
root["step"] =
root[F("step")] =
value_accuracy_to_string(obj->traits.get_step(), step_to_accuracy_decimals(obj->traits.get_step()));
root["mode"] = (int) obj->traits.get_mode();
root[F("mode")] = (int) obj->traits.get_mode();
if (!obj->traits.get_unit_of_measurement().empty())
root["uom"] = obj->traits.get_unit_of_measurement();
root[F("uom")] = obj->traits.get_unit_of_measurement();
this->add_sorting_info_(root, obj);
}
if (std::isnan(value)) {
root["value"] = "\"NaN\"";
root["state"] = "NA";
root[F("value")] = "\"NaN\"";
root[F("state")] = "NA";
} else {
root["value"] = value_accuracy_to_string(value, step_to_accuracy_decimals(obj->traits.get_step()));
root[F("value")] = value_accuracy_to_string(value, step_to_accuracy_decimals(obj->traits.get_step()));
std::string state = value_accuracy_to_string(value, step_to_accuracy_decimals(obj->traits.get_step()));
if (!obj->traits.get_unit_of_measurement().empty())
state += " " + obj->traits.get_unit_of_measurement();
root["state"] = state;
root[F("state")] = state;
}
});
}
@@ -1002,8 +1002,8 @@ std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_con
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_id(root, obj, "date-" + obj->get_object_id(), start_config);
std::string value = str_sprintf("%d-%02d-%02d", obj->year, obj->month, obj->day);
root["value"] = value;
root["state"] = value;
root[F("value")] = value;
root[F("state")] = value;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -1057,8 +1057,8 @@ std::string WebServer::time_json(datetime::TimeEntity *obj, JsonDetail start_con
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_id(root, obj, "time-" + obj->get_object_id(), start_config);
std::string value = str_sprintf("%02d:%02d:%02d", obj->hour, obj->minute, obj->second);
root["value"] = value;
root["state"] = value;
root[F("value")] = value;
root[F("state")] = value;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -1113,8 +1113,8 @@ std::string WebServer::datetime_json(datetime::DateTimeEntity *obj, JsonDetail s
set_json_id(root, obj, "datetime-" + obj->get_object_id(), start_config);
std::string value = str_sprintf("%d-%02d-%02d %02d:%02d:%02d", obj->year, obj->month, obj->day, obj->hour,
obj->minute, obj->second);
root["value"] = value;
root["state"] = value;
root[F("value")] = value;
root[F("state")] = value;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -1163,17 +1163,17 @@ std::string WebServer::text_all_json_generator(WebServer *web_server, void *sour
std::string WebServer::text_json(text::Text *obj, const std::string &value, JsonDetail start_config) {
return json::build_json([this, obj, value, start_config](JsonObject root) {
set_json_id(root, obj, "text-" + obj->get_object_id(), start_config);
root["min_length"] = obj->traits.get_min_length();
root["max_length"] = obj->traits.get_max_length();
root["pattern"] = obj->traits.get_pattern();
root[F("min_length")] = obj->traits.get_min_length();
root[F("max_length")] = obj->traits.get_max_length();
root[F("pattern")] = obj->traits.get_pattern();
if (obj->traits.get_mode() == text::TextMode::TEXT_MODE_PASSWORD) {
root["state"] = "********";
root[F("state")] = "********";
} else {
root["state"] = value;
root[F("state")] = value;
}
root["value"] = value;
root[F("value")] = value;
if (start_config == DETAIL_ALL) {
root["mode"] = (int) obj->traits.get_mode();
root[F("mode")] = (int) obj->traits.get_mode();
this->add_sorting_info_(root, obj);
}
});
@@ -1222,7 +1222,7 @@ std::string WebServer::select_json(select::Select *obj, const std::string &value
return json::build_json([this, obj, value, start_config](JsonObject root) {
set_json_icon_state_value(root, obj, "select-" + obj->get_object_id(), value, value, start_config);
if (start_config == DETAIL_ALL) {
JsonArray opt = root["option"].to<JsonArray>();
JsonArray opt = root[F("option")].to<JsonArray>();
for (auto &option : obj->traits.get_options()) {
opt.add(option);
}
@@ -1292,32 +1292,32 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf
char buf[16];
if (start_config == DETAIL_ALL) {
JsonArray opt = root["modes"].to<JsonArray>();
JsonArray opt = root[F("modes")].to<JsonArray>();
for (climate::ClimateMode m : traits.get_supported_modes())
opt.add(PSTR_LOCAL(climate::climate_mode_to_string(m)));
if (!traits.get_supported_custom_fan_modes().empty()) {
JsonArray opt = root["fan_modes"].to<JsonArray>();
JsonArray opt = root[F("fan_modes")].to<JsonArray>();
for (climate::ClimateFanMode m : traits.get_supported_fan_modes())
opt.add(PSTR_LOCAL(climate::climate_fan_mode_to_string(m)));
}
if (!traits.get_supported_custom_fan_modes().empty()) {
JsonArray opt = root["custom_fan_modes"].to<JsonArray>();
JsonArray opt = root[F("custom_fan_modes")].to<JsonArray>();
for (auto const &custom_fan_mode : traits.get_supported_custom_fan_modes())
opt.add(custom_fan_mode);
}
if (traits.get_supports_swing_modes()) {
JsonArray opt = root["swing_modes"].to<JsonArray>();
JsonArray opt = root[F("swing_modes")].to<JsonArray>();
for (auto swing_mode : traits.get_supported_swing_modes())
opt.add(PSTR_LOCAL(climate::climate_swing_mode_to_string(swing_mode)));
}
if (traits.get_supports_presets() && obj->preset.has_value()) {
JsonArray opt = root["presets"].to<JsonArray>();
JsonArray opt = root[F("presets")].to<JsonArray>();
for (climate::ClimatePreset m : traits.get_supported_presets())
opt.add(PSTR_LOCAL(climate::climate_preset_to_string(m)));
}
if (!traits.get_supported_custom_presets().empty() && obj->custom_preset.has_value()) {
JsonArray opt = root["custom_presets"].to<JsonArray>();
JsonArray opt = root[F("custom_presets")].to<JsonArray>();
for (auto const &custom_preset : traits.get_supported_custom_presets())
opt.add(custom_preset);
}
@@ -1325,48 +1325,48 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf
}
bool has_state = false;
root["mode"] = PSTR_LOCAL(climate_mode_to_string(obj->mode));
root["max_temp"] = value_accuracy_to_string(traits.get_visual_max_temperature(), target_accuracy);
root["min_temp"] = value_accuracy_to_string(traits.get_visual_min_temperature(), target_accuracy);
root["step"] = traits.get_visual_target_temperature_step();
root[F("mode")] = PSTR_LOCAL(climate_mode_to_string(obj->mode));
root[F("max_temp")] = value_accuracy_to_string(traits.get_visual_max_temperature(), target_accuracy);
root[F("min_temp")] = value_accuracy_to_string(traits.get_visual_min_temperature(), target_accuracy);
root[F("step")] = traits.get_visual_target_temperature_step();
if (traits.get_supports_action()) {
root["action"] = PSTR_LOCAL(climate_action_to_string(obj->action));
root["state"] = root["action"];
root[F("action")] = PSTR_LOCAL(climate_action_to_string(obj->action));
root[F("state")] = root[F("action")];
has_state = true;
}
if (traits.get_supports_fan_modes() && obj->fan_mode.has_value()) {
root["fan_mode"] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value()));
root[F("fan_mode")] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value()));
}
if (!traits.get_supported_custom_fan_modes().empty() && obj->custom_fan_mode.has_value()) {
root["custom_fan_mode"] = obj->custom_fan_mode.value().c_str();
root[F("custom_fan_mode")] = obj->custom_fan_mode.value().c_str();
}
if (traits.get_supports_presets() && obj->preset.has_value()) {
root["preset"] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
root[F("preset")] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
}
if (!traits.get_supported_custom_presets().empty() && obj->custom_preset.has_value()) {
root["custom_preset"] = obj->custom_preset.value().c_str();
root[F("custom_preset")] = obj->custom_preset.value().c_str();
}
if (traits.get_supports_swing_modes()) {
root["swing_mode"] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));
root[F("swing_mode")] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));
}
if (traits.get_supports_current_temperature()) {
if (!std::isnan(obj->current_temperature)) {
root["current_temperature"] = value_accuracy_to_string(obj->current_temperature, current_accuracy);
root[F("current_temperature")] = value_accuracy_to_string(obj->current_temperature, current_accuracy);
} else {
root["current_temperature"] = "NA";
root[F("current_temperature")] = "NA";
}
}
if (traits.get_supports_two_point_target_temperature()) {
root["target_temperature_low"] = value_accuracy_to_string(obj->target_temperature_low, target_accuracy);
root["target_temperature_high"] = value_accuracy_to_string(obj->target_temperature_high, target_accuracy);
root[F("target_temperature_low")] = value_accuracy_to_string(obj->target_temperature_low, target_accuracy);
root[F("target_temperature_high")] = value_accuracy_to_string(obj->target_temperature_high, target_accuracy);
if (!has_state) {
root["state"] = value_accuracy_to_string((obj->target_temperature_high + obj->target_temperature_low) / 2.0f,
root[F("state")] = value_accuracy_to_string((obj->target_temperature_high + obj->target_temperature_low) / 2.0f,
target_accuracy);
}
} else {
root["target_temperature"] = value_accuracy_to_string(obj->target_temperature, target_accuracy);
root[F("target_temperature")] = value_accuracy_to_string(obj->target_temperature, target_accuracy);
if (!has_state)
root["state"] = root["target_temperature"];
root[F("state")] = root[F("target_temperature")];
}
});
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
@@ -1500,10 +1500,10 @@ std::string WebServer::valve_json(valve::Valve *obj, JsonDetail start_config) {
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_icon_state_value(root, obj, "valve-" + obj->get_object_id(), obj->is_fully_closed() ? "CLOSED" : "OPEN",
obj->position, start_config);
root["current_operation"] = valve::valve_operation_to_str(obj->current_operation);
root[F("current_operation")] = valve::valve_operation_to_str(obj->current_operation);
if (obj->get_traits().get_supports_position())
root["position"] = obj->position;
root[F("position")] = obj->position;
if (start_config == DETAIL_ALL) {
this->add_sorting_info_(root, obj);
}
@@ -1613,14 +1613,14 @@ std::string WebServer::event_json(event::Event *obj, const std::string &event_ty
return json::build_json([this, obj, event_type, start_config](JsonObject root) {
set_json_id(root, obj, "event-" + obj->get_object_id(), start_config);
if (!event_type.empty()) {
root["event_type"] = event_type;
root[F("event_type")] = event_type;
}
if (start_config == DETAIL_ALL) {
JsonArray event_types = root["event_types"].to<JsonArray>();
JsonArray event_types = root[F("event_types")].to<JsonArray>();
for (auto const &event_type : obj->get_event_types()) {
event_types.add(event_type);
}
root["device_class"] = obj->get_device_class();
root[F("device_class")] = obj->get_device_class();
this->add_sorting_info_(root, obj);
}
});
@@ -1679,13 +1679,13 @@ std::string WebServer::update_json(update::UpdateEntity *obj, JsonDetail start_c
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
return json::build_json([this, obj, start_config](JsonObject root) {
set_json_id(root, obj, "update-" + obj->get_object_id(), start_config);
root["value"] = obj->update_info.latest_version;
root["state"] = update_state_to_string(obj->state);
root[F("value")] = obj->update_info.latest_version;
root[F("state")] = update_state_to_string(obj->state);
if (start_config == DETAIL_ALL) {
root["current_version"] = obj->update_info.current_version;
root["title"] = obj->update_info.title;
root["summary"] = obj->update_info.summary;
root["release_url"] = obj->update_info.release_url;
root[F("current_version")] = obj->update_info.current_version;
root[F("title")] = obj->update_info.title;
root[F("summary")] = obj->update_info.summary;
root[F("release_url")] = obj->update_info.release_url;
this->add_sorting_info_(root, obj);
}
});
@@ -1948,9 +1948,9 @@ bool WebServer::isRequestHandlerTrivial() const { return false; }
void WebServer::add_sorting_info_(JsonObject &root, EntityBase *entity) {
#ifdef USE_WEBSERVER_SORTING
if (this->sorting_entitys_.find(entity) != this->sorting_entitys_.end()) {
root["sorting_weight"] = this->sorting_entitys_[entity].weight;
root[F("sorting_weight")] = this->sorting_entitys_[entity].weight;
if (this->sorting_groups_.find(this->sorting_entitys_[entity].group_id) != this->sorting_groups_.end()) {
root["sorting_group"] = this->sorting_groups_[this->sorting_entitys_[entity].group_id].name;
root[F("sorting_group")] = this->sorting_groups_[this->sorting_entitys_[entity].group_id].name;
}
}
#endif