21 static const char *
const TAG =
"api";
25 ESP_LOGCONFIG(TAG,
"Setting up Home Assistant API server...");
29 ESP_LOGW(TAG,
"Could not create socket.");
34 int err =
socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
36 ESP_LOGW(TAG,
"Socket unable to set reuseaddr: errno %d", err);
39 err =
socket_->setblocking(
false);
41 ESP_LOGW(TAG,
"Socket unable to set nonblocking mode: errno %d", err);
50 ESP_LOGW(TAG,
"Socket unable to set sockaddr: errno %d", errno);
57 ESP_LOGW(TAG,
"Socket unable to bind: errno %d", errno);
64 ESP_LOGW(TAG,
"Socket unable to listen: errno %d", errno);
74 c->send_log_message(level, tag, message);
82 #ifdef USE_ESP32_CAMERA 85 [
this](
const std::shared_ptr<esp32_camera::CameraImage> &image) {
88 c->send_camera_state(image);
99 auto sock =
socket_->accept((
struct sockaddr *) &source_addr, &addr_len);
102 ESP_LOGD(TAG,
"Accepted %s", sock->getpeername().c_str());
111 [](
const std::unique_ptr<APIConnection> &conn) {
return !conn->remove_; });
113 for (
auto it = new_end; it != this->
clients_.end(); ++it) {
115 ESP_LOGV(TAG,
"Removing connection to %s", (*it)->client_info_.c_str());
120 for (
auto &client : this->
clients_) {
125 const uint32_t now =
millis();
128 ESP_LOGE(TAG,
"No client connected to API. Rebooting...");
139 ESP_LOGCONFIG(TAG,
"API Server:");
142 ESP_LOGCONFIG(TAG,
" Using noise encryption: YES");
144 ESP_LOGCONFIG(TAG,
" Using noise encryption: NO");
151 uint32_t len_a = this->
password_.length();
152 const char *b = password.c_str();
153 uint32_t len_b = password.length();
156 volatile uint32_t
length = len_b;
157 volatile const char *left =
nullptr;
158 volatile const char *right = b;
161 if (len_a == length) {
162 left = *((
volatile const char **) &a);
165 if (len_a != length) {
170 for (
size_t i = 0; i <
length; i++) {
171 result |= *left++ ^ *right++;
177 #ifdef USE_BINARY_SENSOR 182 c->send_binary_sensor_state(obj, state);
191 c->send_cover_state(obj);
200 c->send_fan_state(obj);
209 c->send_light_state(obj);
218 c->send_sensor_state(obj, state);
227 c->send_switch_state(obj, state);
231 #ifdef USE_TEXT_SENSOR 236 c->send_text_sensor_state(obj, state);
245 c->send_climate_state(obj);
254 c->send_number_state(obj, state);
258 #ifdef USE_DATETIME_DATE 263 c->send_date_state(obj);
267 #ifdef USE_DATETIME_TIME 272 c->send_time_state(obj);
276 #ifdef USE_DATETIME_DATETIME 281 c->send_datetime_state(obj);
290 c->send_text_state(obj, state);
299 c->send_select_state(obj, state);
308 c->send_lock_state(obj, obj->
state);
317 c->send_valve_state(obj);
321 #ifdef USE_MEDIA_PLAYER 326 c->send_media_player_state(obj);
333 c->send_event(obj, event_type);
340 c->send_update_state(obj);
350 for (
auto &client : this->
clients_) {
351 client->send_homeassistant_service_call(call);
357 std::function<
void(std::string)> f) {
360 .attribute = std::move(attribute),
361 .callback = std::move(f),
366 std::function<
void(std::string)> f) {
369 .attribute = std::move(attribute),
370 .callback = std::move(f),
379 #ifdef USE_HOMEASSISTANT_TIME 381 for (
auto &client : this->
clients_) {
382 if (!client->remove_ && client->is_authenticated())
383 client->send_time_request();
395 #ifdef USE_ALARM_CONTROL_PANEL 400 c->send_alarm_control_panel_state(obj);
Base class for all switches.
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol...
void handle_disconnect(APIConnection *conn)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
bool is_connected() const
void add_on_log_callback(std::function< void(int, const char *, const char *)> &&callback)
Register a callback that will be called for every log message sent.
Base class for all cover devices.
void on_update(update::UpdateEntity *obj) override
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
std::string get_use_address()
Get the active network hostname.
void status_set_warning(const char *message="unspecified")
LockState state
The current reported state of the lock.
void on_media_player_update(media_player::MediaPlayer *obj) override
void on_select_update(select::Select *obj, const std::string &state, size_t index) override
bool check_password(const std::string &password) const
std::vector< HomeAssistantStateSubscription > state_subs_
void on_light_update(light::LightState *obj) override
void on_lock_update(lock::Lock *obj) override
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&callback)
void on_cover_update(cover::Cover *obj) override
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override
Base-class for all text inputs.
Trigger< std::string, std::string > * client_disconnected_trigger_
uint32_t IRAM_ATTR HOT millis()
void on_fan_update(fan::Fan *obj) override
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override
void on_switch_update(switch_::Switch *obj, bool state) override
void dump_config() override
void on_date_update(datetime::DateEntity *obj) override
void on_datetime_update(datetime::DateTimeEntity *obj) override
ESP32Camera * global_esp32_camera
void on_text_update(text::Text *obj, const std::string &state) override
void on_number_update(number::Number *obj, float state) override
Base-class for all numbers.
void status_clear_warning()
void set_reboot_timeout(uint32_t reboot_timeout)
void on_climate_update(climate::Climate *obj) override
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override
Application App
Global storage of Application pointer - only one Application can exist.
void set_port(uint16_t port)
const std::vector< HomeAssistantStateSubscription > & get_state_subs() const
virtual void mark_failed()
Mark this component as failed.
void on_sensor_update(sensor::Sensor *obj, float state) override
void setup_controller(bool include_internal=false)
void on_event(event::Event *obj, const std::string &event_type) override
bool uses_password() const
Base-class for all selects.
std::vector< std::unique_ptr< APIConnection > > clients_
Implementation of SPI Controller mode.
Base class for all valve devices.
Base class for all binary_sensor-type classes.
float get_setup_priority() const override
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void on_shutdown() override
Base-class for all sensors.
void get_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
uint16_t get_port() const
void on_valve_update(valve::Valve *obj) override
void set_password(const std::string &password)
APIServer * global_api_server
std::unique_ptr< socket::Socket > socket_
Base class for all locks.
ClimateDevice - This is the base class for all climate integrations.
void IRAM_ATTR HOT delay(uint32_t ms)
void on_time_update(datetime::TimeEntity *obj) override