ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
uponor_smatrix.cpp
Go to the documentation of this file.
1#include "uponor_smatrix.h"
4#include "esphome/core/log.h"
5
6#include <cinttypes>
7
8namespace esphome {
9namespace uponor_smatrix {
10
11static const char *const TAG = "uponor_smatrix";
12
13// Maximum bytes to log in verbose hex output
14static constexpr size_t UPONOR_MAX_LOG_BYTES = 36;
15
17#ifdef USE_TIME
18 if (this->time_id_ != nullptr) {
19 this->time_id_->add_on_time_sync_callback([this] { this->send_time(); });
20 }
21#endif
22}
23
25 ESP_LOGCONFIG(TAG, "Uponor Smatrix");
26#ifdef USE_TIME
27 if (this->time_id_ != nullptr) {
28 ESP_LOGCONFIG(TAG, " Time synchronization: YES");
29 ESP_LOGCONFIG(TAG, " Time master device address: 0x%08" PRIX32 "", this->time_device_address_);
30 }
31#endif
32
33 this->check_uart_settings(19200);
34
35 if (!this->unknown_devices_.empty()) {
36 ESP_LOGCONFIG(TAG, " Detected unknown device addresses:");
37 for (auto device_address : this->unknown_devices_) {
38 ESP_LOGCONFIG(TAG, " 0x%08" PRIX32 "", device_address);
39 }
40 }
41}
42
45
46 // Discard stale data
47 if (!this->rx_buffer_.empty() && (now - this->last_rx_ > 50)) {
48 ESP_LOGD(TAG, "Discarding %d bytes of unparsed data", this->rx_buffer_.size());
49 this->rx_buffer_.clear();
50 }
51
52 // Read incoming data
53 while (this->available()) {
54 // The controller polls devices every 10 seconds in some units or continuously in others with around 200 ms between
55 // devices. Remember timestamps so we can send our own packets when the bus is expected to be silent.
56 this->last_rx_ = now;
57
58 uint8_t byte;
59 this->read_byte(&byte);
60 if (this->parse_byte_(byte)) {
61 this->rx_buffer_.clear();
62 }
63 }
64
65 // Send packets during bus silence
66 if (this->rx_buffer_.empty() && (now - this->last_rx_ > 50) && (now - this->last_rx_ < 100) &&
67 (now - this->last_tx_ > 200)) {
68#ifdef USE_TIME
69 // Only build time packet when bus is silent and queue is empty to make sure we can send it right away
70 if (this->send_time_requested_ && this->tx_queue_.empty() && this->do_send_time_())
71 this->send_time_requested_ = false;
72#endif
73 // Send the next packet in the queue
74 if (!this->tx_queue_.empty()) {
75 auto packet = std::move(this->tx_queue_.front());
76 this->tx_queue_.pop();
77
78 this->write_array(packet);
79 this->flush();
80
81 this->last_tx_ = now;
82 }
83 }
84}
85
87 this->rx_buffer_.push_back(byte);
88 const uint8_t *packet = this->rx_buffer_.data();
89 size_t packet_len = this->rx_buffer_.size();
90
91 if (packet_len < 7) {
92 // Minimum packet size is 7 bytes, wait for more
93 return false;
94 }
95
96 uint32_t device_address = encode_uint32(packet[0], packet[1], packet[2], packet[3]);
97 uint16_t crc = encode_uint16(packet[packet_len - 1], packet[packet_len - 2]);
98
99 uint16_t computed_crc = crc16(packet, packet_len - 2);
100 if (crc != computed_crc) {
101 // CRC did not match, more data might be coming
102 return false;
103 }
104
105#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
106 char hex_buf[format_hex_size(UPONOR_MAX_LOG_BYTES)];
107#endif
108 ESP_LOGV(TAG, "Received packet: addr=%08" PRIX32 ", data=%s, crc=%04X", device_address,
109 format_hex_to(hex_buf, &packet[4], packet_len - 6), crc);
110
111 // Handle packet
112 size_t data_len = (packet_len - 6) / 3;
113 if (data_len == 0) {
114 if (packet[4] == UPONOR_ID_REQUEST)
115 ESP_LOGVV(TAG, "Ignoring request packet for device 0x%08" PRIX32 "", device_address);
116 return true;
117 }
118
119 // Decode packet payload data for easy access
120 UponorSmatrixData data[data_len];
121 for (size_t i = 0; i < data_len; i++) {
122 data[i].id = packet[(i * 3) + 4];
123 data[i].value = encode_uint16(packet[(i * 3) + 5], packet[(i * 3) + 6]);
124 }
125
126#ifdef USE_TIME
127 // Detect device that acts as time master if not set explicitely
128 if (this->time_device_address_ == 0 && data_len >= 2) {
129 // The first thermostat paired to the controller will act as the time master. Time can only be manually adjusted at
130 // this first thermostat. To synchronize time, we need to know its address, so we search for packets coming from a
131 // thermostat sending both room temperature and time information.
132 bool found_temperature = false;
133 bool found_time = false;
134 for (size_t i = 0; i < data_len; i++) {
135 if (data[i].id == UPONOR_ID_ROOM_TEMP)
136 found_temperature = true;
137 if (data[i].id == UPONOR_ID_DATETIME1)
138 found_time = true;
139 if (found_temperature && found_time) {
140 ESP_LOGI(TAG, "Using detected time device address 0x%08" PRIX32 "", device_address);
141 this->time_device_address_ = device_address;
142 break;
143 }
144 }
145 }
146#endif
147
148 // Forward data to device components
149 bool found = false;
150 for (auto *device : this->devices_) {
151 if (device->address_ == device_address) {
152 found = true;
153 device->on_device_data(data, data_len);
154 }
155 }
156
157 // Log unknown device addresses
158 if (!found && !this->unknown_devices_.count(device_address)) {
159 ESP_LOGI(TAG, "Received packet for unknown device address 0x%08" PRIX32 " ", device_address);
160 this->unknown_devices_.insert(device_address);
161 }
162
163 // Return true to reset buffer
164 return true;
165}
166
167bool UponorSmatrixComponent::send(uint32_t device_address, const UponorSmatrixData *data, size_t data_len) {
168 if (device_address == 0 || data == nullptr || data_len == 0)
169 return false;
170
171 // Assemble packet for send queue. All fields are big-endian except for the little-endian checksum.
172 std::vector<uint8_t> packet;
173 packet.reserve(6 + 3 * data_len);
174
175 packet.push_back(device_address >> 24);
176 packet.push_back(device_address >> 16);
177 packet.push_back(device_address >> 8);
178 packet.push_back(device_address >> 0);
179
180 for (size_t i = 0; i < data_len; i++) {
181 packet.push_back(data[i].id);
182 packet.push_back(data[i].value >> 8);
183 packet.push_back(data[i].value >> 0);
184 }
185
186 auto crc = crc16(packet.data(), packet.size());
187 packet.push_back(crc >> 0);
188 packet.push_back(crc >> 8);
189
190 this->tx_queue_.push(packet);
191 return true;
192}
193
194#ifdef USE_TIME
196 if (this->time_device_address_ == 0 || this->time_id_ == nullptr)
197 return false;
198
199 ESPTime now = this->time_id_->now();
200 if (!now.is_valid())
201 return false;
202
203 uint8_t year = now.year - 2000;
204 uint8_t month = now.month;
205 // ESPHome days are [1-7] starting with Sunday, Uponor days are [0-6] starting with Monday
206 uint8_t day_of_week = (now.day_of_week == 1) ? 6 : (now.day_of_week - 2);
207 uint8_t day_of_month = now.day_of_month;
208 uint8_t hour = now.hour;
209 uint8_t minute = now.minute;
210 uint8_t second = now.second;
211
212 uint16_t time1 = (year & 0x7F) << 7 | (month & 0x0F) << 3 | (day_of_week & 0x07);
213 uint16_t time2 = (day_of_month & 0x1F) << 11 | (hour & 0x1F) << 6 | (minute & 0x3F);
214 uint16_t time3 = second;
215
216 ESP_LOGI(TAG, "Sending local time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
217 now.minute, now.second);
218
219 UponorSmatrixData data[] = {{UPONOR_ID_DATETIME1, time1}, {UPONOR_ID_DATETIME2, time2}, {UPONOR_ID_DATETIME3, time3}};
220 return this->send(this->time_device_address_, data, sizeof(data) / sizeof(data[0]));
221}
222#endif
223
224} // namespace uponor_smatrix
225} // namespace esphome
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
UARTFlushResult flush()
Definition uart.h:48
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:16
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
std::vector< UponorSmatrixDevice * > devices_
bool send(uint32_t device_address, const UponorSmatrixData *data, size_t data_len)
std::queue< std::vector< uint8_t > > tx_queue_
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t second
uint8_t minute
uint8_t hour
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition helpers.cpp:86
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:1342
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:889
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:881
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:397
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36