From 8d36167e114eb3bca37d88388eb8dde1b426bde4 Mon Sep 17 00:00:00 2001 From: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> Date: Tue, 23 Jun 2026 15:21:26 -0400 Subject: [PATCH] [esp32_ble_server] Fix set_value action with by-reference triggers (#17156) --- .../esp32_ble_server/ble_server_automations.h | 6 ++-- tests/components/esp32_ble_server/common.yaml | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/esphome/components/esp32_ble_server/ble_server_automations.h b/esphome/components/esp32_ble_server/ble_server_automations.h index b4e9ed004e..b1f887e2fa 100644 --- a/esphome/components/esp32_ble_server/ble_server_automations.h +++ b/esphome/components/esp32_ble_server/ble_server_automations.h @@ -77,13 +77,15 @@ template class BLECharacteristicSetValueAction : public Actionparent_->set_value(this->buffer_.value(x...)); // Set the listener for read events - this->parent_->on_read([this, x...](uint16_t id) { + // ``mutable`` keeps by-copy captures non-const for triggers passing args by reference + // (e.g. climate on_control's ClimateCall&). See #17142. + this->parent_->on_read([this, x...](uint16_t id) mutable { // Set the value of the characteristic every time it is read this->parent_->set_value(this->buffer_.value(x...)); }); // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic BLECharacteristicSetValueActionManager::get_instance()->set_listener( - this->parent_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); }); + this->parent_, [this, x...]() mutable { this->parent_->set_value(this->buffer_.value(x...)); }); } protected: diff --git a/tests/components/esp32_ble_server/common.yaml b/tests/components/esp32_ble_server/common.yaml index 4e34049038..c617a73f87 100644 --- a/tests/components/esp32_ble_server/common.yaml +++ b/tests/components/esp32_ble_server/common.yaml @@ -77,3 +77,34 @@ esp32_ble_server: id: test_change_descriptor value: data: [0x01, 0x02, 0x03] + +# Regression test for #17142: the set_value action used from a trigger that passes +# its argument by reference (climate on_control supplies ClimateCall&) previously +# failed to compile. +sensor: + - platform: template + id: ble_test_temp + lambda: "return 20.0;" + +output: + - platform: template + id: ble_test_output + type: float + write_action: + - logger.log: "out" + +climate: + - platform: pid + name: "BLE Test Climate" + id: ble_test_climate + sensor: ble_test_temp + default_target_temperature: 20 + heat_output: ble_test_output + control_parameters: + kp: 0.1 + ki: 0.001 + kd: 0.1 + on_control: + - ble_server.characteristic.set_value: + id: test_notify_characteristic + value: !lambda "return std::vector{0, 1, 2};"