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({
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

View File

@ -32,11 +32,12 @@ class BLEServerAutomationInterface {
template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
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_;
};
};

View File

@ -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 + "";'