ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
homeassistant.h
Go to the documentation of this file.
1#pragma once
2
3#include <cctype>
4
10
11#ifdef USE_API_HOMEASSISTANT_SERVICES
13#endif
14
15namespace esphome::homeassistant {
16
19bool is_unset(StringRef value);
20
22inline bool is_input_helper(const char *entity_id, const char *input_service) {
23 return ESPHOME_strncmp_P(entity_id, input_service, sizeof("input_") - 1) == 0;
24}
25
26#ifdef USE_API_HOMEASSISTANT_SERVICES
28bool can_send_action(const char *tag, const char *entity_id, EntityBase *entity);
29
32 static constexpr char ENTITY_ID_KEY[] PROGMEM = "entity_id";
33 auto &entry = resp.data.emplace_back();
34 entry.key = ProgmemStringRef::from_lit(ENTITY_ID_KEY);
35 entry.value = ProgmemStringRef(StringRef(entity_id));
36}
37#endif
38
40template<typename C> bool emit_utf8(uint32_t code_point, C &on_char) {
41 if (code_point == 0 || code_point > 0x10FFFF)
42 return false;
43 if (code_point < 0x80) {
44 on_char(static_cast<char>(code_point));
45 } else if (code_point < 0x800) {
46 on_char(static_cast<char>(0xC0 | (code_point >> 6)));
47 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
48 } else if (code_point < 0x10000) {
49 on_char(static_cast<char>(0xE0 | (code_point >> 12)));
50 on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
51 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
52 } else {
53 on_char(static_cast<char>(0xF0 | (code_point >> 18)));
54 on_char(static_cast<char>(0x80 | ((code_point >> 12) & 0x3F)));
55 on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
56 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
57 }
58 return true;
59}
60
68template<typename C, typename O> bool parse_python_list(StringRef src, C &&on_char, O &&on_item) {
69 const char *pos = src.c_str();
70 const char *const end = pos + src.size();
71 auto skip_spaces = [&]() {
72 while (pos < end && *pos == ' ')
73 pos++;
74 };
75
76 skip_spaces();
77 if (pos == end || *pos != '[')
78 return false;
79 pos++;
80 skip_spaces();
81 if (pos < end && *pos == ']') {
82 pos++;
83 } else {
84 while (true) {
85 skip_spaces();
86 // An enum member is shown as <Name.MEMBER: 'value'>; the quoted value is the item
87 const bool enum_member = pos < end && *pos == '<';
88 if (enum_member) {
89 pos++;
90 while (pos < end && (std::isalnum(static_cast<unsigned char>(*pos)) || *pos == '_' || *pos == '.'))
91 pos++;
92 if (pos == end || *pos != ':')
93 return false;
94 pos++;
95 skip_spaces();
96 }
97 if (pos == end || (*pos != '\'' && *pos != '"'))
98 return false;
99 const char quote = *pos++;
100 while (true) {
101 if (pos == end)
102 return false;
103 const char c = *pos++;
104 if (c == quote)
105 break;
106 if (c != '\\') {
107 on_char(c);
108 continue;
109 }
110 if (pos == end)
111 return false;
112 const char escape = *pos++;
113 switch (escape) {
114 case '\\':
115 case '\'':
116 case '"':
117 on_char(escape);
118 break;
119 case 'n':
120 on_char('\n');
121 break;
122 case 'r':
123 on_char('\r');
124 break;
125 case 't':
126 on_char('\t');
127 break;
128 case 'x':
129 case 'u':
130 case 'U': {
131 const size_t digits = escape == 'x' ? 2 : (escape == 'u' ? 4 : 8);
132 if (static_cast<size_t>(end - pos) < digits)
133 return false;
134 auto code_point = parse_hex<uint32_t>(pos, digits);
135 if (!code_point.has_value() || !emit_utf8(*code_point, on_char))
136 return false;
137 pos += digits;
138 break;
139 }
140 default:
141 return false;
142 }
143 }
144 if (enum_member) {
145 skip_spaces();
146 if (pos == end || *pos != '>')
147 return false;
148 pos++;
149 }
150 on_item();
151 skip_spaces();
152 if (pos == end)
153 return false;
154 const char separator = *pos++;
155 if (separator == ']')
156 break;
157 if (separator != ',')
158 return false;
159 }
160 }
161 skip_spaces();
162 return pos == end;
163}
164
165} // namespace esphome::homeassistant
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
Definition string_ref.h:236
static constexpr ProgmemStringRef from_lit(const char(&s)[N])
A PROGMEM char array, with its length known at compile time.
Definition string_ref.h:242
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1514
bool parse_python_list(StringRef src, C &&on_char, O &&on_item)
Walk a list attribute as Home Assistant sends it: the Python repr of a list of strings,...
bool is_unset(StringRef value)
True for what Home Assistant reports when there is no value: "None" for an attribute that is not set,...
void add_entity_id(api::HomeassistantActionRequest &resp, const char *entity_id)
Adds the entity_id entry to an action's data.
static void const char * entity_id
bool can_send_action(const char *tag, const char *entity_id, EntityBase *entity)
Logs and returns false when no API client is connected or no entity is linked yet.
bool is_input_helper(const char *entity_id, const char *input_service)
True if entity_id is an input helper; input_service must start with "input_".
bool emit_utf8(uint32_t code_point, C &on_char)
Emit the UTF-8 encoding of a code point. Rejects NUL, which would end the item early.
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
uint32_t value
Definition hal.cpp:59
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:401
size_t size_t pos
Definition helpers.h:1530
const void * src
Definition hal.h:68
void const char * tag
Definition log.h:110
uint8_t end[39]
Definition sun_gtil2.cpp:17