ESPHome 2026.1.4
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 {
6namespace hte501 {
7
8static const char *const TAG = "hte501";
9
10static constexpr size_t HTE501_SERIAL_NUMBER_SIZE = 7;
11
13 uint8_t address[] = {0x70, 0x29};
14 uint8_t identification[9];
15 this->write_read(address, sizeof address, identification, sizeof identification);
16 if (identification[8] != crc8(identification, 8, 0xFF, 0x31, true)) {
17 this->error_code_ = CRC_CHECK_FAILED;
18 this->mark_failed();
19 return;
20 }
21#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
22 char serial_hex[format_hex_size(HTE501_SERIAL_NUMBER_SIZE)];
23#endif
24 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex_to(serial_hex, identification, HTE501_SERIAL_NUMBER_SIZE));
25}
26
28 ESP_LOGCONFIG(TAG, "HTE501:");
29 LOG_I2C_DEVICE(this);
30 switch (this->error_code_) {
32 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
33 break;
35 ESP_LOGE(TAG, "The crc check failed");
36 break;
37 case NONE:
38 default:
39 break;
40 }
41 LOG_UPDATE_INTERVAL(this);
42 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
43 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
44}
45
48 uint8_t address_1[] = {0x2C, 0x1B};
49 this->write(address_1, 2);
50 this->set_timeout(50, [this]() {
51 uint8_t i2c_response[6];
52 this->read(i2c_response, 6);
53 if (i2c_response[2] != crc8(i2c_response, 2, 0xFF, 0x31, true) &&
54 i2c_response[5] != crc8(i2c_response + 3, 2, 0xFF, 0x31, true)) {
55 this->error_code_ = CRC_CHECK_FAILED;
56 this->status_set_warning();
57 return;
58 }
59 float temperature = (float) encode_uint16(i2c_response[0], i2c_response[1]);
60 if (temperature > 55536) {
61 temperature = (temperature - 65536) / 100;
62 } else {
64 }
65 float humidity = ((float) encode_uint16(i2c_response[3], i2c_response[4])) / 100.0f;
66
67 ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
68 if (this->temperature_sensor_ != nullptr)
69 this->temperature_sensor_->publish_state(temperature);
70 if (this->humidity_sensor_ != nullptr)
71 this->humidity_sensor_->publish_state(humidity);
73 });
74}
75} // namespace hte501
76} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
void status_clear_warning()
sensor::Sensor * temperature_sensor_
Definition hte501.h:22
float get_setup_priority() const override
Definition hte501.cpp:46
sensor::Sensor * humidity_sensor_
Definition hte501.h:23
enum esphome::hte501::HTE501Component::ErrorCode NONE
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:194
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:76
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:804
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:45
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:463
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:322
uint16_t temperature
Definition sun_gtil2.cpp:12