10static const uint8_t KMETER_ERROR_STATUS_REG = 0x20;
11static const uint8_t KMETER_TEMP_VAL_REG = 0x00;
12static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10;
13static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE;
16 this->error_code_ =
NONE;
26 ESP_LOGCONFIG(TAG,
"Could write to the address %d.", this->
address_);
28 ESP_LOGCONFIG(TAG,
"Could not write to the address %d.", this->
address_);
34 uint8_t read_buf[4] = {1};
35 if (!this->
read_bytes(KMETER_ERROR_STATUS_REG, read_buf, 1)) {
36 ESP_LOGCONFIG(TAG,
"Could not read from the device.");
41 if (read_buf[0] != 0) {
42 ESP_LOGCONFIG(TAG,
"The device is not ready.");
49void KMeterISOComponent::update() {
52 if (this->temperature_sensor_ !=
nullptr) {
53 if (!this->
read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4)) {
54 ESP_LOGW(TAG,
"Error reading temperature.");
56 int32_t temp =
encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
57 float temp_f = temp / 100.0;
58 ESP_LOGV(TAG,
"Got temperature=%.2f °C", temp_f);
59 this->temperature_sensor_->publish_state(temp_f);
63 if (this->internal_temperature_sensor_ !=
nullptr) {
64 if (!this->
read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4)) {
65 ESP_LOGW(TAG,
"Error reading internal temperature.");
68 int32_t internal_temp =
encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
69 float internal_temp_f = internal_temp / 100.0;
70 ESP_LOGV(TAG,
"Got internal temperature=%.2f °C", internal_temp_f);
71 this->internal_temperature_sensor_->publish_state(internal_temp_f);
virtual ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer, size_t read_count)=0
This virtual method writes bytes to an I2CBus from an array, then reads bytes into an array of ReadBu...
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.