[ld2450] Use Apply entities for the forwarding buttons, switches, numbers and selects

This commit is contained in:
J. Nick Koston
2026-10-16 10:48:30 -10:00
parent ed9b566091
commit dd59a827ff
18 changed files with 34 additions and 206 deletions
+9 -8
View File
@@ -13,22 +13,19 @@ from esphome.const import (
)
from esphome.types import ConfigType
from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns
FactoryResetButton = ld2450_ns.class_("FactoryResetButton", button.Button)
RestartButton = ld2450_ns.class_("RestartButton", button.Button)
from .. import CONF_LD2450_ID, LD2450Component
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
FactoryResetButton,
button.ApplyButton,
device_class=DEVICE_CLASS_RESTART,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
),
cv.Optional(CONF_RESTART): button.button_schema(
RestartButton,
button.ApplyButton,
device_class=DEVICE_CLASS_RESTART,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon=ICON_RESTART,
@@ -39,5 +36,9 @@ CONFIG_SCHEMA = {
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_LD2450_ID])
buttons = button.sub_buttons(config, parent=hub)
await buttons(CONF_FACTORY_RESET, hub.set_factory_reset_button)
await buttons(CONF_RESTART, hub.set_restart_button)
await buttons(
CONF_FACTORY_RESET, hub.set_factory_reset_button, apply="factory_reset()"
)
await buttons(
CONF_RESTART, hub.set_restart_button, apply="restart_and_read_all_info()"
)
@@ -1,7 +0,0 @@
#include "factory_reset_button.h"
namespace esphome::ld2450 {
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/button/button.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class FactoryResetButton : public button::Button, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) FactoryResetButton()` would zero-fill .bss that is already zero.
FactoryResetButton() {}
protected:
void press_action() override;
};
} // namespace esphome::ld2450
@@ -1,7 +0,0 @@
#include "restart_button.h"
namespace esphome::ld2450 {
void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); }
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/button/button.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class RestartButton : public button::Button, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) RestartButton()` would zero-fill .bss that is already zero.
RestartButton() {}
protected:
void press_action() override;
};
} // namespace esphome::ld2450
+3 -2
View File
@@ -24,7 +24,6 @@ ICON_ARROW_TOP_LEFT = "mdi:arrow-top-left"
ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE = "mdi:arrow-top-left-bold-box-outline"
MAX_ZONES = 3
PresenceTimeoutNumber = ld2450_ns.class_("PresenceTimeoutNumber", number.Number)
ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number)
CONFIG_SCHEMA = cv.Schema(
@@ -32,7 +31,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Required(CONF_PRESENCE_TIMEOUT): number.number_schema(
PresenceTimeoutNumber,
number.ApplyNumber,
unit_of_measurement=UNIT_SECOND,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_TIMELAPSE,
@@ -88,6 +87,8 @@ async def to_code(config: ConfigType) -> None:
min_value=0,
max_value=3600,
step=1,
apply="set_presence_timeout()",
publish="before",
)
for zone_num in range(MAX_ZONES):
if zone_conf := config.get(f"zone_{zone_num + 1}"):
@@ -1,10 +0,0 @@
#include "presence_timeout_number.h"
namespace esphome::ld2450 {
void PresenceTimeoutNumber::control(float value) {
this->publish_state(value);
this->parent_->set_presence_timeout();
}
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/number/number.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class PresenceTimeoutNumber : public number::Number, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) PresenceTimeoutNumber()` would zero-fill .bss that is already zero.
PresenceTimeoutNumber() {}
protected:
void control(float value) override;
};
} // namespace esphome::ld2450
+7 -5
View File
@@ -9,23 +9,21 @@ from esphome.const import (
)
from esphome.types import ConfigType
from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns
from .. import CONF_LD2450_ID, LD2450Component
CONF_ZONE_TYPE = "zone_type"
BaudRateSelect = ld2450_ns.class_("BaudRateSelect", select.Select)
ZoneTypeSelect = ld2450_ns.class_("ZoneTypeSelect", select.Select)
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Optional(CONF_BAUD_RATE): select.select_schema(
BaudRateSelect,
select.ApplySelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_THERMOMETER,
),
cv.Optional(CONF_ZONE_TYPE): select.select_schema(
ZoneTypeSelect,
select.ApplySelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_THERMOMETER,
),
@@ -48,6 +46,8 @@ async def to_code(config: ConfigType) -> None:
"256000",
"460800",
],
apply="set_baud_rate(entity->option_at(index))",
publish="before",
)
await selects(
CONF_ZONE_TYPE,
@@ -57,4 +57,6 @@ async def to_code(config: ConfigType) -> None:
"Detection",
"Filter",
],
apply="set_zone_type(entity->option_at(index))",
publish="before",
)
@@ -1,10 +0,0 @@
#include "baud_rate_select.h"
namespace esphome::ld2450 {
void BaudRateSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_baud_rate(this->option_at(index));
}
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class BaudRateSelect : public select::Select, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) BaudRateSelect()` would zero-fill .bss that is already zero.
BaudRateSelect() {}
protected:
void control(size_t index) override;
};
} // namespace esphome::ld2450
@@ -1,10 +0,0 @@
#include "zone_type_select.h"
namespace esphome::ld2450 {
void ZoneTypeSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_zone_type(this->option_at(index));
}
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class ZoneTypeSelect : public select::Select, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) ZoneTypeSelect()` would zero-fill .bss that is already zero.
ZoneTypeSelect() {}
protected:
void control(size_t index) override;
};
} // namespace esphome::ld2450
+15 -8
View File
@@ -11,10 +11,7 @@ from esphome.const import (
)
from esphome.types import ConfigType
from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns
BluetoothSwitch = ld2450_ns.class_("BluetoothSwitch", switch.Switch)
MultiTargetSwitch = ld2450_ns.class_("MultiTargetSwitch", switch.Switch)
from .. import CONF_LD2450_ID, LD2450Component
CONF_MULTI_TARGET = "multi_target"
@@ -22,13 +19,13 @@ CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
cv.Optional(CONF_BLUETOOTH): switch.switch_schema(
BluetoothSwitch,
switch.ApplySwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_BLUETOOTH,
),
cv.Optional(CONF_MULTI_TARGET): switch.switch_schema(
MultiTargetSwitch,
switch.ApplySwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_PULSE,
@@ -39,5 +36,15 @@ CONFIG_SCHEMA = {
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_LD2450_ID])
switches = switch.sub_switches(config, parent=hub)
await switches(CONF_BLUETOOTH, hub.set_bluetooth_switch)
await switches(CONF_MULTI_TARGET, hub.set_multi_target_switch)
await switches(
CONF_BLUETOOTH,
hub.set_bluetooth_switch,
apply="set_bluetooth(state)",
publish="before",
)
await switches(
CONF_MULTI_TARGET,
hub.set_multi_target_switch,
apply="set_multi_target(state)",
publish="before",
)
@@ -1,10 +0,0 @@
#include "bluetooth_switch.h"
namespace esphome::ld2450 {
void BluetoothSwitch::write_state(bool state) {
this->publish_state(state);
this->parent_->set_bluetooth(state);
}
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class BluetoothSwitch : public switch_::Switch, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) BluetoothSwitch()` would zero-fill .bss that is already zero.
BluetoothSwitch() {}
protected:
void write_state(bool state) override;
};
} // namespace esphome::ld2450
@@ -1,10 +0,0 @@
#include "multi_target_switch.h"
namespace esphome::ld2450 {
void MultiTargetSwitch::write_state(bool state) {
this->publish_state(state);
this->parent_->set_multi_target(state);
}
} // namespace esphome::ld2450
@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "../ld2450.h"
namespace esphome::ld2450 {
class MultiTargetSwitch : public switch_::Switch, public Parented<LD2450Component> {
public:
// User provided, not "= default": `new(p) MultiTargetSwitch()` would zero-fill .bss that is already zero.
MultiTargetSwitch() {}
protected:
void write_state(bool state) override;
};
} // namespace esphome::ld2450