ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
homeassistant_binary_sensor.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
5
7
8static const char *const TAG = "homeassistant.binary_sensor";
9
12 auto val = parse_on_off(state.c_str());
13 switch (val) {
14 case PARSE_NONE:
15 case PARSE_TOGGLE:
16 ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str());
17 break;
18 case PARSE_ON:
19 case PARSE_OFF:
20 bool new_state = val == PARSE_ON;
21 if (this->attribute_ != nullptr) {
22 ESP_LOGD(TAG, "'%s::%s': Got attribute state %s", this->entity_id_, this->attribute_, ONOFF(new_state));
23 } else {
24 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, ONOFF(new_state));
25 }
26 if (this->initial_) {
27 this->publish_initial_state(new_state);
28 } else {
29 this->publish_state(new_state);
30 }
31 break;
32 }
33 this->initial_ = false;
34 });
35}
37 LOG_BINARY_SENSOR("", "Homeassistant Binary Sensor", this);
38 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
39 if (this->attribute_ != nullptr) {
40 ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_);
41 }
42}
44
45} // namespace esphome::homeassistant
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
void publish_state(bool new_state)
Publish a new state to the front-end.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
void publish_initial_state(bool new_state)
Publish the initial state, this will not make the callback manager send callbacks and is meant only f...
mopeka_std_values val[3]
APIServer * global_api_server
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:53
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
@ PARSE_ON
Definition helpers.h:1553
@ PARSE_TOGGLE
Definition helpers.h:1555
@ PARSE_OFF
Definition helpers.h:1554
@ PARSE_NONE
Definition helpers.h:1552