ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
homeassistant_switch.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "homeassistant.switch");
10
11using namespace esphome::switch_;
12
15 auto val = parse_on_off(state.c_str());
16 switch (val) {
17 case PARSE_NONE:
18 case PARSE_TOGGLE:
19 ESP_LOGW(TAG, "Can't convert '%s' to binary state!", state.c_str());
20 break;
21 case PARSE_ON:
22 case PARSE_OFF:
23 bool new_state = val == PARSE_ON;
24 ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_, ONOFF(new_state));
25 this->publish_state(new_state);
26 break;
27 }
28 });
29}
30
32 LOG_SWITCH("", "Homeassistant Switch", this);
33 ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_);
34}
35
37
39 if (!api::global_api_server->is_connected()) {
40 ESP_LOGE(TAG, "No clients connected to API server");
41 return;
42 }
43
44#ifdef USE_API_WIZARD_LINKED_INPUTS
45 if (this->entity_id_[0] == '\0') {
46 ESP_LOGW(TAG, "'%s': No entity ID set yet", LOG_STR_ARG(this->get_log_name()));
47 return;
48 }
49#endif
50
51 // In flash on the ESP8266 and encoded from there
52 static const char SERVICE_ON[] PROGMEM = "homeassistant.turn_on";
53 static const char SERVICE_OFF[] PROGMEM = "homeassistant.turn_off";
54 static const char ENTITY_ID_KEY[] PROGMEM = "entity_id";
55
57 resp.service = state ? ProgmemStringRef::from_lit(SERVICE_ON) : ProgmemStringRef::from_lit(SERVICE_OFF);
58
59 resp.data.init(1);
60 auto &entity_id_kv = resp.data.emplace_back();
61 entity_id_kv.key = ProgmemStringRef::from_lit(ENTITY_ID_KEY);
62 entity_id_kv.value = ProgmemStringRef(StringRef(this->entity_id_));
63
65}
66
67} // namespace esphome::homeassistant
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:230
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:236
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
void send_homeassistant_action(const HomeassistantActionRequest &call)
void subscribe_home_assistant_state(const char *entity_id, const char *attribute, std::function< void(StringRef)> &&f)
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1423
bool state
The current reported state of the binary sensor.
Definition switch.h:60
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:70
bool state
Definition fan.h:2
mopeka_std_values val[3]
APIServer * global_api_server
ESPHOME_LOG_TAG(TAG, "homeassistant.binary_sensor")
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:57
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:554
@ PARSE_ON
Definition helpers.h:1741
@ PARSE_TOGGLE
Definition helpers.h:1743
@ PARSE_OFF
Definition helpers.h:1742
@ PARSE_NONE
Definition helpers.h:1740