ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hte501.cpp
Go to the documentation of this file.
1#include "hte501.h"
3#include "esphome/core/log.h"
4
5namespace esphome::hte501 {
6
7ESPHOME_LOG_TAG(TAG, "hte501");
8
9static constexpr size_t HTE501_SERIAL_NUMBER_SIZE = 7;
10
12 uint8_t address[] = {0x70, 0x29};
13 uint8_t identification[9];
14 if (this->write_read(address, sizeof address, identification, sizeof identification) != i2c::ERROR_OK) {
15 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
16 return;
17 }
18 if (identification[8] != crc8(identification, 8, 0xFF, 0x31, true)) {
19 this->mark_failed(LOG_STR("CRC check failed"));
20 return;
21 }
22#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
23 char serial_hex[format_hex_size(HTE501_SERIAL_NUMBER_SIZE)];
24#endif
25 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex_to(serial_hex, identification, HTE501_SERIAL_NUMBER_SIZE));
26}
27
28void HTE501Component::dump_config() {
29 ESP_LOGCONFIG(TAG, "HTE501:");
30 LOG_I2C_DEVICE(this);
31 LOG_UPDATE_INTERVAL(this);
32 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
33 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
34}
35
36void HTE501Component::update() {
37 uint8_t address_1[] = {0x2C, 0x1B};
38 if (this->write(address_1, 2) != i2c::ERROR_OK) {
39 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
40 return;
41 }
42 this->set_timeout(50, [this]() {
43 uint8_t i2c_response[6];
44 if (this->read(i2c_response, 6) != i2c::ERROR_OK) {
45 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
46 return;
47 }
48 if (i2c_response[2] != crc8(i2c_response, 2, 0xFF, 0x31, true) ||
49 i2c_response[5] != crc8(i2c_response + 3, 2, 0xFF, 0x31, true)) {
50 this->status_set_warning(LOG_STR("CRC check failed"));
51 return;
52 }
53 float temperature = static_cast<int16_t>(encode_uint16(i2c_response[0], i2c_response[1])) / 100.0f;
54 float humidity = ((float) encode_uint16(i2c_response[3], i2c_response[4])) / 100.0f;
55
56 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
57 PUBLISH_STATE(this->temperature_sensor_, temperature);
58 PUBLISH_STATE(this->humidity_sensor_, humidity);
60 });
61}
62} // namespace esphome::hte501
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
writes an array of bytes to a device, then reads an array, as a single transaction
Definition i2c.h:205
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
ESPHOME_LOG_TAG(TAG, "hte501")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
FLAG_HAS_TRANSITION float
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:1661
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:68
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:1043
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:428
uint16_t temperature
Definition sun_gtil2.cpp:12