ESPHome  2024.10.2
kmeteriso.cpp
Go to the documentation of this file.
1 #include "kmeteriso.h"
2 #include "esphome/core/hal.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace kmeteriso {
7 
8 static const char *const TAG = "kmeteriso.sensor";
9 
10 static const uint8_t KMETER_ERROR_STATUS_REG = 0x20;
11 static const uint8_t KMETER_TEMP_VAL_REG = 0x00;
12 static const uint8_t KMETER_INTERNAL_TEMP_VAL_REG = 0x10;
13 static const uint8_t KMETER_FIRMWARE_VERSION_REG = 0xFE;
14 
16  ESP_LOGCONFIG(TAG, "Setting up KMeterISO...");
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.
22  this->component_state_ &= ~COMPONENT_STATE_MASK;
24  }
25 
26  auto err = this->bus_->writev(this->address_, nullptr, 0);
27  if (err == esphome::i2c::ERROR_OK) {
28  ESP_LOGCONFIG(TAG, "Could write to the address %d.", this->address_);
29  } else {
30  ESP_LOGCONFIG(TAG, "Could not write to the address %d.", this->address_);
31  this->error_code_ = COMMUNICATION_FAILED;
32  this->mark_failed();
33  return;
34  }
35 
36  uint8_t read_buf[4] = {1};
37  if (!this->read_bytes(KMETER_ERROR_STATUS_REG, read_buf, 1)) {
38  ESP_LOGCONFIG(TAG, "Could not read from the device.");
39  this->error_code_ = COMMUNICATION_FAILED;
40  this->mark_failed();
41  return;
42  }
43  if (read_buf[0] != 0) {
44  ESP_LOGCONFIG(TAG, "The device is not ready.");
45  this->error_code_ = STATUS_FAILED;
46  this->mark_failed();
47  return;
48  }
49  ESP_LOGCONFIG(TAG, "The device was successfully setup.");
50 }
51 
53 
55  uint8_t read_buf[4];
56 
57  if (this->temperature_sensor_ != nullptr) {
58  if (!this->read_bytes(KMETER_TEMP_VAL_REG, read_buf, 4)) {
59  ESP_LOGW(TAG, "Error reading temperature.");
60  } else {
61  int32_t temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
62  float temp_f = temp / 100.0;
63  ESP_LOGV(TAG, "Got temperature=%.2f °C", temp_f);
64  this->temperature_sensor_->publish_state(temp_f);
65  }
66  }
67 
68  if (this->internal_temperature_sensor_ != nullptr) {
69  if (!this->read_bytes(KMETER_INTERNAL_TEMP_VAL_REG, read_buf, 4)) {
70  ESP_LOGW(TAG, "Error reading internal temperature.");
71  return;
72  } else {
73  int32_t internal_temp = encode_uint32(read_buf[3], read_buf[2], read_buf[1], read_buf[0]);
74  float internal_temp_f = internal_temp / 100.0;
75  ESP_LOGV(TAG, "Got internal temperature=%.2f °C", internal_temp_f);
76  this->internal_temperature_sensor_->publish_state(internal_temp_f);
77  }
78  }
79 }
80 
81 } // namespace kmeteriso
82 } // namespace esphome
const uint32_t COMPONENT_STATE_FAILED
Definition: component.cpp:36
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: kmeteriso.cpp:52
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:212
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:186
enum esphome::kmeteriso::KMeterISOComponent::ErrorCode NONE
uint32_t component_state_
State of this component.
Definition: component.h:272
virtual ErrorCode writev(uint8_t address, WriteBuffer *buffers, size_t cnt)
Definition: i2c_bus.h:80
No error found during execution of method.
Definition: i2c_bus.h:13
const uint32_t COMPONENT_STATE_CONSTRUCTION
Definition: component.cpp:33
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
const uint32_t COMPONENT_STATE_MASK
Definition: component.cpp:32
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
I2CBus * bus_
pointer to I2CBus instance
Definition: i2c.h:270
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * internal_temperature_sensor_
Definition: kmeteriso.h:25