ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
homeassistant.h
Go to the documentation of this file.
1#pragma once
2
3#include <cctype>
4
7
9
12bool is_unset(StringRef value);
13
15template<typename C> bool emit_utf8(uint32_t code_point, C &on_char) {
16 if (code_point == 0 || code_point > 0x10FFFF)
17 return false;
18 if (code_point < 0x80) {
19 on_char(static_cast<char>(code_point));
20 } else if (code_point < 0x800) {
21 on_char(static_cast<char>(0xC0 | (code_point >> 6)));
22 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
23 } else if (code_point < 0x10000) {
24 on_char(static_cast<char>(0xE0 | (code_point >> 12)));
25 on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
26 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
27 } else {
28 on_char(static_cast<char>(0xF0 | (code_point >> 18)));
29 on_char(static_cast<char>(0x80 | ((code_point >> 12) & 0x3F)));
30 on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
31 on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
32 }
33 return true;
34}
35
43template<typename C, typename O> bool parse_python_list(StringRef src, C &&on_char, O &&on_item) {
44 const char *pos = src.c_str();
45 const char *const end = pos + src.size();
46 auto skip_spaces = [&]() {
47 while (pos < end && *pos == ' ')
48 pos++;
49 };
50
51 skip_spaces();
52 if (pos == end || *pos != '[')
53 return false;
54 pos++;
55 skip_spaces();
56 if (pos < end && *pos == ']') {
57 pos++;
58 } else {
59 while (true) {
60 skip_spaces();
61 // An enum member is shown as <Name.MEMBER: 'value'>; the quoted value is the item
62 const bool enum_member = pos < end && *pos == '<';
63 if (enum_member) {
64 pos++;
65 while (pos < end && (std::isalnum(static_cast<unsigned char>(*pos)) || *pos == '_' || *pos == '.'))
66 pos++;
67 if (pos == end || *pos != ':')
68 return false;
69 pos++;
70 skip_spaces();
71 }
72 if (pos == end || (*pos != '\'' && *pos != '"'))
73 return false;
74 const char quote = *pos++;
75 while (true) {
76 if (pos == end)
77 return false;
78 const char c = *pos++;
79 if (c == quote)
80 break;
81 if (c != '\\') {
82 on_char(c);
83 continue;
84 }
85 if (pos == end)
86 return false;
87 const char escape = *pos++;
88 switch (escape) {
89 case '\\':
90 case '\'':
91 case '"':
92 on_char(escape);
93 break;
94 case 'n':
95 on_char('\n');
96 break;
97 case 'r':
98 on_char('\r');
99 break;
100 case 't':
101 on_char('\t');
102 break;
103 case 'x':
104 case 'u':
105 case 'U': {
106 const size_t digits = escape == 'x' ? 2 : (escape == 'u' ? 4 : 8);
107 if (static_cast<size_t>(end - pos) < digits)
108 return false;
109 auto code_point = parse_hex<uint32_t>(pos, digits);
110 if (!code_point.has_value() || !emit_utf8(*code_point, on_char))
111 return false;
112 pos += digits;
113 break;
114 }
115 default:
116 return false;
117 }
118 }
119 if (enum_member) {
120 skip_spaces();
121 if (pos == end || *pos != '>')
122 return false;
123 pos++;
124 }
125 on_item();
126 skip_spaces();
127 if (pos == end)
128 return false;
129 const char separator = *pos++;
130 if (separator == ']')
131 break;
132 if (separator != ',')
133 return false;
134 }
135 }
136 skip_spaces();
137 return pos == end;
138}
139
140} // namespace esphome::homeassistant
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
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,...
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.
uint32_t value
Definition hal.cpp:41
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:370
size_t size_t pos
Definition helpers.h:1333
const void * src
Definition hal.h:68
uint8_t end[39]
Definition sun_gtil2.cpp:17