ESPHome  2022.6.3
api_server.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/defines.h"
6 #include "esphome/core/log.h"
8 #include "api_pb2.h"
9 #include "api_pb2_service.h"
10 #include "list_entities.h"
11 #include "subscribe_state.h"
12 #include "user_services.h"
13 #include "api_noise_context.h"
14 
15 namespace esphome {
16 namespace api {
17 
18 class APIServer : public Component, public Controller {
19  public:
20  APIServer();
21  void setup() override;
22  uint16_t get_port() const;
23  float get_setup_priority() const override;
24  void loop() override;
25  void dump_config() override;
26  void on_shutdown() override;
27  bool check_password(const std::string &password) const;
28  bool uses_password() const;
29  void set_port(uint16_t port);
30  void set_password(const std::string &password);
31  void set_reboot_timeout(uint32_t reboot_timeout);
32 
33 #ifdef USE_API_NOISE
34  void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
35  std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
36 #endif // USE_API_NOISE
37 
38  void handle_disconnect(APIConnection *conn);
39 #ifdef USE_BINARY_SENSOR
41 #endif
42 #ifdef USE_COVER
43  void on_cover_update(cover::Cover *obj) override;
44 #endif
45 #ifdef USE_FAN
46  void on_fan_update(fan::Fan *obj) override;
47 #endif
48 #ifdef USE_LIGHT
49  void on_light_update(light::LightState *obj) override;
50 #endif
51 #ifdef USE_SENSOR
52  void on_sensor_update(sensor::Sensor *obj, float state) override;
53 #endif
54 #ifdef USE_SWITCH
55  void on_switch_update(switch_::Switch *obj, bool state) override;
56 #endif
57 #ifdef USE_TEXT_SENSOR
58  void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
59 #endif
60 #ifdef USE_CLIMATE
61  void on_climate_update(climate::Climate *obj) override;
62 #endif
63 #ifdef USE_NUMBER
64  void on_number_update(number::Number *obj, float state) override;
65 #endif
66 #ifdef USE_SELECT
67  void on_select_update(select::Select *obj, const std::string &state, size_t index) override;
68 #endif
69 #ifdef USE_LOCK
70  void on_lock_update(lock::Lock *obj) override;
71 #endif
72 #ifdef USE_MEDIA_PLAYER
74 #endif
76  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
77 #ifdef USE_HOMEASSISTANT_TIME
78  void request_time();
79 #endif
80 
81  bool is_connected() const;
82 
84  std::string entity_id;
86  std::function<void(std::string)> callback;
87  };
88 
90  std::function<void(std::string)> f);
91  const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
92  const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
93 
94  protected:
95  std::unique_ptr<socket::Socket> socket_ = nullptr;
96  uint16_t port_{6053};
97  uint32_t reboot_timeout_{300000};
98  uint32_t last_connected_{0};
99  std::vector<std::unique_ptr<APIConnection>> clients_;
100  std::string password_;
101  std::vector<HomeAssistantStateSubscription> state_subs_;
102  std::vector<UserServiceDescriptor *> user_services_;
103 
104 #ifdef USE_API_NOISE
105  std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
106 #endif // USE_API_NOISE
107 };
108 
109 extern APIServer *global_api_server; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
110 
111 template<typename... Ts> class APIConnectedCondition : public Condition<Ts...> {
112  public:
113  bool check(Ts... x) override { return global_api_server->is_connected(); }
114 };
115 
116 } // namespace api
117 } // namespace esphome
Base class for all switches.
Definition: switch.h:33
void handle_disconnect(APIConnection *conn)
Definition: api_server.cpp:175
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:32
bool is_connected() const
Definition: api_server.cpp:316
Base class for all cover devices.
Definition: cover.h:111
const std::vector< UserServiceDescriptor * > & get_user_services() const
Definition: api_server.h:92
void on_media_player_update(media_player::MediaPlayer *obj) override
Definition: api_server.cpp:276
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
Definition: api_server.cpp:258
std::function< void(std::string)> callback
Definition: api_server.h:86
bool check_password(const std::string &password) const
Definition: api_server.cpp:147
std::vector< HomeAssistantStateSubscription > state_subs_
Definition: api_server.h:101
void on_light_update(light::LightState *obj) override
Definition: api_server.cpp:204
void set_noise_psk(psk_t psk)
Definition: api_server.h:34
void on_lock_update(lock::Lock *obj) override
Definition: api_server.cpp:267
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:289
void on_cover_update(cover::Cover *obj) override
Definition: api_server.cpp:186
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Definition: api_server.cpp:177
void loop() override
Definition: api_server.cpp:94
void on_fan_update(fan::Fan *obj) override
Definition: api_server.cpp:195
void setup() override
Definition: api_server.cpp:24
std::shared_ptr< APINoiseContext > noise_ctx_
Definition: api_server.h:105
void on_switch_update(switch_::Switch *obj, bool state) override
Definition: api_server.cpp:222
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition: api_server.h:35
void dump_config() override
Definition: api_server.cpp:137
Base class for all automation conditions.
Definition: automation.h:74
void on_number_update(number::Number *obj, float state) override
Definition: api_server.cpp:249
Base-class for all numbers.
Definition: number.h:29
void set_reboot_timeout(uint32_t reboot_timeout)
Definition: api_server.cpp:307
void on_climate_update(climate::Climate *obj) override
Definition: api_server.cpp:240
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Definition: api_server.cpp:231
void register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:76
std::vector< UserServiceDescriptor * > user_services_
Definition: api_server.h:102
void set_port(uint16_t port)
Definition: api_server.cpp:285
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
Definition: api_server.cpp:303
void on_sensor_update(sensor::Sensor *obj, float state) override
Definition: api_server.cpp:213
bool uses_password() const
Definition: api_server.cpp:146
Base-class for all selects.
Definition: select.h:24
std::vector< std::unique_ptr< APIConnection > > clients_
Definition: api_server.h:99
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
bool check(Ts... x) override
Definition: api_server.h:113
float get_setup_priority() const override
Definition: api_server.cpp:284
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:295
void on_shutdown() override
Definition: api_server.cpp:317
Base-class for all sensors.
Definition: sensor.h:47
uint16_t get_port() const
Definition: api_server.cpp:306
std::array< uint8_t, 32 > psk_t
void set_password(const std::string &password)
Definition: api_server.cpp:288
APIServer * global_api_server
Definition: api_server.cpp:286
std::unique_ptr< socket::Socket > socket_
Definition: api_server.h:95
Base class for all locks.
Definition: lock.h:103
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
bool state
Definition: fan.h:34