ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hyt271.cpp
Go to the documentation of this file.
1#include "hyt271.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
5
6namespace esphome::hyt271 {
7
8ESPHOME_LOG_TAG(TAG, "hyt271");
9
10static constexpr uint32_t WAIT_CONVERT_TIMEOUT_ID = 0;
11
12static const uint8_t HYT271_ADDRESS = 0x28;
13
15 ESP_LOGCONFIG(TAG, "HYT271:");
16 LOG_I2C_DEVICE(this);
17 LOG_UPDATE_INTERVAL(this);
18 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
19 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
20}
21void HYT271Component::update() {
22 uint8_t raw_data[4] = {0, 0, 0, 0};
23
24 if (this->write(&raw_data[0], 0) != i2c::ERROR_OK) {
25 this->status_set_warning();
26 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
27 return;
28 }
29 this->set_timeout(WAIT_CONVERT_TIMEOUT_ID, 50, [this]() {
30 uint8_t raw_data[4];
31 if (this->read(raw_data, 4) != i2c::ERROR_OK) {
32 this->status_set_warning();
33 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
34 return;
35 }
36 uint16_t raw_temperature = encode_uint16(raw_data[2], raw_data[3]) >> 2;
37 uint16_t raw_humidity = encode_uint16(raw_data[0] & 0x3F, raw_data[1]);
38
39 float temperature = ((float(raw_temperature)) * (165.0f / 16383.0f)) - 40.0f;
40 float humidity = (float(raw_humidity)) * (100.0f / 16383.0f);
41
42 ESP_LOGD(TAG, "Got Temperature=%.1f°C Humidity=%.1f%%", temperature, humidity);
43
44 PUBLISH_STATE(this->temperature_sensor_, temperature);
45 PUBLISH_STATE(this->humidity_sensor_, humidity);
47 });
48}
49} // namespace esphome::hyt271
virtual void dump_config()
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(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, "hyt271")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
FLAG_HAS_TRANSITION float
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
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12