22 static const char *
const TAG =
"api";
26 ESP_LOGCONFIG(TAG,
"Setting up Home Assistant API server...");
30 ESP_LOGW(TAG,
"Could not create socket.");
35 int err =
socket_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
37 ESP_LOGW(TAG,
"Socket unable to set reuseaddr: errno %d", err);
40 err =
socket_->setblocking(
false);
42 ESP_LOGW(TAG,
"Socket unable to set nonblocking mode: errno %d", err);
51 ESP_LOGW(TAG,
"Socket unable to set sockaddr: errno %d", errno);
58 ESP_LOGW(TAG,
"Socket unable to bind: errno %d", errno);
65 ESP_LOGW(TAG,
"Socket unable to listen: errno %d", errno);
75 c->send_log_message(level, tag, message);
83 #ifdef USE_ESP32_CAMERA 86 [
this](
const std::shared_ptr<esp32_camera::CameraImage> &image) {
89 c->send_camera_state(image);
100 auto sock =
socket_->accept((
struct sockaddr *) &source_addr, &addr_len);
103 ESP_LOGD(TAG,
"Accepted %s", sock->getpeername().c_str());
112 [](
const std::unique_ptr<APIConnection> &conn) {
return !conn->remove_; });
114 for (
auto it = new_end; it != this->
clients_.end(); ++it) {
116 ESP_LOGV(TAG,
"Removing connection to %s", (*it)->client_info_.c_str());
121 for (
auto &client : this->
clients_) {
126 const uint32_t now =
millis();
129 ESP_LOGE(TAG,
"No client connected to API. Rebooting...");
140 ESP_LOGCONFIG(TAG,
"API Server:");
143 ESP_LOGCONFIG(TAG,
" Using noise encryption: YES");
145 ESP_LOGCONFIG(TAG,
" Using noise encryption: NO");
152 uint32_t len_a = this->
password_.length();
153 const char *b = password.c_str();
154 uint32_t len_b = password.length();
157 volatile uint32_t
length = len_b;
158 volatile const char *left =
nullptr;
159 volatile const char *right = b;
162 if (len_a == length) {
163 left = *((
volatile const char **) &a);
166 if (len_a != length) {
171 for (
size_t i = 0; i <
length; i++) {
172 result |= *left++ ^ *right++;
178 #ifdef USE_BINARY_SENSOR 183 c->send_binary_sensor_state(obj, state);
192 c->send_cover_state(obj);
201 c->send_fan_state(obj);
210 c->send_light_state(obj);
219 c->send_sensor_state(obj, state);
228 c->send_switch_state(obj, state);
232 #ifdef USE_TEXT_SENSOR 237 c->send_text_sensor_state(obj, state);
246 c->send_climate_state(obj);
255 c->send_number_state(obj, state);
259 #ifdef USE_DATETIME_DATE 264 c->send_date_state(obj);
268 #ifdef USE_DATETIME_TIME 273 c->send_time_state(obj);
277 #ifdef USE_DATETIME_DATETIME 282 c->send_datetime_state(obj);
291 c->send_text_state(obj, state);
300 c->send_select_state(obj, state);
309 c->send_lock_state(obj, obj->
state);
318 c->send_valve_state(obj);
322 #ifdef USE_MEDIA_PLAYER 327 c->send_media_player_state(obj);
334 c->send_event(obj, event_type);
341 c->send_update_state(obj);
351 for (
auto &client : this->
clients_) {
352 client->send_homeassistant_service_call(call);
358 std::function<
void(std::string)> f) {
361 .attribute = std::move(attribute),
362 .callback = std::move(f),
367 std::function<
void(std::string)> f) {
370 .attribute = std::move(attribute),
371 .callback = std::move(f),
380 #ifdef USE_HOMEASSISTANT_TIME 382 for (
auto &client : this->
clients_) {
383 if (!client->remove_ && client->is_authenticated())
384 client->send_time_request();
396 #ifdef USE_ALARM_CONTROL_PANEL 401 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