ESPHome  2024.11.0
api_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 #ifdef USE_API
5 #include "api_noise_context.h"
6 #include "api_pb2.h"
7 #include "api_pb2_service.h"
10 #include "esphome/core/component.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 
19 namespace esphome {
20 namespace api {
21 
22 class APIServer : public Component, public Controller {
23  public:
24  APIServer();
25  void setup() override;
26  uint16_t get_port() const;
27  float get_setup_priority() const override;
28  void loop() override;
29  void dump_config() override;
30  void on_shutdown() override;
31  bool check_password(const std::string &password) const;
32  bool uses_password() const;
33  void set_port(uint16_t port);
34  void set_password(const std::string &password);
35  void set_reboot_timeout(uint32_t reboot_timeout);
36 
37 #ifdef USE_API_NOISE
38  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
39  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
40 #endif // USE_API_NOISE
41 
42  void handle_disconnect(APIConnection *conn);
43 #ifdef USE_BINARY_SENSOR
45 #endif
46 #ifdef USE_COVER
47  void on_cover_update(cover::Cover *obj) override;
48 #endif
49 #ifdef USE_FAN
50  void on_fan_update(fan::Fan *obj) override;
51 #endif
52 #ifdef USE_LIGHT
53  void on_light_update(light::LightState *obj) override;
54 #endif
55 #ifdef USE_SENSOR
56  void on_sensor_update(sensor::Sensor *obj, float state) override;
57 #endif
58 #ifdef USE_SWITCH
59  void on_switch_update(switch_::Switch *obj, bool state) override;
60 #endif
61 #ifdef USE_TEXT_SENSOR
62  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
63 #endif
64 #ifdef USE_CLIMATE
65  void on_climate_update(climate::Climate *obj) override;
66 #endif
67 #ifdef USE_NUMBER
68  void on_number_update(number::Number *obj, float state) override;
69 #endif
70 #ifdef USE_DATETIME_DATE
71  void on_date_update(datetime::DateEntity *obj) override;
72 #endif
73 #ifdef USE_DATETIME_TIME
74  void on_time_update(datetime::TimeEntity *obj) override;
75 #endif
76 #ifdef USE_DATETIME_DATETIME
77  void on_datetime_update(datetime::DateTimeEntity *obj) override;
78 #endif
79 #ifdef USE_TEXT
80  void on_text_update(text::Text *obj, const std::string &state) override;
81 #endif
82 #ifdef USE_SELECT
83  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
84 #endif
85 #ifdef USE_LOCK
86  void on_lock_update(lock::Lock *obj) override;
87 #endif
88 #ifdef USE_VALVE
89  void on_valve_update(valve::Valve *obj) override;
90 #endif
91 #ifdef USE_MEDIA_PLAYER
93 #endif
95  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
96 #ifdef USE_HOMEASSISTANT_TIME
97  void request_time();
98 #endif
99 
100 #ifdef USE_ALARM_CONTROL_PANEL
102 #endif
103 #ifdef USE_EVENT
104  void on_event(event::Event *obj, const std::string &event_type) override;
105 #endif
106 #ifdef USE_UPDATE
107  void on_update(update::UpdateEntity *obj) override;
108 #endif
109 
110  bool is_connected() const;
111 
113  std::string entity_id;
115  std::function<void(std::string)> callback;
116  bool once;
117  };
118 
120  std::function<void(std::string)> f);
121  void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
122  std::function<void(std::string)> f);
123  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
124  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
125 
128  return this->client_disconnected_trigger_;
129  }
130 
131  protected:
132  std::unique_ptr<socket::Socket> socket_ = nullptr;
133  uint16_t port_{6053};
134  uint32_t reboot_timeout_{300000};
135  uint32_t last_connected_{0};
136  std::vector<std::unique_ptr<APIConnection>> clients_;
137  std::string password_;
138  std::vector<HomeAssistantStateSubscription> state_subs_;
139  std::vector<UserServiceDescriptor *> user_services_;
142 
143 #ifdef USE_API_NOISE
144  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
145 #endif // USE_API_NOISE
146 };
147 
148 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
149 
150 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
151  public:
152  bool check(Ts... x) override { return global_api_server->is_connected(); }
153 };
154 
155 } // namespace api
156 } // namespace esphome
157 #endif
Base class for all switches.
Definition: switch.h:39
void handle_disconnect(APIConnection *conn)
Definition: api_server.cpp:177
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
bool is_connected() const
Definition: api_server.cpp:388
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:124
void on_update(update::UpdateEntity *obj) override
Definition: api_server.cpp:339
void on_media_player_update(media_player::MediaPlayer *obj) override
Definition: api_server.cpp:323
uint16_t x
Definition: tt21100.cpp:17
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
Definition: api_server.cpp:296
std::function< void(std::string)> callback
Definition: api_server.h:115
Trigger< std::string, std::string > * client_connected_trigger_
Definition: api_server.h:140
bool check_password(const std::string &password) const
Definition: api_server.cpp:149
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:138
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:206
void set_noise_psk(psk_t psk)
Definition: api_server.h:38
void on_lock_update(lock::Lock *obj) override
Definition: api_server.cpp:305
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:350
void on_cover_update(cover::Cover *obj) override
Definition: api_server.cpp:188
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Definition: api_server.cpp:179
Base-class for all text inputs.
Definition: text.h:24
void loop() override
Definition: api_server.cpp:95
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition: api_server.h:141
void on_fan_update(fan::Fan *obj) override
Definition: api_server.cpp:197
void setup() override
Definition: api_server.cpp:25
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
Definition: api_server.cpp:397
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:144
void on_switch_update(switch_::Switch *obj, bool state) override
Definition: api_server.cpp:224
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:39
void dump_config() override
Definition: api_server.cpp:139
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition: api_server.h:126
void on_date_update(datetime::DateEntity *obj) override
Definition: api_server.cpp:260
void on_datetime_update(datetime::DateTimeEntity *obj) override
Definition: api_server.cpp:278
Base class for all automation conditions.
Definition: automation.h:74
void on_text_update(text::Text *obj, const std::string &state) override
Definition: api_server.cpp:287
void on_number_update(number::Number *obj, float state) override
Definition: api_server.cpp:251
Base-class for all numbers.
Definition: number.h:39
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:379
void on_climate_update(climate::Climate *obj) override
Definition: api_server.cpp:242
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Definition: api_server.cpp:233
void register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:95
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:139
void set_port(uint16_t port)
Definition: api_server.cpp:346
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:375
void on_sensor_update(sensor::Sensor *obj, float state) override
Definition: api_server.cpp:215
void on_event(event::Event *obj, const std::string &event_type) override
Definition: api_server.cpp:332
bool uses_password() const
Definition: api_server.cpp:148
Base-class for all selects.
Definition: select.h:31
std::vector< std::unique_ptr< APIConnection > > clients_
Definition: api_server.h:136
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all valve devices.
Definition: valve.h:105
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
bool check(Ts... x) override
Definition: api_server.h:152
float get_setup_priority() const override
Definition: api_server.cpp:345
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:357
void on_shutdown() override
Definition: api_server.cpp:389
Base-class for all sensors.
Definition: sensor.h:57
void get_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:366
uint16_t get_port() const
Definition: api_server.cpp:378
void on_valve_update(valve::Valve *obj) override
Definition: api_server.cpp:314
std::array< uint8_t, 32 > psk_t
Trigger< std::string, std::string > * get_client_disconnected_trigger() const
Definition: api_server.h:127
void set_password(const std::string &password)
Definition: api_server.cpp:349
APIServer * global_api_server
Definition: api_server.cpp:347
std::unique_ptr< socket::Socket > socket_
Definition: api_server.h:132
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
bool state
Definition: fan.h:34
void on_time_update(datetime::TimeEntity *obj) override
Definition: api_server.cpp:269