ESPHome 2025.5.0
Loading...
Searching...
No Matches
homeassistant_binary_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace homeassistant {
7
8static 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
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void publish_initial_state(bool state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
bool state
The current reported state of the binary sensor.
void publish_state(bool state)
Publish a new state to the front-end.
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
mopeka_std_values val[4]
APIServer * global_api_server
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:26
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:420
@ PARSE_ON
Definition helpers.h:442
@ PARSE_TOGGLE
Definition helpers.h:444
@ PARSE_OFF
Definition helpers.h:443
@ PARSE_NONE
Definition helpers.h:441