ESPHome  2025.2.0
prometheus_handler.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #ifdef USE_NETWORK
4 #include <map>
5 #include <utility>
6 
11 
12 namespace esphome {
13 namespace prometheus {
14 
15 class PrometheusHandler : public AsyncWebHandler, public Component {
16  public:
18 
24  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
25 
31  void add_label_id(EntityBase *obj, const std::string &value) { relabel_map_id_.insert({obj, value}); }
32 
38  void add_label_name(EntityBase *obj, const std::string &value) { relabel_map_name_.insert({obj, value}); }
39 
40  bool canHandle(AsyncWebServerRequest *request) override {
41  if (request->method() == HTTP_GET) {
42  if (request->url() == "/metrics")
43  return true;
44  }
45 
46  return false;
47  }
48 
49  void handleRequest(AsyncWebServerRequest *req) override;
50 
51  void setup() override {
52  this->base_->init();
53  this->base_->add_handler(this);
54  }
55  float get_setup_priority() const override {
56  // After WiFi
57  return setup_priority::WIFI - 1.0f;
58  }
59 
60  protected:
61  std::string relabel_id_(EntityBase *obj);
62  std::string relabel_name_(EntityBase *obj);
63  void add_area_label_(AsyncResponseStream *stream, std::string &area);
64  void add_node_label_(AsyncResponseStream *stream, std::string &node);
65  void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name);
66 
67 #ifdef USE_SENSOR
68  void sensor_type_(AsyncResponseStream *stream);
71  void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node,
72  std::string &friendly_name);
73 #endif
74 
75 #ifdef USE_BINARY_SENSOR
76  void binary_sensor_type_(AsyncResponseStream *stream);
79  void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area,
80  std::string &node, std::string &friendly_name);
81 #endif
82 
83 #ifdef USE_FAN
84  void fan_type_(AsyncResponseStream *stream);
87  void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node,
88  std::string &friendly_name);
89 #endif
90 
91 #ifdef USE_LIGHT
92  void light_type_(AsyncResponseStream *stream);
95  void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node,
96  std::string &friendly_name);
97 #endif
98 
99 #ifdef USE_COVER
100  void cover_type_(AsyncResponseStream *stream);
103  void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node,
104  std::string &friendly_name);
105 #endif
106 
107 #ifdef USE_SWITCH
108  void switch_type_(AsyncResponseStream *stream);
111  void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node,
112  std::string &friendly_name);
113 #endif
114 
115 #ifdef USE_LOCK
116  void lock_type_(AsyncResponseStream *stream);
119  void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node,
120  std::string &friendly_name);
121 #endif
122 
123 #ifdef USE_TEXT_SENSOR
124  void text_sensor_type_(AsyncResponseStream *stream);
127  void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node,
128  std::string &friendly_name);
129 #endif
130 
131 #ifdef USE_NUMBER
132  void number_type_(AsyncResponseStream *stream);
135  void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node,
136  std::string &friendly_name);
137 #endif
138 
139 #ifdef USE_SELECT
140  void select_type_(AsyncResponseStream *stream);
143  void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node,
144  std::string &friendly_name);
145 #endif
146 
147 #ifdef USE_MEDIA_PLAYER
148  void media_player_type_(AsyncResponseStream *stream);
151  void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area,
152  std::string &node, std::string &friendly_name);
153 #endif
154 
155 #ifdef USE_UPDATE
156  void update_entity_type_(AsyncResponseStream *stream);
159  void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node,
160  std::string &friendly_name);
161  void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state);
162 #endif
163 
164 #ifdef USE_VALVE
165  void valve_type_(AsyncResponseStream *stream);
168  void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node,
169  std::string &friendly_name);
170 #endif
171 
173  bool include_internal_{false};
174  std::map<EntityBase *, std::string> relabel_map_id_;
175  std::map<EntityBase *, std::string> relabel_map_name_;
176 };
177 
178 } // namespace prometheus
179 } // namespace esphome
180 #endif
Base class for all switches.
Definition: switch.h:39
void handleRequest(AsyncWebServerRequest *req) override
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
Base class for all cover devices.
Definition: cover.h:111
void add_node_label_(AsyncResponseStream *stream, std::string &node)
void select_type_(AsyncResponseStream *stream)
Return the type for prometheus.
std::string relabel_id_(EntityBase *obj)
void text_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void update_entity_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void switch_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_area_label_(AsyncResponseStream *stream, std::string &area)
void number_row_(AsyncResponseStream *stream, number::Number *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the number state as prometheus data point.
void valve_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the binary sensor state as prometheus data point.
bool canHandle(AsyncWebServerRequest *request) override
void media_player_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_handler(AsyncWebHandler *handler)
void text_sensor_row_(AsyncResponseStream *stream, text_sensor::TextSensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the text sensor values state as prometheus data point.
void binary_sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
Base-class for all numbers.
Definition: number.h:39
void add_friendly_name_label_(AsyncResponseStream *stream, std::string &friendly_name)
void fan_row_(AsyncResponseStream *stream, fan::Fan *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the fan state as prometheus data point.
void switch_row_(AsyncResponseStream *stream, switch_::Switch *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the switch values state as prometheus data point.
void select_row_(AsyncResponseStream *stream, select::Select *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the select state as prometheus data point.
void update_entity_row_(AsyncResponseStream *stream, update::UpdateEntity *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the update state and info as prometheus data point.
std::map< EntityBase *, std::string > relabel_map_name_
void set_include_internal(bool include_internal)
Determine whether internal components should be exported as metrics.
void light_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void add_label_id(EntityBase *obj, const std::string &value)
Add the value for an entity&#39;s "id" label.
void handle_update_state_(AsyncResponseStream *stream, update::UpdateState state)
web_server_base::WebServerBase * base_
void number_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void lock_row_(AsyncResponseStream *stream, lock::Lock *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the lock values state as prometheus data point.
PrometheusHandler(web_server_base::WebServerBase *base)
void lock_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the sensor state as prometheus data point.
void add_label_name(EntityBase *obj, const std::string &value)
Add the value for an entity&#39;s "name" label.
void fan_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void sensor_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void cover_type_(AsyncResponseStream *stream)
Return the type for prometheus.
void media_player_row_(AsyncResponseStream *stream, media_player::MediaPlayer *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the media player state as prometheus data point.
std::string relabel_name_(EntityBase *obj)
void cover_row_(AsyncResponseStream *stream, cover::Cover *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the cover values state as prometheus data point.
Base-class for all selects.
Definition: select.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
Base-class for all sensors.
Definition: sensor.h:57
void light_row_(AsyncResponseStream *stream, light::LightState *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the light values state as prometheus data point.
std::map< EntityBase *, std::string > relabel_map_id_
void valve_row_(AsyncResponseStream *stream, valve::Valve *obj, std::string &area, std::string &node, std::string &friendly_name)
Return the valve state as prometheus data point.
Base class for all locks.
Definition: lock.h:103
bool state
Definition: fan.h:34