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

Optionally show internal components on the web server (#2627)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
mechanarchy
2021-11-30 02:52:20 +11:00
committed by GitHub
parent adf48246a9
commit 6f07421911
7 changed files with 67 additions and 78 deletions

View File

@@ -31,10 +31,10 @@ static const char *const TAG = "web_server";
void write_row(AsyncResponseStream *stream, EntityBase *obj, const std::string &klass, const std::string &action,
const std::function<void(AsyncResponseStream &stream, EntityBase *obj)> &action_func = nullptr) {
if (obj->is_internal())
return;
stream->print("<tr class=\"");
stream->print(klass.c_str());
if (obj->is_internal())
stream->print(" internal");
stream->print("\" id=\"");
stream->print(klass.c_str());
stream->print("-");
@@ -83,7 +83,7 @@ void WebServer::set_js_include(const char *js_include) { this->js_include_ = js_
void WebServer::setup() {
ESP_LOGCONFIG(TAG, "Setting up web server...");
this->setup_controller();
this->setup_controller(this->include_internal_);
this->base_->init();
this->events_.onConnect([this](AsyncEventSourceClient *client) {
@@ -92,55 +92,55 @@ void WebServer::setup() {
#ifdef USE_SENSOR
for (auto *obj : App.get_sensors())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->sensor_json(obj, obj->state).c_str(), "state");
#endif
#ifdef USE_SWITCH
for (auto *obj : App.get_switches())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->switch_json(obj, obj->state).c_str(), "state");
#endif
#ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->binary_sensor_json(obj, obj->state).c_str(), "state");
#endif
#ifdef USE_FAN
for (auto *obj : App.get_fans())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->fan_json(obj).c_str(), "state");
#endif
#ifdef USE_LIGHT
for (auto *obj : App.get_lights())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->light_json(obj).c_str(), "state");
#endif
#ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->text_sensor_json(obj, obj->state).c_str(), "state");
#endif
#ifdef USE_COVER
for (auto *obj : App.get_covers())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->cover_json(obj).c_str(), "state");
#endif
#ifdef USE_NUMBER
for (auto *obj : App.get_numbers())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->number_json(obj, obj->state).c_str(), "state");
#endif
#ifdef USE_SELECT
for (auto *obj : App.get_selects())
if (!obj->is_internal())
if (this->include_internal_ || !obj->is_internal())
client->send(this->select_json(obj, obj->state).c_str(), "state");
#endif
});
@@ -188,57 +188,66 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
#ifdef USE_SENSOR
for (auto *obj : App.get_sensors())
write_row(stream, obj, "sensor", "");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "sensor", "");
#endif
#ifdef USE_SWITCH
for (auto *obj : App.get_switches())
write_row(stream, obj, "switch", "<button>Toggle</button>");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "switch", "<button>Toggle</button>");
#endif
#ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors())
write_row(stream, obj, "binary_sensor", "");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "binary_sensor", "");
#endif
#ifdef USE_FAN
for (auto *obj : App.get_fans())
write_row(stream, obj, "fan", "<button>Toggle</button>");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "fan", "<button>Toggle</button>");
#endif
#ifdef USE_LIGHT
for (auto *obj : App.get_lights())
write_row(stream, obj, "light", "<button>Toggle</button>");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "light", "<button>Toggle</button>");
#endif
#ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors())
write_row(stream, obj, "text_sensor", "");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "text_sensor", "");
#endif
#ifdef USE_COVER
for (auto *obj : App.get_covers())
write_row(stream, obj, "cover", "<button>Open</button><button>Close</button>");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "cover", "<button>Open</button><button>Close</button>");
#endif
#ifdef USE_NUMBER
for (auto *obj : App.get_numbers())
write_row(stream, obj, "number", "");
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "number", "");
#endif
#ifdef USE_SELECT
for (auto *obj : App.get_selects())
write_row(stream, obj, "select", "", [](AsyncResponseStream &stream, EntityBase *obj) {
select::Select *select = (select::Select *) obj;
stream.print("<select>");
stream.print("<option></option>");
for (auto const &option : select->traits.get_options()) {
stream.print("<option>");
stream.print(option.c_str());
stream.print("</option>");
}
stream.print("</select>");
});
if (this->include_internal_ || !obj->is_internal())
write_row(stream, obj, "select", "", [](AsyncResponseStream &stream, EntityBase *obj) {
select::Select *select = (select::Select *) obj;
stream.print("<select>");
stream.print("<option></option>");
for (auto const &option : select->traits.get_options()) {
stream.print("<option>");
stream.print(option.c_str());
stream.print("</option>");
}
stream.print("</select>");
});
#endif
stream->print(F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for "
@@ -293,8 +302,6 @@ void WebServer::on_sensor_update(sensor::Sensor *obj, float state) {
}
void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (sensor::Sensor *obj : App.get_sensors()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
std::string data = this->sensor_json(obj, obj->state);
@@ -321,8 +328,6 @@ void WebServer::on_text_sensor_update(text_sensor::TextSensor *obj, const std::s
}
void WebServer::handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (text_sensor::TextSensor *obj : App.get_text_sensors()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
std::string data = this->text_sensor_json(obj, obj->state);
@@ -353,8 +358,6 @@ std::string WebServer::switch_json(switch_::Switch *obj, bool value) {
}
void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (switch_::Switch *obj : App.get_switches()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
@@ -381,8 +384,6 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM
#ifdef USE_BINARY_SENSOR
void WebServer::on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) {
if (obj->is_internal())
return;
this->events_.send(this->binary_sensor_json(obj, state).c_str(), "state");
}
std::string WebServer::binary_sensor_json(binary_sensor::BinarySensor *obj, bool value) {
@@ -394,8 +395,6 @@ std::string WebServer::binary_sensor_json(binary_sensor::BinarySensor *obj, bool
}
void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (binary_sensor::BinarySensor *obj : App.get_binary_sensors()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
std::string data = this->binary_sensor_json(obj, obj->state);
@@ -407,11 +406,7 @@ void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, con
#endif
#ifdef USE_FAN
void WebServer::on_fan_update(fan::FanState *obj) {
if (obj->is_internal())
return;
this->events_.send(this->fan_json(obj).c_str(), "state");
}
void WebServer::on_fan_update(fan::FanState *obj) { this->events_.send(this->fan_json(obj).c_str(), "state"); }
std::string WebServer::fan_json(fan::FanState *obj) {
return json::build_json([obj](JsonObject &root) {
root["id"] = "fan-" + obj->get_object_id();
@@ -442,8 +437,6 @@ std::string WebServer::fan_json(fan::FanState *obj) {
}
void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (fan::FanState *obj : App.get_fans()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
@@ -504,15 +497,9 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc
#endif
#ifdef USE_LIGHT
void WebServer::on_light_update(light::LightState *obj) {
if (obj->is_internal())
return;
this->events_.send(this->light_json(obj).c_str(), "state");
}
void WebServer::on_light_update(light::LightState *obj) { this->events_.send(this->light_json(obj).c_str(), "state"); }
void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (light::LightState *obj : App.get_lights()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
@@ -579,15 +566,9 @@ std::string WebServer::light_json(light::LightState *obj) {
#endif
#ifdef USE_COVER
void WebServer::on_cover_update(cover::Cover *obj) {
if (obj->is_internal())
return;
this->events_.send(this->cover_json(obj).c_str(), "state");
}
void WebServer::on_cover_update(cover::Cover *obj) { this->events_.send(this->cover_json(obj).c_str(), "state"); }
void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (cover::Cover *obj : App.get_covers()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
@@ -646,8 +627,6 @@ void WebServer::on_number_update(number::Number *obj, float state) {
}
void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (auto *obj : App.get_numbers()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;
std::string data = this->number_json(obj, obj->state);
@@ -673,8 +652,6 @@ void WebServer::on_select_update(select::Select *obj, const std::string &state)
}
void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match) {
for (auto *obj : App.get_selects()) {
if (obj->is_internal())
continue;
if (obj->get_object_id() != match.id)
continue;