ESPHome  2024.5.5
api_server.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "api_noise_context.h"
4 #include "api_pb2.h"
5 #include "api_pb2_service.h"
10 #include "esphome/core/defines.h"
11 #include "esphome/core/log.h"
12 #include "list_entities.h"
13 #include "subscribe_state.h"
14 #include "user_services.h"
15 
16 #include <vector>
17 
18 namespace esphome {
19 namespace api {
20 
21 class APIServer : public Component, public Controller {
22  public:
23  APIServer();
24  void setup() override;
25  uint16_t get_port() const;
26  float get_setup_priority() const override;
27  void loop() override;
28  void dump_config() override;
29  void on_shutdown() override;
30  bool check_password(const std::string &password) const;
31  bool uses_password() const;
32  void set_port(uint16_t port);
33  void set_password(const std::string &password);
34  void set_reboot_timeout(uint32_t reboot_timeout);
35 
36 #ifdef USE_API_NOISE
37  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
38  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
39 #endif // USE_API_NOISE
40 
41  void handle_disconnect(APIConnection *conn);
42 #ifdef USE_BINARY_SENSOR
44 #endif
45 #ifdef USE_COVER
46  void on_cover_update(cover::Cover *obj) override;
47 #endif
48 #ifdef USE_FAN
49  void on_fan_update(fan::Fan *obj) override;
50 #endif
51 #ifdef USE_LIGHT
52  void on_light_update(light::LightState *obj) override;
53 #endif
54 #ifdef USE_SENSOR
55  void on_sensor_update(sensor::Sensor *obj, float state) override;
56 #endif
57 #ifdef USE_SWITCH
58  void on_switch_update(switch_::Switch *obj, bool state) override;
59 #endif
60 #ifdef USE_TEXT_SENSOR
61  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
62 #endif
63 #ifdef USE_CLIMATE
64  void on_climate_update(climate::Climate *obj) override;
65 #endif
66 #ifdef USE_NUMBER
67  void on_number_update(number::Number *obj, float state) override;
68 #endif
69 #ifdef USE_DATETIME_DATE
70  void on_date_update(datetime::DateEntity *obj) override;
71 #endif
72 #ifdef USE_DATETIME_TIME
73  void on_time_update(datetime::TimeEntity *obj) override;
74 #endif
75 #ifdef USE_DATETIME_DATETIME
76  void on_datetime_update(datetime::DateTimeEntity *obj) override;
77 #endif
78 #ifdef USE_TEXT
79  void on_text_update(text::Text *obj, const std::string &state) override;
80 #endif
81 #ifdef USE_SELECT
82  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
83 #endif
84 #ifdef USE_LOCK
85  void on_lock_update(lock::Lock *obj) override;
86 #endif
87 #ifdef USE_VALVE
88  void on_valve_update(valve::Valve *obj) override;
89 #endif
90 #ifdef USE_MEDIA_PLAYER
92 #endif
94  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
95 #ifdef USE_HOMEASSISTANT_TIME
96  void request_time();
97 #endif
98 
99 #ifdef USE_ALARM_CONTROL_PANEL
101 #endif
102 #ifdef USE_EVENT
103  void on_event(event::Event *obj, const std::string &event_type) override;
104 #endif
105 
106  bool is_connected() const;
107 
109  std::string entity_id;
111  std::function<void(std::string)> callback;
112  };
113 
115  std::function<void(std::string)> f);
116  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
117  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
118 
121  return this->client_disconnected_trigger_;
122  }
123 
124  protected:
125  std::unique_ptr<socket::Socket> socket_ = nullptr;
126  uint16_t port_{6053};
127  uint32_t reboot_timeout_{300000};
128  uint32_t last_connected_{0};
129  std::vector<std::unique_ptr<APIConnection>> clients_;
130  std::string password_;
131  std::vector<HomeAssistantStateSubscription> state_subs_;
132  std::vector<UserServiceDescriptor *> user_services_;
135 
136 #ifdef USE_API_NOISE
137  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
138 #endif // USE_API_NOISE
139 };
140 
141 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
142 
143 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
144  public:
145  bool check(Ts... x) override { return global_api_server->is_connected(); }
146 };
147 
148 } // namespace api
149 } // namespace esphome
Base class for all switches.
Definition: switch.h:39
void handle_disconnect(APIConnection *conn)
Definition: api_server.cpp:176
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
bool is_connected() const
Definition: api_server.cpp:370
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:117
void on_media_player_update(media_player::MediaPlayer *obj) override
Definition: api_server.cpp:322
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:295
std::function< void(std::string)> callback
Definition: api_server.h:111
Trigger< std::string, std::string > * client_connected_trigger_
Definition: api_server.h:133
bool check_password(const std::string &password) const
Definition: api_server.cpp:148
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:131
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:205
void set_noise_psk(psk_t psk)
Definition: api_server.h:37
void on_lock_update(lock::Lock *obj) override
Definition: api_server.cpp:304
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:342
void on_cover_update(cover::Cover *obj) override
Definition: api_server.cpp:187
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Definition: api_server.cpp:178
Base-class for all text inputs.
Definition: text.h:24
void loop() override
Definition: api_server.cpp:94
Trigger< std::string, std::string > * client_disconnected_trigger_
Definition: api_server.h:134
void on_fan_update(fan::Fan *obj) override
Definition: api_server.cpp:196
void setup() override
Definition: api_server.cpp:24
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
Definition: api_server.cpp:379
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:137
void on_switch_update(switch_::Switch *obj, bool state) override
Definition: api_server.cpp:223
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:38
void dump_config() override
Definition: api_server.cpp:138
Trigger< std::string, std::string > * get_client_connected_trigger() const
Definition: api_server.h:119
void on_date_update(datetime::DateEntity *obj) override
Definition: api_server.cpp:259
void on_datetime_update(datetime::DateTimeEntity *obj) override
Definition: api_server.cpp:277
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:286
void on_number_update(number::Number *obj, float state) override
Definition: api_server.cpp:250
Base-class for all numbers.
Definition: number.h:39
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:361
void on_climate_update(climate::Climate *obj) override
Definition: api_server.cpp:241
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Definition: api_server.cpp:232
void register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:94
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:132
void set_port(uint16_t port)
Definition: api_server.cpp:338
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:357
void on_sensor_update(sensor::Sensor *obj, float state) override
Definition: api_server.cpp:214
void on_event(event::Event *obj, const std::string &event_type) override
Definition: api_server.cpp:331
bool uses_password() const
Definition: api_server.cpp:147
Base-class for all selects.
Definition: select.h:31
std::vector< std::unique_ptr< APIConnection > > clients_
Definition: api_server.h:129
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:145
float get_setup_priority() const override
Definition: api_server.cpp:337
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:349
void on_shutdown() override
Definition: api_server.cpp:371
Base-class for all sensors.
Definition: sensor.h:57
uint16_t get_port() const
Definition: api_server.cpp:360
void on_valve_update(valve::Valve *obj) override
Definition: api_server.cpp:313
std::array< uint8_t, 32 > psk_t
Trigger< std::string, std::string > * get_client_disconnected_trigger() const
Definition: api_server.h:120
void set_password(const std::string &password)
Definition: api_server.cpp:341
APIServer * global_api_server
Definition: api_server.cpp:339
std::unique_ptr< socket::Socket > socket_
Definition: api_server.h:125
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:268