[ld2410] Use register_apply_action for bluetooth_password.set (#19667)

This commit is contained in:
J. Nick Koston
2026-09-25 19:20:44 +00:00
committed by GitHub
parent a1b0e85a9e
commit 456e13adee
5 changed files with 20 additions and 43 deletions
+2 -22
View File
@@ -4,8 +4,6 @@ import esphome.codegen as cg
from esphome.components import uart
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_PASSWORD, CONF_THROTTLE, CONF_TIMEOUT
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
AUTO_LOAD = ["ld24xx"]
@@ -86,11 +84,6 @@ CALIBRATION_ACTION_SCHEMA = maybe_simple_id(
# Actions
BluetoothPasswordSetAction = ld2410_ns.class_(
"BluetoothPasswordSetAction", automation.Action
)
BLUETOOTH_PASSWORD_SET_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(LD2410Component),
@@ -98,21 +91,8 @@ BLUETOOTH_PASSWORD_SET_SCHEMA = cv.Schema(
}
)
@automation.register_action(
automation.register_apply_action(
"bluetooth_password.set",
BluetoothPasswordSetAction,
BLUETOOTH_PASSWORD_SET_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_PASSWORD, "set_bluetooth_password", cg.std_string),
)
async def bluetooth_password_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_PASSWORD], args, cg.std_string)
cg.add(var.set_password(template_))
return var
-20
View File
@@ -1,20 +0,0 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "ld2410.h"
namespace esphome::ld2410 {
template<typename... Ts> class BluetoothPasswordSetAction final : public Action<Ts...> {
public:
explicit BluetoothPasswordSetAction(LD2410Component *ld2410_comp) : ld2410_comp_(ld2410_comp) {}
TEMPLATABLE_VALUE(std::string, password)
void play(const Ts &...x) override { this->ld2410_comp_->set_bluetooth_password(this->password_.value(x...)); }
protected:
LD2410Component *ld2410_comp_;
};
} // namespace esphome::ld2410
-1
View File
@@ -24,7 +24,6 @@
#endif
#include "esphome/components/ld24xx/ld24xx.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/automation.h"
#include "esphome/core/helpers.h"
#include <array>
@@ -0,0 +1,10 @@
button:
- platform: template
name: LD2410 Actions
on_press:
- bluetooth_password.set:
id: my_ld2410
password: HiLink
- bluetooth_password.set:
id: my_ld2410
password: !lambda return "HiLink";
@@ -0,0 +1,8 @@
substitutions:
tx_pin: GPIO0
rx_pin: GPIO2
packages:
uart: !include ../../test_build_components/common/uart/esp8266-ard.yaml
ld2410: !include common.yaml
actions: !include common-actions.yaml