[dallas_temp] Use a numeric scheduler id and drop the cached address string (#20361)

This commit is contained in:
J. Nick Koston
2026-10-09 07:14:19 -10:00
committed by GitHub
parent 4c17ee7393
commit aba0c5d7ee
3 changed files with 9 additions and 21 deletions
@@ -5,6 +5,8 @@ namespace esphome::dallas_temp {
ESPHOME_LOG_TAG(TAG, "dallas.temp.sensor");
static constexpr uint32_t CONVERSION_TIMEOUT_ID = 0;
static const uint8_t DALLAS_MODEL_DS18S20 = 0x10;
static const uint8_t DALLAS_MODEL_DS18B20 = 0x28;
static const uint8_t DALLAS_COMMAND_START_CONVERSION = 0x44;
@@ -44,7 +46,7 @@ void DallasTemperatureSensor::update() {
this->send_command_(DALLAS_COMMAND_START_CONVERSION);
this->set_timeout(this->get_address_name().c_str(), this->millis_to_wait_for_conversion_(), [this] {
this->set_timeout(CONVERSION_TIMEOUT_ID, this->millis_to_wait_for_conversion_(), [this] {
if (!this->read_scratch_pad_() || !this->check_scratch_pad_()) {
this->publish_state(NAN);
return;
-13
View File
@@ -4,19 +4,6 @@ namespace esphome::one_wire {
ESPHOME_LOG_TAG(TAG, "one_wire");
const std::string &OneWireDevice::get_address_name() {
if (this->address_name_.empty()) {
char hex_buf[19]; // "0x" + 16 hex chars + null
this->address_name_ = format_hex_prefixed_to(hex_buf, this->address_);
}
return this->address_name_;
}
void OneWireDevice::set_address(uint64_t address) {
this->address_ = address;
this->address_name_.clear();
}
bool OneWireDevice::send_command_(uint8_t cmd) {
if (!this->bus_->select(this->address_))
return false;
+6 -7
View File
@@ -7,14 +7,17 @@
namespace esphome::one_wire {
#define LOG_ONE_WIRE_DEVICE(this) \
ESP_LOGCONFIG(TAG, " Address: %s (%s)", this->get_address_name().c_str(), \
LOG_STR_ARG(this->bus_->get_model_str(this->address_ & 0xff)));
do { \
char address_buf[format_hex_prefixed_size(sizeof(uint64_t))]; \
ESP_LOGCONFIG(TAG, " Address: %s (%s)", format_hex_prefixed_to(address_buf, (this)->address_), \
LOG_STR_ARG((this)->bus_->get_model_str((this)->address_ & 0xff))); \
} while (0)
class OneWireDevice {
public:
/// @brief store the address of the device
/// @param address of the device
void set_address(uint64_t address);
void set_address(uint64_t address) { this->address_ = address; }
void set_index(uint8_t index) { this->index_ = index; }
@@ -22,16 +25,12 @@ class OneWireDevice {
/// @param bus pointer to the OneWireBus object
void set_one_wire_bus(OneWireBus *bus) { this->bus_ = bus; }
/// Helper to create (and cache) the name for this sensor. For example "0xfe0000031f1eaf29".
const std::string &get_address_name();
protected:
static constexpr uint8_t INDEX_NOT_SET = 255;
uint64_t address_{0};
uint8_t index_{INDEX_NOT_SET};
OneWireBus *bus_{nullptr}; ///< pointer to OneWireBus instance
std::string address_name_;
/// @brief find an address if necessary
/// should be called from setup