mirror of
https://github.com/esphome/esphome.git
synced 2026-10-20 13:06:45 +00:00
[ld2450] Use Apply switches
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user