ESPHome 2025.10.3
Loading...
Searching...
No Matches
api_server.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_API
5#include "api_noise_context.h"
6#include "api_pb2.h"
7#include "api_pb2_service.h"
12#include "esphome/core/log.h"
13#include "list_entities.h"
14#include "subscribe_state.h"
15#ifdef USE_API_SERVICES
16#include "user_services.h"
17#endif
18
19#include <map>
20#include <vector>
21
22namespace esphome::api {
23
24#ifdef USE_API_NOISE
27} PACKED; // NOLINT
28#endif
29
30class APIServer : public Component, public Controller {
31 public:
32 APIServer();
33 void setup() override;
34 uint16_t get_port() const;
35 float get_setup_priority() const override;
36 void loop() override;
37 void dump_config() override;
38 void on_shutdown() override;
39 bool teardown() override;
40#ifdef USE_API_PASSWORD
41 bool check_password(const uint8_t *password_data, size_t password_len) const;
42 void set_password(const std::string &password);
43#endif
44 void set_port(uint16_t port);
45 void set_reboot_timeout(uint32_t reboot_timeout);
46 void set_batch_delay(uint16_t batch_delay);
47 uint16_t get_batch_delay() const { return batch_delay_; }
48 void set_listen_backlog(uint8_t listen_backlog) { this->listen_backlog_ = listen_backlog; }
49 void set_max_connections(uint8_t max_connections) { this->max_connections_ = max_connections; }
50
51 // Get reference to shared buffer for API connections
52 std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; }
53
54#ifdef USE_API_NOISE
55 bool save_noise_psk(psk_t psk, bool make_active = true);
56 void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
57 std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
58#endif // USE_API_NOISE
59
61#ifdef USE_BINARY_SENSOR
63#endif
64#ifdef USE_COVER
65 void on_cover_update(cover::Cover *obj) override;
66#endif
67#ifdef USE_FAN
68 void on_fan_update(fan::Fan *obj) override;
69#endif
70#ifdef USE_LIGHT
72#endif
73#ifdef USE_SENSOR
74 void on_sensor_update(sensor::Sensor *obj, float state) override;
75#endif
76#ifdef USE_SWITCH
77 void on_switch_update(switch_::Switch *obj, bool state) override;
78#endif
79#ifdef USE_TEXT_SENSOR
80 void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
81#endif
82#ifdef USE_CLIMATE
84#endif
85#ifdef USE_NUMBER
86 void on_number_update(number::Number *obj, float state) override;
87#endif
88#ifdef USE_DATETIME_DATE
90#endif
91#ifdef USE_DATETIME_TIME
93#endif
94#ifdef USE_DATETIME_DATETIME
96#endif
97#ifdef USE_TEXT
98 void on_text_update(text::Text *obj, const std::string &state) override;
99#endif
100#ifdef USE_SELECT
101 void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
102#endif
103#ifdef USE_LOCK
104 void on_lock_update(lock::Lock *obj) override;
105#endif
106#ifdef USE_VALVE
107 void on_valve_update(valve::Valve *obj) override;
108#endif
109#ifdef USE_MEDIA_PLAYER
111#endif
112#ifdef USE_API_HOMEASSISTANT_SERVICES
114
115#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
116 // Action response handling
117 using ActionResponseCallback = std::function<void(const class ActionResponse &)>;
118 void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback);
119 void handle_action_response(uint32_t call_id, bool success, const std::string &error_message);
120#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
121 void handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
122 const uint8_t *response_data, size_t response_data_len);
123#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
124#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES
125#endif // USE_API_HOMEASSISTANT_SERVICES
126#ifdef USE_API_SERVICES
127 void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
128#endif
129#ifdef USE_HOMEASSISTANT_TIME
130 void request_time();
131#endif
132
133#ifdef USE_ALARM_CONTROL_PANEL
135#endif
136#ifdef USE_EVENT
137 void on_event(event::Event *obj, const std::string &event_type) override;
138#endif
139#ifdef USE_UPDATE
140 void on_update(update::UpdateEntity *obj) override;
141#endif
142#ifdef USE_ZWAVE_PROXY
144#endif
145
146 bool is_connected() const;
147
148#ifdef USE_API_HOMEASSISTANT_STATES
150 std::string entity_id;
152 std::function<void(std::string)> callback;
153 bool once;
154 };
155
156 void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
157 std::function<void(std::string)> f);
158 void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
159 std::function<void(std::string)> f);
160 const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
161#endif
162#ifdef USE_API_SERVICES
163 const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
164#endif
165
166#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
168#endif
169#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
173#endif
174
175 protected:
177 // Pointers and pointer-like types first (4 bytes each)
178 std::unique_ptr<socket::Socket> socket_ = nullptr;
179#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
181#endif
182#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
184#endif
185
186 // 4-byte aligned types
187 uint32_t reboot_timeout_{300000};
188
189 // Vectors and strings (12 bytes each on 32-bit)
190 std::vector<std::unique_ptr<APIConnection>> clients_;
191#ifdef USE_API_PASSWORD
192 std::string password_;
193#endif
194 std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
195#ifdef USE_API_HOMEASSISTANT_STATES
196 std::vector<HomeAssistantStateSubscription> state_subs_;
197#endif
198#ifdef USE_API_SERVICES
199 std::vector<UserServiceDescriptor *> user_services_;
200#endif
201#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
206 std::vector<PendingActionResponse> action_response_callbacks_;
207#endif
208
209 // Group smaller types together
210 uint16_t port_{6053};
211 uint16_t batch_delay_{100};
212 // Connection limits - these defaults will be overridden by config values
213 // from cv.SplitDefault in __init__.py which sets platform-specific defaults
214 uint8_t listen_backlog_{4};
216 bool shutting_down_ = false;
217 // 7 bytes used, 1 byte padding
218
219#ifdef USE_API_NOISE
220 std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
222#endif // USE_API_NOISE
223};
224
225extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
226
227template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
228 public:
229 bool check(Ts... x) override { return global_api_server->is_connected(); }
230};
231
232} // namespace esphome::api
233#endif
Base class for all automation conditions.
Definition automation.h:124
bool check(Ts... x) override
Definition api_server.h:229
void on_valve_update(valve::Valve *obj) override
uint16_t get_batch_delay() const
Definition api_server.h:47
void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback)
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition api_server.h:57
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:190
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
void set_password(const std::string &password)
void on_time_update(datetime::TimeEntity *obj) override
void on_cover_update(cover::Cover *obj) override
void on_sensor_update(sensor::Sensor *obj, float state) override
std::vector< UserServiceDescriptor * > user_services_
Definition api_server.h:199
void on_light_update(light::LightState *obj) override
void on_media_player_update(media_player::MediaPlayer *obj) override
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition api_server.h:163
void set_port(uint16_t port)
void dump_config() override
void handle_disconnect(APIConnection *conn)
void set_batch_delay(uint16_t batch_delay)
void set_reboot_timeout(uint32_t reboot_timeout)
void set_listen_backlog(uint8_t listen_backlog)
Definition api_server.h:48
Trigger< std::string, std::string > * client_connected_trigger_
Definition api_server.h:180
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:127
bool save_noise_psk(psk_t psk, bool make_active=true)
void on_lock_update(lock::Lock *obj) override
void setup() override
void on_date_update(datetime::DateEntity *obj) override
void handle_action_response(uint32_t call_id, bool success, const std::string &error_message)
bool teardown() override
void send_homeassistant_action(const HomeassistantActionRequest &call)
void on_update(update::UpdateEntity *obj) override
bool check_password(const uint8_t *password_data, size_t password_len) const
std::vector< PendingActionResponse > action_response_callbacks_
Definition api_server.h:206
void get_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
std::shared_ptr< APINoiseContext > noise_ctx_
Definition api_server.h:220
void on_number_update(number::Number *obj, float state) override
void on_text_update(text::Text *obj, const std::string &state) override
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition api_server.h:183
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:194
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void on_climate_update(climate::Climate *obj) override
std::function< void(const class ActionResponse &)> ActionResponseCallback
Definition api_server.h:117
void set_max_connections(uint8_t max_connections)
Definition api_server.h:49
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
ESPPreferenceObject noise_pref_
Definition api_server.h:221
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
void on_fan_update(fan::Fan *obj) override
std::vector< HomeAssistantStateSubscription > state_subs_
Definition api_server.h:196
void on_zwave_proxy_request(const esphome::api::ProtoMessage &msg)
void on_switch_update(switch_::Switch *obj, bool state) override
uint16_t get_port() const
std::vector< uint8_t > & get_shared_buffer_ref()
Definition api_server.h:52
void set_noise_psk(psk_t psk)
Definition api_server.h:56
void on_datetime_update(datetime::DateTimeEntity *obj) override
void on_event(event::Event *obj, const std::string &event_type) override
Trigger< std::string, std::string > * get_client_disconnected_trigger() const
Definition api_server.h:170
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:178
void on_shutdown() override
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition api_server.h:167
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
Base class for all binary_sensor-type classes.
ClimateDevice - This is the base class for all climate integrations.
Definition climate.h:168
Base class for all cover devices.
Definition cover.h:111
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
Base class for all locks.
Definition lock.h:109
Base-class for all numbers.
Definition number.h:30
Base-class for all selects.
Definition select.h:31
Base-class for all sensors.
Definition sensor.h:42
Base class for all switches.
Definition switch.h:39
Base-class for all text inputs.
Definition text.h:24
Base class for all valve devices.
Definition valve.h:105
bool state
Definition fan.h:0
APIServer * global_api_server
struct esphome::api::SavedNoisePsk PACKED
std::array< uint8_t, 32 > psk_t
uint16_t x
Definition tt21100.cpp:5