diff --git a/esphome/components/esp32_ble_server/__init__.py b/esphome/components/esp32_ble_server/__init__.py index 0b6a2a9069..2d3821ccbd 100644 --- a/esphome/components/esp32_ble_server/__init__.py +++ b/esphome/components/esp32_ble_server/__init__.py @@ -199,8 +199,11 @@ async def to_code(config): @automation.register_action("ble_server.characteristic_set_value", BLECharacteristicSetValueAction, cv.Schema({ cv.Required(CONF_ID): cv.use_id(BLECharacteristic), - cv.Required(CONF_VALUE): CHARACTERISTIC_VALUE_SCHEMA, + cv.Required(CONF_VALUE): cv.templatable(cv.string), })) -async def ble_enable_to_code(config, action_id, template_arg, args): - char_var = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, char_var) +async def ble_server_characteristic_set_value(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, paren) + template_ = await cg.templatable(config[CONF_VALUE], args, cg.std_string) + cg.add(var.set_value(template_)) + return var diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index e4c6cb7cc8..66e146bfee 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -32,11 +32,12 @@ class BLEServerAutomationInterface { template class BLECharacteristicSetValueAction : public Action { public: - BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : characteristic_(characteristic) {} - void play(Ts... x) override { this->characteristic_->set_value(x...); } + BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {} + TEMPLATABLE_VALUE(std::string, value) + void play(Ts... x) override {this->parent_->set_value(this->value_.value(x...)); } protected: - BLECharacteristic *characteristic_; + BLECharacteristic *parent_; }; }; diff --git a/tests/components/esp32_ble_server/common.yaml b/tests/components/esp32_ble_server/common.yaml index 7524227d00..b71c607c0a 100644 --- a/tests/components/esp32_ble_server/common.yaml +++ b/tests/components/esp32_ble_server/common.yaml @@ -15,8 +15,12 @@ esp32_ble_server: - uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc42d advertise: false characteristics: - - id: test_write_characteristic - uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc12d + - id: test_change_characteristic + uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc11d + properties: + - read + value: "Initial" + - uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc12d properties: - read - write @@ -25,5 +29,5 @@ esp32_ble_server: - lambda: |- ESP_LOGD("BLE", "Received: %s", x.c_str()); - ble_server.characteristic_set_value: - id: test_write_characteristic - value: x + id: test_change_characteristic + value: !lambda 'return "Echo " + x + "";' \ No newline at end of file