Add in area and device to the prometheus labels (#7692)

This commit is contained in:
Jordan Zucker 2024-10-29 18:03:10 -07:00 committed by GitHub
parent bac6880a1e
commit aae2ee2ecb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 208 additions and 24 deletions

View file

@ -7,53 +7,56 @@ namespace prometheus {
void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) { void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) {
AsyncResponseStream *stream = req->beginResponseStream("text/plain; version=0.0.4; charset=utf-8"); AsyncResponseStream *stream = req->beginResponseStream("text/plain; version=0.0.4; charset=utf-8");
std::string area = App.get_area();
std::string node = App.get_name();
std::string friendly_name = App.get_friendly_name();
#ifdef USE_SENSOR #ifdef USE_SENSOR
this->sensor_type_(stream); this->sensor_type_(stream);
for (auto *obj : App.get_sensors()) for (auto *obj : App.get_sensors())
this->sensor_row_(stream, obj); this->sensor_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
this->binary_sensor_type_(stream); this->binary_sensor_type_(stream);
for (auto *obj : App.get_binary_sensors()) for (auto *obj : App.get_binary_sensors())
this->binary_sensor_row_(stream, obj); this->binary_sensor_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
this->fan_type_(stream); this->fan_type_(stream);
for (auto *obj : App.get_fans()) for (auto *obj : App.get_fans())
this->fan_row_(stream, obj); this->fan_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
this->light_type_(stream); this->light_type_(stream);
for (auto *obj : App.get_lights()) for (auto *obj : App.get_lights())
this->light_row_(stream, obj); this->light_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_COVER #ifdef USE_COVER
this->cover_type_(stream); this->cover_type_(stream);
for (auto *obj : App.get_covers()) for (auto *obj : App.get_covers())
this->cover_row_(stream, obj); this->cover_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
this->switch_type_(stream); this->switch_type_(stream);
for (auto *obj : App.get_switches()) for (auto *obj : App.get_switches())
this->switch_row_(stream, obj); this->switch_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_LOCK #ifdef USE_LOCK
this->lock_type_(stream); this->lock_type_(stream);
for (auto *obj : App.get_locks()) for (auto *obj : App.get_locks())
this->lock_row_(stream, obj); this->lock_row_(stream, obj, area, node, friendly_name);
#endif #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
this->text_sensor_type_(stream); this->text_sensor_type_(stream);
for (auto *obj : App.get_text_sensors()) for (auto *obj : App.get_text_sensors())
this->text_sensor_row_(stream, obj); this->text_sensor_row_(stream, obj, area, node, friendly_name);
#endif #endif
req->send(stream); req->send(stream);
@ -69,25 +72,53 @@ std::string PrometheusHandler::relabel_name_(EntityBase *obj) {
return item == relabel_map_name_.end() ? obj->get_name() : item->second; return item == relabel_map_name_.end() ? obj->get_name() : item->second;
} }
void PrometheusHandler::add_area_label_(AsyncResponseStream *stream, std::string &area) {
if (!area.empty()) {
stream->print(F("\",area=\""));
stream->print(area.c_str());
}
}
void PrometheusHandler::add_node_label_(AsyncResponseStream *stream, std::string &node) {
if (!node.empty()) {
stream->print(F("\",node=\""));
stream->print(node.c_str());
}
}
void PrometheusHandler::add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name) {
if (!friendly_name.empty()) {
stream->print(F("\",friendly_name=\""));
stream->print(friendly_name.c_str());
}
}
// Type-specific implementation // Type-specific implementation
#ifdef USE_SENSOR #ifdef USE_SENSOR
void PrometheusHandler::sensor_type_(AsyncResponseStream *stream) { void PrometheusHandler::sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_sensor_value gauge\n")); stream->print(F("#TYPE esphome_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_sensor_failed gauge\n")); stream->print(F("#TYPE esphome_sensor_failed gauge\n"));
} }
void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj) { void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area,
std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
if (!std::isnan(obj->state)) { if (!std::isnan(obj->state)) {
// We have a valid value, output this value // We have a valid value, output this value
stream->print(F("esphome_sensor_failed{id=\"")); stream->print(F("esphome_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_sensor_value{id=\"")); stream->print(F("esphome_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",unit=\"")); stream->print(F("\",unit=\""));
@ -99,6 +130,9 @@ void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor
// Invalid state // Invalid state
stream->print(F("esphome_sensor_failed{id=\"")); stream->print(F("esphome_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n")); stream->print(F("\"} 1\n"));
@ -112,19 +146,26 @@ void PrometheusHandler::binary_sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_binary_sensor_value gauge\n")); stream->print(F("#TYPE esphome_binary_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_binary_sensor_failed gauge\n")); stream->print(F("#TYPE esphome_binary_sensor_failed gauge\n"));
} }
void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj) { void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj,
std::string &area, std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
if (obj->has_state()) { if (obj->has_state()) {
// We have a valid value, output this value // We have a valid value, output this value
stream->print(F("esphome_binary_sensor_failed{id=\"")); stream->print(F("esphome_binary_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_binary_sensor_value{id=\"")); stream->print(F("esphome_binary_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -134,6 +175,9 @@ void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_s
// Invalid state // Invalid state
stream->print(F("esphome_binary_sensor_failed{id=\"")); stream->print(F("esphome_binary_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n")); stream->print(F("\"} 1\n"));
@ -148,17 +192,24 @@ void PrometheusHandler::fan_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_fan_speed gauge\n")); stream->print(F("#TYPE esphome_fan_speed gauge\n"));
stream->print(F("#TYPE esphome_fan_oscillation gauge\n")); stream->print(F("#TYPE esphome_fan_oscillation gauge\n"));
} }
void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) { void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
stream->print(F("esphome_fan_failed{id=\"")); stream->print(F("esphome_fan_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_fan_value{id=\"")); stream->print(F("esphome_fan_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -168,6 +219,9 @@ void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
if (obj->get_traits().supports_speed()) { if (obj->get_traits().supports_speed()) {
stream->print(F("esphome_fan_speed{id=\"")); stream->print(F("esphome_fan_speed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -178,6 +232,9 @@ void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
if (obj->get_traits().supports_oscillation()) { if (obj->get_traits().supports_oscillation()) {
stream->print(F("esphome_fan_oscillation{id=\"")); stream->print(F("esphome_fan_oscillation{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -193,12 +250,16 @@ void PrometheusHandler::light_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_light_color gauge\n")); stream->print(F("#TYPE esphome_light_color gauge\n"));
stream->print(F("#TYPE esphome_light_effect_active gauge\n")); stream->print(F("#TYPE esphome_light_effect_active gauge\n"));
} }
void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightState *obj) { void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area,
std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
// State // State
stream->print(F("esphome_light_state{id=\"")); stream->print(F("esphome_light_state{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -211,6 +272,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
color.as_rgbw(&r, &g, &b, &w); color.as_rgbw(&r, &g, &b, &w);
stream->print(F("esphome_light_color{id=\"")); stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"brightness\"} ")); stream->print(F("\",channel=\"brightness\"} "));
@ -218,6 +282,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n")); stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\"")); stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"r\"} ")); stream->print(F("\",channel=\"r\"} "));
@ -225,6 +292,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n")); stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\"")); stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"g\"} ")); stream->print(F("\",channel=\"g\"} "));
@ -232,6 +302,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n")); stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\"")); stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"b\"} ")); stream->print(F("\",channel=\"b\"} "));
@ -239,6 +312,9 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
stream->print(F("\n")); stream->print(F("\n"));
stream->print(F("esphome_light_color{id=\"")); stream->print(F("esphome_light_color{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",channel=\"w\"} ")); stream->print(F("\",channel=\"w\"} "));
@ -249,12 +325,18 @@ void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightStat
if (effect == "None") { if (effect == "None") {
stream->print(F("esphome_light_effect_active{id=\"")); stream->print(F("esphome_light_effect_active{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",effect=\"None\"} 0\n")); stream->print(F("\",effect=\"None\"} 0\n"));
} else { } else {
stream->print(F("esphome_light_effect_active{id=\"")); stream->print(F("esphome_light_effect_active{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",effect=\"")); stream->print(F("\",effect=\""));
@ -269,19 +351,26 @@ void PrometheusHandler::cover_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_cover_value gauge\n")); stream->print(F("#TYPE esphome_cover_value gauge\n"));
stream->print(F("#TYPE esphome_cover_failed gauge\n")); stream->print(F("#TYPE esphome_cover_failed gauge\n"));
} }
void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *obj) { void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
if (!std::isnan(obj->position)) { if (!std::isnan(obj->position)) {
// We have a valid value, output this value // We have a valid value, output this value
stream->print(F("esphome_cover_failed{id=\"")); stream->print(F("esphome_cover_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_cover_value{id=\"")); stream->print(F("esphome_cover_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -290,6 +379,9 @@ void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *ob
if (obj->get_traits().get_supports_tilt()) { if (obj->get_traits().get_supports_tilt()) {
stream->print(F("esphome_cover_tilt{id=\"")); stream->print(F("esphome_cover_tilt{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -300,6 +392,9 @@ void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *ob
// Invalid state // Invalid state
stream->print(F("esphome_cover_failed{id=\"")); stream->print(F("esphome_cover_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n")); stream->print(F("\"} 1\n"));
@ -312,17 +407,24 @@ void PrometheusHandler::switch_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_switch_value gauge\n")); stream->print(F("#TYPE esphome_switch_value gauge\n"));
stream->print(F("#TYPE esphome_switch_failed gauge\n")); stream->print(F("#TYPE esphome_switch_failed gauge\n"));
} }
void PrometheusHandler::switch_row_(AsyncResponseStream *stream, switch_::Switch *obj) { void PrometheusHandler::switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area,
std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
stream->print(F("esphome_switch_failed{id=\"")); stream->print(F("esphome_switch_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_switch_value{id=\"")); stream->print(F("esphome_switch_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -336,17 +438,24 @@ void PrometheusHandler::lock_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_lock_value gauge\n")); stream->print(F("#TYPE esphome_lock_value gauge\n"));
stream->print(F("#TYPE esphome_lock_failed gauge\n")); stream->print(F("#TYPE esphome_lock_failed gauge\n"));
} }
void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj) { void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
stream->print(F("esphome_lock_failed{id=\"")); stream->print(F("esphome_lock_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_lock_value{id=\"")); stream->print(F("esphome_lock_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} ")); stream->print(F("\"} "));
@ -361,19 +470,26 @@ void PrometheusHandler::text_sensor_type_(AsyncResponseStream *stream) {
stream->print(F("#TYPE esphome_text_sensor_value gauge\n")); stream->print(F("#TYPE esphome_text_sensor_value gauge\n"));
stream->print(F("#TYPE esphome_text_sensor_failed gauge\n")); stream->print(F("#TYPE esphome_text_sensor_failed gauge\n"));
} }
void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj) { void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area,
std::string &node, std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_) if (obj->is_internal() && !this->include_internal_)
return; return;
if (obj->has_state()) { if (obj->has_state()) {
// We have a valid value, output this value // We have a valid value, output this value
stream->print(F("esphome_text_sensor_failed{id=\"")); stream->print(F("esphome_text_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 0\n")); stream->print(F("\"} 0\n"));
// Data itself // Data itself
stream->print(F("esphome_text_sensor_value{id=\"")); stream->print(F("esphome_text_sensor_value{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\",value=\"")); stream->print(F("\",value=\""));
@ -385,6 +501,9 @@ void PrometheusHandler::text_sensor_row_(AsyncResponseStream *stream, text_senso
// Invalid state // Invalid state
stream->print(F("esphome_text_sensor_failed{id=\"")); stream->print(F("esphome_text_sensor_failed{id=\""));
stream->print(relabel_id_(obj).c_str()); stream->print(relabel_id_(obj).c_str());
add_area_label_(stream, area);
add_node_label_(stream, node);
add_friendly_name_label_(stream, friendly_name);
stream->print(F("\",name=\"")); stream->print(F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(F("\"} 1\n")); stream->print(F("\"} 1\n"));

View file

@ -60,61 +60,72 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
protected: protected:
std::string relabel_id_(EntityBase *obj); std::string relabel_id_(EntityBase *obj);
std::string relabel_name_(EntityBase *obj); std::string relabel_name_(EntityBase *obj);
void add_area_label_(AsyncResponseStream *stream, std::string &area);
void add_node_label_(AsyncResponseStream *stream, std::string &node);
void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
#ifdef USE_SENSOR #ifdef USE_SENSOR
/// Return the type for prometheus /// Return the type for prometheus
void sensor_type_(AsyncResponseStream *stream); void sensor_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point /// Return the sensor state as prometheus data point
void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj); void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
/// Return the type for prometheus /// Return the type for prometheus
void binary_sensor_type_(AsyncResponseStream *stream); void binary_sensor_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point /// Return the sensor state as prometheus data point
void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj); void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
std::string &node, std::string &friendly_name);
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
/// Return the type for prometheus /// Return the type for prometheus
void fan_type_(AsyncResponseStream *stream); void fan_type_(AsyncResponseStream *stream);
/// Return the sensor state as prometheus data point /// Return the sensor state as prometheus data point
void fan_row_(AsyncResponseStream *stream, fan::Fan *obj); void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
/// Return the type for prometheus /// Return the type for prometheus
void light_type_(AsyncResponseStream *stream); void light_type_(AsyncResponseStream *stream);
/// Return the Light Values state as prometheus data point /// Return the Light Values state as prometheus data point
void light_row_(AsyncResponseStream *stream, light::LightState *obj); void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_COVER #ifdef USE_COVER
/// Return the type for prometheus /// Return the type for prometheus
void cover_type_(AsyncResponseStream *stream); void cover_type_(AsyncResponseStream *stream);
/// Return the switch Values state as prometheus data point /// Return the switch Values state as prometheus data point
void cover_row_(AsyncResponseStream *stream, cover::Cover *obj); void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
/// Return the type for prometheus /// Return the type for prometheus
void switch_type_(AsyncResponseStream *stream); void switch_type_(AsyncResponseStream *stream);
/// Return the switch Values state as prometheus data point /// Return the switch Values state as prometheus data point
void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj); void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_LOCK #ifdef USE_LOCK
/// Return the type for prometheus /// Return the type for prometheus
void lock_type_(AsyncResponseStream *stream); void lock_type_(AsyncResponseStream *stream);
/// Return the lock Values state as prometheus data point /// Return the lock Values state as prometheus data point
void lock_row_(AsyncResponseStream *stream, lock::Lock *obj); void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
/// Return the type for prometheus /// Return the type for prometheus
void text_sensor_type_(AsyncResponseStream *stream); void text_sensor_type_(AsyncResponseStream *stream);
/// Return the lock Values state as prometheus data point /// Return the lock Values state as prometheus data point
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj); void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
std::string &friendly_name);
#endif #endif
web_server_base::WebServerBase *base_; web_server_base::WebServerBase *base_;

View file

@ -1,3 +1,8 @@
esphome:
name: livingroomdevice
friendly_name: Living Room Device
area: Living Room
wifi: wifi:
ssid: MySSID ssid: MySSID
password: password1 password: password1
@ -14,6 +19,9 @@ sensor:
update_interval: 60s update_interval: 60s
text_sensor: text_sensor:
- platform: version
name: "ESPHome Version"
hide_timestamp: true
- platform: template - platform: template
id: template_text_sensor1 id: template_text_sensor1
lambda: |- lambda: |-
@ -24,6 +32,52 @@ text_sensor:
} }
update_interval: 60s update_interval: 60s
binary_sensor:
- platform: template
id: template_binary_sensor1
lambda: |-
if (millis() > 10000) {
return true;
} else {
return false;
}
switch:
- platform: template
id: template_switch1
lambda: |-
if (millis() > 10000) {
return true;
} else {
return false;
}
optimistic: true
fan:
- platform: template
id: template_fan1
cover:
- platform: template
id: template_cover1
lambda: |-
if (millis() > 10000) {
return COVER_OPEN;
} else {
return COVER_CLOSED;
}
lock:
- platform: template
id: template_lock1
lambda: |-
if (millis() > 10000) {
return LOCK_STATE_LOCKED;
} else {
return LOCK_STATE_UNLOCKED;
}
optimistic: true
prometheus: prometheus:
include_internal: true include_internal: true
relabel: relabel: