Add templatable characteristic values

This commit is contained in:
Rapsssito 2024-06-28 00:07:17 +02:00
parent ce0ecfe506
commit ce711a95f3
3 changed files with 19 additions and 11 deletions

View File

@ -199,8 +199,11 @@ async def to_code(config):
@automation.register_action("ble_server.characteristic_set_value", BLECharacteristicSetValueAction, cv.Schema({ @automation.register_action("ble_server.characteristic_set_value", BLECharacteristicSetValueAction, cv.Schema({
cv.Required(CONF_ID): cv.use_id(BLECharacteristic), 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): async def ble_server_characteristic_set_value(config, action_id, template_arg, args):
char_var = await cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, char_var) 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

View File

@ -32,11 +32,12 @@ class BLEServerAutomationInterface {
template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> { template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
public: public:
BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : characteristic_(characteristic) {} BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
void play(Ts... x) override { this->characteristic_->set_value(x...); } TEMPLATABLE_VALUE(std::string, value)
void play(Ts... x) override {this->parent_->set_value(this->value_.value(x...)); }
protected: protected:
BLECharacteristic *characteristic_; BLECharacteristic *parent_;
}; };
}; };

View File

@ -15,8 +15,12 @@ esp32_ble_server:
- uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc42d - uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc42d
advertise: false advertise: false
characteristics: characteristics:
- id: test_write_characteristic - id: test_change_characteristic
uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc12d uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc11d
properties:
- read
value: "Initial"
- uuid: 2a24b789-7a1b-4535-af3e-ee76a35cc12d
properties: properties:
- read - read
- write - write
@ -25,5 +29,5 @@ esp32_ble_server:
- lambda: |- - lambda: |-
ESP_LOGD("BLE", "Received: %s", x.c_str()); ESP_LOGD("BLE", "Received: %s", x.c_str());
- ble_server.characteristic_set_value: - ble_server.characteristic_set_value:
id: test_write_characteristic id: test_change_characteristic
value: x value: !lambda 'return "Echo " + x + "";'