ESPHome 2026.1.0-dev
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_ != nullptr) {
23 ESP_LOGD(TAG, "'%s::%s': Got attribute state %s", this->entity_id_, this->attribute_, ONOFF(new_state));
24 } else {
25 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, ONOFF(new_state));
26 }
27 if (this->initial_) {
28 this->publish_initial_state(new_state);
29 } else {
30 this->publish_state(new_state);
31 }
32 break;
33 }
34 this->initial_ = false;
35 });
36}
38 LOG_BINARY_SENSOR("", "Homeassistant Binary Sensor", this);
39 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
40 if (this->attribute_ != nullptr) {
41 ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_);
42 }
43}
45
46} // namespace homeassistant
47} // namespace esphome
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(std::string)> f)
void publish_state(bool new_state)
Publish a new state to the front-end.
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[4]
APIServer * global_api_server
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:88
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:392
@ PARSE_ON
Definition helpers.h:945
@ PARSE_TOGGLE
Definition helpers.h:947
@ PARSE_OFF
Definition helpers.h:946
@ PARSE_NONE
Definition helpers.h:944