ESPHome 2025.5.0
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 check_password(const std::string &password) const;
38 bool uses_password() const;
39 void set_port(uint16_t port);
40 void set_password(const std::string &password);
41 void set_reboot_timeout(uint32_t reboot_timeout);
42
43#ifdef USE_API_NOISE
44 bool save_noise_psk(psk_t psk, bool make_active = true);
45 void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
46 std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
47#endif // USE_API_NOISE
48
50#ifdef USE_BINARY_SENSOR
52#endif
53#ifdef USE_COVER
54 void on_cover_update(cover::Cover *obj) override;
55#endif
56#ifdef USE_FAN
57 void on_fan_update(fan::Fan *obj) override;
58#endif
59#ifdef USE_LIGHT
60 void on_light_update(light::LightState *obj) override;
61#endif
62#ifdef USE_SENSOR
63 void on_sensor_update(sensor::Sensor *obj, float state) override;
64#endif
65#ifdef USE_SWITCH
66 void on_switch_update(switch_::Switch *obj, bool state) override;
67#endif
68#ifdef USE_TEXT_SENSOR
69 void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
70#endif
71#ifdef USE_CLIMATE
72 void on_climate_update(climate::Climate *obj) override;
73#endif
74#ifdef USE_NUMBER
75 void on_number_update(number::Number *obj, float state) override;
76#endif
77#ifdef USE_DATETIME_DATE
78 void on_date_update(datetime::DateEntity *obj) override;
79#endif
80#ifdef USE_DATETIME_TIME
81 void on_time_update(datetime::TimeEntity *obj) override;
82#endif
83#ifdef USE_DATETIME_DATETIME
85#endif
86#ifdef USE_TEXT
87 void on_text_update(text::Text *obj, const std::string &state) override;
88#endif
89#ifdef USE_SELECT
90 void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
91#endif
92#ifdef USE_LOCK
93 void on_lock_update(lock::Lock *obj) override;
94#endif
95#ifdef USE_VALVE
96 void on_valve_update(valve::Valve *obj) override;
97#endif
98#ifdef USE_MEDIA_PLAYER
100#endif
102 void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
103#ifdef USE_HOMEASSISTANT_TIME
104 void request_time();
105#endif
106
107#ifdef USE_ALARM_CONTROL_PANEL
109#endif
110#ifdef USE_EVENT
111 void on_event(event::Event *obj, const std::string &event_type) override;
112#endif
113#ifdef USE_UPDATE
114 void on_update(update::UpdateEntity *obj) override;
115#endif
116
117 bool is_connected() const;
118
120 std::string entity_id;
122 std::function<void(std::string)> callback;
123 bool once;
124 };
125
126 void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
127 std::function<void(std::string)> f);
128 void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
129 std::function<void(std::string)> f);
130 const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
131 const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
132
137
138 protected:
139 std::unique_ptr<socket::Socket> socket_ = nullptr;
140 uint16_t port_{6053};
141 uint32_t reboot_timeout_{300000};
142 uint32_t last_connected_{0};
143 std::vector<std::unique_ptr<APIConnection>> clients_;
144 std::string password_;
145 std::vector<HomeAssistantStateSubscription> state_subs_;
146 std::vector<UserServiceDescriptor *> user_services_;
149
150#ifdef USE_API_NOISE
151 std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
153#endif // USE_API_NOISE
154};
155
156extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
157
158template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
159 public:
160 bool check(Ts... x) override { return global_api_server->is_connected(); }
161};
162
163} // namespace api
164} // namespace esphome
165#endif
Base class for all automation conditions.
Definition automation.h:75
bool check(Ts... x) override
Definition api_server.h:160
void on_valve_update(valve::Valve *obj) override
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition api_server.h:46
std::vector< std::unique_ptr< APIConnection > > clients_
Definition api_server.h:143
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:146
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:131
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:147
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:102
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 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:151
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:148
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
ESPPreferenceObject noise_pref_
Definition api_server.h:152
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:145
void on_switch_update(switch_::Switch *obj, bool state) override
uint16_t get_port() const
void set_noise_psk(psk_t psk)
Definition api_server.h:45
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:134
float get_setup_priority() const override
std::unique_ptr< socket::Socket > socket_
Definition api_server.h:139
void on_shutdown() override
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition api_server.h:133
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:57
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