ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
kmeteriso.cpp
Go to the documentation of this file.
1#include "kmeteriso.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace kmeteriso {
8
9static const char *const TAG = "kmeteriso.sensor";
10
11static const uint8_t KMETER_ERROR_STATUS_REG = 0x20;
12static const uint8_t KMETER_TEMP_VAL_REG = 0x00;
13static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10;
14static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE;
15
17 this->error_code_ = NONE;
18
19 // Mark as not failed before initializing. Some devices will turn off sensors to save on batteries
20 // and when they come back on, the COMPONENT_STATE_FAILED bit must be unset on the component.
21 if (this->is_failed()) {
23 }
24
25 auto err = this->bus_->write_readv(this->address_, nullptr, 0, nullptr, 0);
26 if (err == esphome::i2c::ERROR_OK) {
27 ESP_LOGCONFIG(TAG, "Could write to the address %d.", this->address_);
28 } else {
29 ESP_LOGCONFIG(TAG, "Could not write to the address %d.", this->address_);
30 this->error_code_ = COMMUNICATION_FAILED;
31 this->mark_failed();
32 return;
33 }
34
35 uint8_t read_buf[4] = {1};
36 if (!this->read_bytes(KMETER_ERROR_STATUS_REG, read_buf, 1)) {
37 ESP_LOGCONFIG(TAG, "Could not read from the device.");
38 this->error_code_ = COMMUNICATION_FAILED;
39 this->mark_failed();
40 return;
41 }
42 if (read_buf[0] != 0) {
43 ESP_LOGCONFIG(TAG, "The device is not ready.");
44 this->error_code_ = STATUS_FAILED;
45 this->mark_failed();
46 return;
47 }
48}
49
51 uint8_t read_buf[4];
52
53 if (this->temperature_sensor_ != nullptr) {
54 if (!this->read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4)) {
55 ESP_LOGW(TAG, "Error reading temperature.");
56 } else {
57 int32_t temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
58 float temp_f = temp / 100.0;
59 ESP_LOGV(TAG, "Got temperature=%.2f °C", temp_f);
61 }
62 }
63
64 if (this->internal_temperature_sensor_ != nullptr) {
65 if (!this->read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4)) {
66 ESP_LOGW(TAG, "Error reading internal temperature.");
67 return;
68 } else {
69 int32_t internal_temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
70 float internal_temp_f = internal_temp / 100.0;
71 ESP_LOGV(TAG, "Got internal temperature=%.2f °C", internal_temp_f);
72 this->internal_temperature_sensor_->publish_state(internal_temp_f);
73 }
74 }
75}
76
77} // namespace kmeteriso
78} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
void reset_to_construction_state()
Reset this component back to the construction state to allow setup to run again.
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...
I2CBus * bus_
pointer to I2CBus instance
Definition i2c.h:271
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
enum esphome::kmeteriso::KMeterISOComponent::ErrorCode NONE
sensor::Sensor * internal_temperature_sensor_
Definition kmeteriso.h:24
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:661