ESPHome 2025.9.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 std::string &password) 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
48 // Get reference to shared buffer for API connections
49 std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; }
50
51#ifdef USE_API_NOISE
52 bool save_noise_psk(psk_t psk, bool make_active = true);
53 void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
54 std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
55#endif // USE_API_NOISE
56
58#ifdef USE_BINARY_SENSOR
60#endif
61#ifdef USE_COVER
62 void on_cover_update(cover::Cover *obj) override;
63#endif
64#ifdef USE_FAN
65 void on_fan_update(fan::Fan *obj) override;
66#endif
67#ifdef USE_LIGHT
69#endif
70#ifdef USE_SENSOR
71 void on_sensor_update(sensor::Sensor *obj, float state) override;
72#endif
73#ifdef USE_SWITCH
74 void on_switch_update(switch_::Switch *obj, bool state) override;
75#endif
76#ifdef USE_TEXT_SENSOR
77 void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
78#endif
79#ifdef USE_CLIMATE
81#endif
82#ifdef USE_NUMBER
83 void on_number_update(number::Number *obj, float state) override;
84#endif
85#ifdef USE_DATETIME_DATE
87#endif
88#ifdef USE_DATETIME_TIME
90#endif
91#ifdef USE_DATETIME_DATETIME
93#endif
94#ifdef USE_TEXT
95 void on_text_update(text::Text *obj, const std::string &state) override;
96#endif
97#ifdef USE_SELECT
98 void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
99#endif
100#ifdef USE_LOCK
101 void on_lock_update(lock::Lock *obj) override;
102#endif
103#ifdef USE_VALVE
104 void on_valve_update(valve::Valve *obj) override;
105#endif
106#ifdef USE_MEDIA_PLAYER
108#endif
109#ifdef USE_API_HOMEASSISTANT_SERVICES
111#endif
112#ifdef USE_API_SERVICES
113 void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
114#endif
115#ifdef USE_HOMEASSISTANT_TIME
116 void request_time();
117#endif
118
119#ifdef USE_ALARM_CONTROL_PANEL
121#endif
122#ifdef USE_EVENT
123 void on_event(event::Event *obj, const std::string &event_type) override;
124#endif
125#ifdef USE_UPDATE
126 void on_update(update::UpdateEntity *obj) override;
127#endif
128
129 bool is_connected() const;
130
131#ifdef USE_API_HOMEASSISTANT_STATES
133 std::string entity_id;
135 std::function<void(std::string)> callback;
136 bool once;
137 };
138
139 void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
140 std::function<void(std::string)> f);
141 void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
142 std::function<void(std::string)> f);
143 const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
144#endif
145#ifdef USE_API_SERVICES
146 const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
147#endif
148
149#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
151#endif
152#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
156#endif
157
158 protected:
160 // Pointers and pointer-like types first (4 bytes each)
161 std::unique_ptr<socket::Socket> socket_ = nullptr;
162#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
164#endif
165#ifdef USE_API_CLIENT_DISCONNECTED_TRIGGER
167#endif
168
169 // 4-byte aligned types
170 uint32_t reboot_timeout_{300000};
171
172 // Vectors and strings (12 bytes each on 32-bit)
173 std::vector<std::unique_ptr<APIConnection>> clients_;
174#ifdef USE_API_PASSWORD
175 std::string password_;
176#endif
177 std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
178#ifdef USE_API_HOMEASSISTANT_STATES
179 std::vector<HomeAssistantStateSubscription> state_subs_;
180#endif
181#ifdef USE_API_SERVICES
182 std::vector<UserServiceDescriptor *> user_services_;
183#endif
184
185 // Group smaller types together
186 uint16_t port_{6053};
187 uint16_t batch_delay_{100};
188 bool shutting_down_ = false;
189 // 5 bytes used, 3 bytes padding
190
191#ifdef USE_API_NOISE
192 std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
194#endif // USE_API_NOISE
195};
196
197extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
198
199template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
200 public:
201 bool check(Ts... x) override { return global_api_server->is_connected(); }
202};
203
204} // namespace esphome::api
205#endif
Base class for all automation conditions.
Definition automation.h:124
bool check(Ts... x) override
Definition api_server.h:201
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:54
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:173
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
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:182
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:146
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)
Trigger< std::string, std::string > * client_connected_trigger_
Definition api_server.h:163
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:113
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 on_update(update::UpdateEntity *obj) override
bool check_password(const std::string &password) 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:192
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:166
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:177
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 on_binary_sensor_update(binary_sensor::BinarySensor *obj) override
ESPPreferenceObject noise_pref_
Definition api_server.h:193
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:179
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:49
void set_noise_psk(psk_t psk)
Definition api_server.h:53
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:153
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:161
void on_shutdown() override
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition api_server.h:150
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:66
Base class for all locks.
Definition lock.h:103
Base-class for all numbers.
Definition number.h:39
Base-class for all selects.
Definition select.h:31
Base-class for all sensors.
Definition sensor.h:59
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