ESPHome 2025.6.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#include "user_services.h"
16
17#include <vector>
18
19namespace esphome {
20namespace api {
21
22#ifdef USE_API_NOISE
25} PACKED; // NOLINT
26#endif
27
28class APIServer : public Component, public Controller {
29 public:
30 APIServer();
31 void setup() override;
32 uint16_t get_port() const;
33 float get_setup_priority() const override;
34 void loop() override;
35 void dump_config() override;
36 void on_shutdown() override;
37 bool teardown() override;
38 bool check_password(const std::string &password) const;
39 bool uses_password() const;
40 void set_port(uint16_t port);
41 void set_password(const std::string &password);
42 void set_reboot_timeout(uint32_t reboot_timeout);
43 void set_batch_delay(uint32_t batch_delay);
44 uint32_t get_batch_delay() const { return batch_delay_; }
45
46 // Get reference to shared buffer for API connections
47 std::vector<uint8_t> &get_shared_buffer_ref() { return shared_write_buffer_; }
48
49#ifdef USE_API_NOISE
50 bool save_noise_psk(psk_t psk, bool make_active = true);
51 void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
52 std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
53#endif // USE_API_NOISE
54
56#ifdef USE_BINARY_SENSOR
58#endif
59#ifdef USE_COVER
60 void on_cover_update(cover::Cover *obj) override;
61#endif
62#ifdef USE_FAN
63 void on_fan_update(fan::Fan *obj) override;
64#endif
65#ifdef USE_LIGHT
66 void on_light_update(light::LightState *obj) override;
67#endif
68#ifdef USE_SENSOR
69 void on_sensor_update(sensor::Sensor *obj, float state) override;
70#endif
71#ifdef USE_SWITCH
72 void on_switch_update(switch_::Switch *obj, bool state) override;
73#endif
74#ifdef USE_TEXT_SENSOR
75 void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
76#endif
77#ifdef USE_CLIMATE
78 void on_climate_update(climate::Climate *obj) override;
79#endif
80#ifdef USE_NUMBER
81 void on_number_update(number::Number *obj, float state) override;
82#endif
83#ifdef USE_DATETIME_DATE
84 void on_date_update(datetime::DateEntity *obj) override;
85#endif
86#ifdef USE_DATETIME_TIME
87 void on_time_update(datetime::TimeEntity *obj) override;
88#endif
89#ifdef USE_DATETIME_DATETIME
91#endif
92#ifdef USE_TEXT
93 void on_text_update(text::Text *obj, const std::string &state) override;
94#endif
95#ifdef USE_SELECT
96 void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
97#endif
98#ifdef USE_LOCK
99 void on_lock_update(lock::Lock *obj) override;
100#endif
101#ifdef USE_VALVE
102 void on_valve_update(valve::Valve *obj) override;
103#endif
104#ifdef USE_MEDIA_PLAYER
106#endif
108 void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
109#ifdef USE_HOMEASSISTANT_TIME
110 void request_time();
111#endif
112
113#ifdef USE_ALARM_CONTROL_PANEL
115#endif
116#ifdef USE_EVENT
117 void on_event(event::Event *obj, const std::string &event_type) override;
118#endif
119#ifdef USE_UPDATE
120 void on_update(update::UpdateEntity *obj) override;
121#endif
122
123 bool is_connected() const;
124
126 std::string entity_id;
128 std::function<void(std::string)> callback;
129 bool once;
130 };
131
132 void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
133 std::function<void(std::string)> f);
134 void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
135 std::function<void(std::string)> f);
136 const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
137 const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
138
143
144 protected:
145 bool shutting_down_ = false;
146 std::unique_ptr<socket::Socket> socket_ = nullptr;
147 uint16_t port_{6053};
148 uint32_t reboot_timeout_{300000};
149 uint32_t batch_delay_{100};
150 uint32_t last_connected_{0};
151 std::vector<std::unique_ptr<APIConnection>> clients_;
152 std::string password_;
153 std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
154 std::vector<HomeAssistantStateSubscription> state_subs_;
155 std::vector<UserServiceDescriptor *> user_services_;
158
159#ifdef USE_API_NOISE
160 std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
162#endif // USE_API_NOISE
163};
164
165extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
166
167template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
168 public:
169 bool check(Ts... x) override { return global_api_server->is_connected(); }
170};
171
172} // namespace api
173} // namespace esphome
174#endif
Base class for all automation conditions.
Definition automation.h:75
bool check(Ts... x) override
Definition api_server.h:169
void set_batch_delay(uint32_t batch_delay)
void on_valve_update(valve::Valve *obj) override
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition api_server.h:52
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:151
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_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
void on_sensor_update(sensor::Sensor *obj, float state) override
std::vector< UserServiceDescriptor * > user_services_
Definition api_server.h:155
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:137
void set_port(uint16_t port)
void dump_config() override
void handle_disconnect(APIConnection *conn)
void set_reboot_timeout(uint32_t reboot_timeout)
Trigger< std::string, std::string > * client_connected_trigger_
Definition api_server.h:156
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:108
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:160
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:157
std::vector< uint8_t > shared_write_buffer_
Definition api_server.h:153
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
uint32_t get_batch_delay() const
Definition api_server.h:44
ESPPreferenceObject noise_pref_
Definition api_server.h:161
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:154
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:47
void set_noise_psk(psk_t psk)
Definition api_server.h:51
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:140
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:146
void on_shutdown() override
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition api_server.h:139
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:63
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:62
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5