ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
text_sensor.cpp
Go to the documentation of this file.
1#include "text_sensor.h"
2
3#include <algorithm>
4#include <cctype>
5#include <cstdio>
6#include <cstring>
7
9#include "esphome/core/log.h"
11
12namespace esphome::duco {
13
14#ifdef USE_DUCO_ERROR_TEXT_SENSOR
15// Only built when an error text sensor is configured; the tables are about 3 kB of flash
16ESPHOME_LOG_TAG(TAG, "duco.text_sensor");
17static constexpr size_t MAX_ERROR_TEXT_LENGTH = 255;
18
19PROGMEM_STRING_TABLE(ErrorCodes, "E01.04.01", "E03.01.01", "E03.01.02", "E03.01.03", "E03.01.04", "E03.01.05",
20 "E03.01.06", "E04.01.01", "E04.01.02", "E04.01.03", "E04.01.04", "E04.01.05", "E04.01.06",
21 "E06.01.01", "E06.01.02", "E06.01.03", "E06.01.04", "E06.01.05", "E06.01.06", "E16.01.01",
22 "E16.01.02", "E16.01.03", "E16.01.04", "E16.01.05", "E16.01.06", "E28.04.01", "E29.01.01",
23 "E29.01.02", "E29.01.03", "E29.01.04", "E29.01.05", "E29.01.06", "E30.01.01", "E30.01.02",
24 "E30.01.03", "E30.01.04", "E30.01.05", "E30.01.06", "E31.19.01", "E32.10.01", "E33.10.01",
25 "E35.17.01", "E35.17.02", "E35.17.03", "E36.24.01", "E36.24.02", "E37.17.01", "E37.17.02",
26 "E37.17.03", "E40.17.01", "E40.17.02", "E40.17.03", "E42.22.01", "E42.22.02", "E43.23.01",
27 "E43.23.02", "E44.06.01", "E44.06.02");
29 ErrorDescriptions, "No response from the extractor fan.", "ODA temperature sensor value is too low.",
30 "ODA temperature sensor value is too high.", "ODA temperature sensor cannot be read.",
31 "ODA temperature sensor value is unchanged for too long.", "ODA temperature sensor is short-circuited.",
32 "ODA temperature sensor has a bad connection.", "ETA temperature sensor value is too low.",
33 "ETA temperature sensor value is too high.", "ETA temperature sensor cannot be read.",
34 "ETA temperature sensor value is unchanged for too long.", "ETA temperature sensor is short-circuited.",
35 "ETA temperature sensor has a bad connection.", "EHA temperature sensor value is too low.",
36 "EHA temperature sensor value is too high.", "EHA temperature sensor cannot be read.",
37 "EHA temperature sensor value is unchanged for too long.", "EHA temperature sensor is short-circuited.",
38 "EHA temperature sensor has a bad connection.", "Extract air pressure sensor value is too low.",
39 "Extract air pressure sensor value is too high.", "Extract air pressure sensor cannot be read.",
40 "Extract air pressure sensor value is unchanged for too long.", "Extract air pressure sensor is short-circuited.",
41 "Extract air pressure sensor has a bad connection.", "No response from the supply fan.",
42 "Supply air pressure sensor value is too low.", "Supply air pressure sensor value is too high.",
43 "Supply air pressure sensor cannot be read.", "Supply air pressure sensor value is unchanged for too long.",
44 "Supply air pressure sensor is short-circuited.", "Supply air pressure sensor has a bad connection.",
45 "SUP temperature sensor value is too low.", "SUP temperature sensor value is too high.",
46 "SUP temperature sensor cannot be read.", "SUP temperature sensor value is unchanged for too long.",
47 "SUP temperature sensor is short-circuited.", "SUP temperature sensor has a bad connection.",
48 "Filters need replacement.", "Outdoor temperature is too low; ventilation is off for 8 hours.",
49 "Outdoor temperature is too low; ventilation is permanently off.", "Temperature sensor value is too low.",
50 "Temperature sensor value is too high.", "Temperature sensor cannot be read.",
51 "Connection to the weather station has been lost.", "Connection to the outdoor temperature sensor has been lost.",
52 "CO2 sensor value is too low.", "CO2 sensor value is too high.", "CO2 sensor cannot be read.",
53 "Humidity sensor value is too low.", "Humidity sensor value is too high.", "Humidity sensor cannot be read.",
54 "Too many valves are connected.", "Too few valves are connected; at least two are required.",
55 "Flow adjustment has not happened or failed.", "The requested flow rate cannot be achieved.",
56 "No connection to the Duco Pre-Heater.", "A Duco Pre-Heater is connected but not configured.",
57 "Unknown DUCO error");
58static_assert(ErrorCodes::COUNT + 1 == ErrorDescriptions::COUNT);
59
60// One line per error, with its description or only its code. Returns the text length, or 0 if it does not fit
61// in MAX_ERROR_TEXT_LENGTH; text holds one byte more than that so a cut off text is told apart from a full one.
62static size_t format_errors(const uint8_t *records, uint8_t count, char *text, size_t size, bool descriptions) {
63 size_t pos = 0;
64 for (uint8_t index = 0; index < count; index++) {
65 const uint8_t *record = records + index * 5;
66 const size_t code_start = pos;
67 pos = buf_append_printf(text, size, pos, "E%02u.%02u.%02u", record[1], record[2], record[3]);
68 if (descriptions && pos < size) {
69 const int16_t description_index = progmem_string_table_find<ErrorCodes>(text + code_start, pos - code_start);
70 pos = buf_append_str(text, size, pos, " ");
72 reinterpret_cast<ESPHOME_PGM_P>(ErrorDescriptions::get_progmem_str(description_index)));
73 }
74 pos = buf_append_str(text, size, pos, "\n");
75 }
76 return pos <= MAX_ERROR_TEXT_LENGTH ? pos : 0;
77}
78#endif
79
81 switch (this->reading_) {
83 // Address 0 selects the separate box serial; nodes use a different data block (main board is node 1)
84 if (this->address_ == 0) {
85 this->parent_->send(this, FUNCTION_NODE_DATA, {0x01, 0x01, 0x00, 0x1a, 0x10});
86 } else {
87 this->parent_->send(this, FUNCTION_NODE_DATA, {0x01, this->address_, 0x00, 0x08, 0x06});
88 }
89 break;
91 this->parent_->send(this, FUNCTION_NODE_DATA, {0x01, this->address_, 0x00, 0x00, 0x04});
92 break;
94#ifdef USE_DUCO_ERROR_TEXT_SENSOR
95 this->parent_->send(this, FUNCTION_ERRORS, {this->address_, 0x00, 0x00, 0x04});
96#endif
97 break;
99 this->parent_->send(this, FUNCTION_NODE_DATA, {0x01, this->address_, 0x00, 0x49, 0x04});
100 break;
102 this->parent_->send(this, FUNCTION_NODE, {0x01, this->address_});
103 break;
104 }
105}
106
108 switch (this->reading_) {
110 this->publish_serial_number_(message);
111 break;
113 this->publish_version_(message);
114 break;
116#ifdef USE_DUCO_ERROR_TEXT_SENSOR
117 this->publish_errors_(message);
118#endif
119 break;
121 this->publish_uptime_(message);
122 break;
124 if (message.size >= 1)
126 break;
127 }
128}
129
131 if (message.size <= 2)
132 return;
133 // The serial number starts at byte 2 and is padded with zeros
134 const char *serial = reinterpret_cast<const char *>(message.data + 2);
135 const auto serial_length = strnlen(serial, message.size - 2);
136 if (serial_length == 0 || !std::all_of(serial, serial + serial_length, [](char character) {
137 return std::isprint(static_cast<unsigned char>(character));
138 })) {
139 return;
140 }
141 this->publish_state(serial, serial_length);
142}
143
145 if (message.size < 7)
146 return;
147 const unsigned major = encode_uint16(message.data[3], message.data[2]);
148 const unsigned minor = message.data[4], patch = message.data[6], build = message.data[5];
149 char version[20];
150 snprintf(version, sizeof(version), "%u.%u.%u.%u", major, minor, patch, build);
151 this->publish_state(version);
152}
153
154#ifdef USE_DUCO_ERROR_TEXT_SENSOR
156 if (message.size == 0 || message.data[0] == 0) {
157 this->publish_state("No errors");
158 return;
159 }
160 const uint8_t error_count = message.data[0];
161 if (message.size < 1 + static_cast<size_t>(error_count) * 5) {
162 ESP_LOGW(TAG, "Error answer for %u errors has only %u bytes", error_count, message.size);
163 return;
164 }
165
166 char text[MAX_ERROR_TEXT_LENGTH + 2];
167 size_t length = format_errors(message.data + 1, error_count, text, sizeof(text), true);
168 if (length == 0) // the descriptions do not fit, the codes always do
169 length = format_errors(message.data + 1, error_count, text, sizeof(text), false);
170 this->publish_state(text, length);
171}
172#endif
173
175 if (message.size < 10)
176 return;
177 const unsigned uptime_minutes = encode_uint16(message.data[9], message.data[8]);
178 const unsigned days = uptime_minutes / 1440;
179 const unsigned hours = uptime_minutes % 1440 / 60;
180 const unsigned minutes = uptime_minutes % 60;
181 char formatted[24];
182 snprintf(formatted, sizeof(formatted), "%ud %uh %um", days, hours, minutes);
183 this->publish_state(formatted);
184}
185
186} // namespace esphome::duco
void publish_errors_(const DucoMessage &message)
void publish_version_(const DucoMessage &message)
void receive_response(const DucoMessage &message) override
void publish_uptime_(const DucoMessage &message)
void publish_serial_number_(const DucoMessage &message)
void publish_state(const std::string &state)
Definition text_sensor.h:42
const LogString * message
Definition component.cpp:35
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "duco")
uint8_t node_type_byte(const DucoMessage &message)
The node type byte of a non-empty node answer; a one-byte answer has no network type.
Definition duco.h:37
PROGMEM_STRING_TABLE(ErrorCodes, "E01.04.01", "E03.01.01", "E03.01.02", "E03.01.03", "E03.01.04", "E03.01.05", "E03.01.06", "E04.01.01", "E04.01.02", "E04.01.03", "E04.01.04", "E04.01.05", "E04.01.06", "E06.01.01", "E06.01.02", "E06.01.03", "E06.01.04", "E06.01.05", "E06.01.06", "E16.01.01", "E16.01.02", "E16.01.03", "E16.01.04", "E16.01.05", "E16.01.06", "E28.04.01", "E29.01.01", "E29.01.02", "E29.01.03", "E29.01.04", "E29.01.05", "E29.01.06", "E30.01.01", "E30.01.02", "E30.01.03", "E30.01.04", "E30.01.05", "E30.01.06", "E31.19.01", "E32.10.01", "E33.10.01", "E35.17.01", "E35.17.02", "E35.17.03", "E36.24.01", "E36.24.02", "E37.17.01", "E37.17.02", "E37.17.03", "E40.17.01", "E40.17.02", "E40.17.03", "E42.22.01", "E42.22.02", "E43.23.01", "E43.23.02", "E44.06.01", "E44.06.02")
const LogString * node_type_name(uint8_t type)
The name of a node type byte.
Definition duco.cpp:181
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str)
Safely append a PROGMEM string to buffer, returning new position (capped at size).
Definition helpers.cpp:32
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Definition helpers.h:1568
uint16_t size
Definition helpers.cpp:44
int16_t progmem_string_table_find(const char *value, size_t len)
The index of the string value (len bytes, in RAM) in a PROGMEM_STRING_TABLE, or -1 when it is not one...
Definition progmem.h:188
size_t size_t pos
Definition helpers.h:1493
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1199
const __FlashStringHelper * ProgmemStr
Definition progmem.h:33
A received message; data points into the receive buffer and is valid only during the callback.
Definition duco.h:30