ESPHome 2025.10.0-dev
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 <vector>
20
21namespace esphome::api {
22
23#ifdef USE_API_NOISE
26} PACKED; // NOLINT
27#endif
28
29class APIServer : public Component, public Controller {
30 public:
31 APIServer();
32 void setup() override;
33 uint16_t get_port() const;
34 float get_setup_priority() const override;
35 void loop() override;
36 void dump_config() override;
37 void on_shutdown() override;
38 bool teardown() override;
39#ifdef USE_API_PASSWORD
40 bool check_password(const uint8_t *password_data, size_t password_len) const;
41 void set_password(const std::string &password);
42#endif
43 void set_port(uint16_t port);
44 void set_reboot_timeout(uint32_t reboot_timeout);
45 void set_batch_delay(uint16_t batch_delay);
46 uint16_t get_batch_delay() const { return batch_delay_; }
47 void set_listen_backlog(uint8_t listen_backlog) { this->listen_backlog_ = listen_backlog; }
48 void set_max_connections(uint8_t max_connections) { this->max_connections_ = max_connections; }
49
50 // Get reference to shared buffer for API connections
51 std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; }
52
53#ifdef USE_API_NOISE
54 bool save_noise_psk(psk_t psk, bool make_active = true);
55 void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
56 std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
57#endif // USE_API_NOISE
58
60#ifdef USE_BINARY_SENSOR
62#endif
63#ifdef USE_COVER
64 void on_cover_update(cover::Cover *obj) override;
65#endif
66#ifdef USE_FAN
67 void on_fan_update(fan::Fan *obj) override;
68#endif
69#ifdef USE_LIGHT
71#endif
72#ifdef USE_SENSOR
73 void on_sensor_update(sensor::Sensor *obj, float state) override;
74#endif
75#ifdef USE_SWITCH
76 void on_switch_update(switch_::Switch *obj, bool state) override;
77#endif
78#ifdef USE_TEXT_SENSOR
79 void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
80#endif
81#ifdef USE_CLIMATE
83#endif
84#ifdef USE_NUMBER
85 void on_number_update(number::Number *obj, float state) override;
86#endif
87#ifdef USE_DATETIME_DATE
89#endif
90#ifdef USE_DATETIME_TIME
92#endif
93#ifdef USE_DATETIME_DATETIME
95#endif
96#ifdef USE_TEXT
97 void on_text_update(text::Text *obj, const std::string &state) override;
98#endif
99#ifdef USE_SELECT
100 void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
101#endif
102#ifdef USE_LOCK
103 void on_lock_update(lock::Lock *obj) override;
104#endif
105#ifdef USE_VALVE
106 void on_valve_update(valve::Valve *obj) override;
107#endif
108#ifdef USE_MEDIA_PLAYER
110#endif
111#ifdef USE_API_HOMEASSISTANT_SERVICES
113
114#endif
115#ifdef USE_API_SERVICES
116 void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
117#endif
118#ifdef USE_HOMEASSISTANT_TIME
119 void request_time();
120#endif
121
122#ifdef USE_ALARM_CONTROL_PANEL
124#endif
125#ifdef USE_EVENT
126 void on_event(event::Event *obj, const std::string &event_type) override;
127#endif
128#ifdef USE_UPDATE
129 void on_update(update::UpdateEntity *obj) override;
130#endif
131#ifdef USE_ZWAVE_PROXY
133#endif
134
135 bool is_connected() const;
136
137#ifdef USE_API_HOMEASSISTANT_STATES
139 std::string entity_id;
141 std::function<void(std::string)> callback;
142 bool once;
143 };
144
145 void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
146 std::function<void(std::string)> f);
147 void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
148 std::function<void(std::string)> f);
149 const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
150#endif
151#ifdef USE_API_SERVICES
152 const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
153#endif
154
155#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
157#endif
158#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
162#endif
163
164 protected:
166 // Pointers and pointer-like types first (4 bytes each)
167 std::unique_ptr<socket::Socket> socket_ = nullptr;
168#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
170#endif
171#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
173#endif
174
175 // 4-byte aligned types
176 uint32_t reboot_timeout_{300000};
177
178 // Vectors and strings (12 bytes each on 32-bit)
179 std::vector<std::unique_ptr<APIConnection>> clients_;
180#ifdef USE_API_PASSWORD
181 std::string password_;
182#endif
183 std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
184#ifdef USE_API_HOMEASSISTANT_STATES
185 std::vector<HomeAssistantStateSubscription> state_subs_;
186#endif
187#ifdef USE_API_SERVICES
188 std::vector<UserServiceDescriptor *> user_services_;
189#endif
190
191 // Group smaller types together
192 uint16_t port_{6053};
193 uint16_t batch_delay_{100};
194 // Connection limits - these defaults will be overridden by config values
195 // from cv.SplitDefault in __init__.py which sets platform-specific defaults
196 uint8_t listen_backlog_{4};
198 bool shutting_down_ = false;
199 // 7 bytes used, 1 byte padding
200
201#ifdef USE_API_NOISE
202 std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
204#endif // USE_API_NOISE
205};
206
207extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
208
209template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
210 public:
211 bool check(Ts... x) override { return global_api_server->is_connected(); }
212};
213
214} // namespace esphome::api
215#endif
Base class for all automation conditions.
Definition automation.h:124
bool check(Ts... x) override
Definition api_server.h:211
void on_valve_update(valve::Valve *obj) override
uint16_t get_batch_delay() const
Definition api_server.h:46
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition api_server.h:56
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:179
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:188
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:152
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:47
Trigger< std::string, std::string > * client_connected_trigger_
Definition api_server.h:169
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:116
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
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
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:202
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:172
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:183
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
void set_max_connections(uint8_t max_connections)
Definition api_server.h:48
void on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
ESPPreferenceObject noise_pref_
Definition api_server.h:203
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:185
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:51
void set_noise_psk(psk_t psk)
Definition api_server.h:55
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:159
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:167
void on_shutdown() override
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition api_server.h:156
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:103
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