ESPHome  2024.11.0
homeassistant_binary_sensor.cpp
Go to the documentation of this file.
2 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace homeassistant {
7 
8 static const char *const TAG = "homeassistant.binary_sensor";
9 
12  this->entity_id_, this->attribute_, [this](const std::string &state) {
13  auto val = parse_on_off(state.c_str());
14  switch (val) {
15  case PARSE_NONE:
16  case PARSE_TOGGLE:
17  ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str());
18  break;
19  case PARSE_ON:
20  case PARSE_OFF:
21  bool new_state = val == PARSE_ON;
22  if (this->attribute_.has_value()) {
23  ESP_LOGD(TAG, "'%s::%s': Got attribute state %s", this->entity_id_.c_str(),
24  this->attribute_.value().c_str(), ONOFF(new_state));
25  } else {
26  ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state));
27  }
28  if (this->initial_) {
29  this->publish_initial_state(new_state);
30  } else {
31  this->publish_state(new_state);
32  }
33  break;
34  }
35  this->initial_ = false;
36  });
37 }
39  LOG_BINARY_SENSOR("", "Homeassistant Binary Sensor", this);
40  ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
41  if (this->attribute_.has_value()) {
42  ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_.value().c_str());
43  }
44 }
46 
47 } // namespace homeassistant
48 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
void publish_initial_state(bool state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
mopeka_std_values val[4]
bool has_value() const
Definition: optional.h:87
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition: helpers.cpp:400
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
void publish_state(bool state)
Publish a new state to the front-end.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:357
APIServer * global_api_server
Definition: api_server.cpp:347