Skip to content

Commit 0256e00

Browse files
Rihan9kbx81bdraco
authored
[ld2412] New component (esphome#9075)
Co-authored-by: Keith Burzinski <kbx81x@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent c65af68 commit 0256e00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2220
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ esphome/components/kuntze/* @ssieb
246246
esphome/components/lc709203f/* @ilikecake
247247
esphome/components/lcd_menu/* @numo68
248248
esphome/components/ld2410/* @regevbr @sebcaps
249+
esphome/components/ld2412/* @Rihan9
249250
esphome/components/ld2420/* @descipher
250251
esphome/components/ld2450/* @hareeshmu
251252
esphome/components/ld24xx/* @kbx81
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import esphome.codegen as cg
2+
from esphome.components import uart
3+
import esphome.config_validation as cv
4+
from esphome.const import CONF_ID, CONF_THROTTLE
5+
6+
AUTO_LOAD = ["ld24xx"]
7+
CODEOWNERS = ["@Rihan9"]
8+
DEPENDENCIES = ["uart"]
9+
MULTI_CONF = True
10+
11+
LD2412_ns = cg.esphome_ns.namespace("ld2412")
12+
LD2412Component = LD2412_ns.class_("LD2412Component", cg.Component, uart.UARTDevice)
13+
14+
CONF_LD2412_ID = "ld2412_id"
15+
16+
CONF_MAX_MOVE_DISTANCE = "max_move_distance"
17+
CONF_MAX_STILL_DISTANCE = "max_still_distance"
18+
CONF_MOVE_THRESHOLDS = [f"g{x}_move_threshold" for x in range(9)]
19+
CONF_STILL_THRESHOLDS = [f"g{x}_still_threshold" for x in range(9)]
20+
21+
CONFIG_SCHEMA = (
22+
cv.Schema(
23+
{
24+
cv.GenerateID(): cv.declare_id(LD2412Component),
25+
cv.Optional(CONF_THROTTLE): cv.invalid(
26+
f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead"
27+
),
28+
}
29+
)
30+
.extend(uart.UART_DEVICE_SCHEMA)
31+
.extend(cv.COMPONENT_SCHEMA)
32+
)
33+
34+
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
35+
"ld2412",
36+
require_tx=True,
37+
require_rx=True,
38+
parity="NONE",
39+
stop_bits=1,
40+
)
41+
42+
43+
async def to_code(config):
44+
var = cg.new_Pvariable(config[CONF_ID])
45+
await cg.register_component(var, config)
46+
await uart.register_uart_device(var, config)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import esphome.codegen as cg
2+
from esphome.components import binary_sensor
3+
import esphome.config_validation as cv
4+
from esphome.const import (
5+
CONF_HAS_MOVING_TARGET,
6+
CONF_HAS_STILL_TARGET,
7+
CONF_HAS_TARGET,
8+
DEVICE_CLASS_MOTION,
9+
DEVICE_CLASS_OCCUPANCY,
10+
DEVICE_CLASS_RUNNING,
11+
ENTITY_CATEGORY_DIAGNOSTIC,
12+
ICON_ACCOUNT,
13+
ICON_MOTION_SENSOR,
14+
)
15+
16+
from . import CONF_LD2412_ID, LD2412Component
17+
18+
DEPENDENCIES = ["ld2412"]
19+
20+
CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS = "dynamic_background_correction_status"
21+
22+
CONFIG_SCHEMA = {
23+
cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
24+
cv.Optional(
25+
CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS
26+
): binary_sensor.binary_sensor_schema(
27+
device_class=DEVICE_CLASS_RUNNING,
28+
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
29+
icon=ICON_ACCOUNT,
30+
),
31+
cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
32+
device_class=DEVICE_CLASS_OCCUPANCY,
33+
filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
34+
icon=ICON_ACCOUNT,
35+
),
36+
cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
37+
device_class=DEVICE_CLASS_MOTION,
38+
filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
39+
icon=ICON_MOTION_SENSOR,
40+
),
41+
cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
42+
device_class=DEVICE_CLASS_OCCUPANCY,
43+
filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
44+
icon=ICON_MOTION_SENSOR,
45+
),
46+
}
47+
48+
49+
async def to_code(config):
50+
LD2412_component = await cg.get_variable(config[CONF_LD2412_ID])
51+
if dynamic_background_correction_status_config := config.get(
52+
CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS
53+
):
54+
sens = await binary_sensor.new_binary_sensor(
55+
dynamic_background_correction_status_config
56+
)
57+
cg.add(
58+
LD2412_component.set_dynamic_background_correction_status_binary_sensor(
59+
sens
60+
)
61+
)
62+
if has_target_config := config.get(CONF_HAS_TARGET):
63+
sens = await binary_sensor.new_binary_sensor(has_target_config)
64+
cg.add(LD2412_component.set_target_binary_sensor(sens))
65+
if has_moving_target_config := config.get(CONF_HAS_MOVING_TARGET):
66+
sens = await binary_sensor.new_binary_sensor(has_moving_target_config)
67+
cg.add(LD2412_component.set_moving_target_binary_sensor(sens))
68+
if has_still_target_config := config.get(CONF_HAS_STILL_TARGET):
69+
sens = await binary_sensor.new_binary_sensor(has_still_target_config)
70+
cg.add(LD2412_component.set_still_target_binary_sensor(sens))
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import esphome.codegen as cg
2+
from esphome.components import button
3+
import esphome.config_validation as cv
4+
from esphome.const import (
5+
CONF_FACTORY_RESET,
6+
CONF_RESTART,
7+
DEVICE_CLASS_RESTART,
8+
ENTITY_CATEGORY_CONFIG,
9+
ENTITY_CATEGORY_DIAGNOSTIC,
10+
ICON_DATABASE,
11+
ICON_PULSE,
12+
ICON_RESTART,
13+
ICON_RESTART_ALERT,
14+
)
15+
16+
from .. import CONF_LD2412_ID, LD2412_ns, LD2412Component
17+
18+
FactoryResetButton = LD2412_ns.class_("FactoryResetButton", button.Button)
19+
QueryButton = LD2412_ns.class_("QueryButton", button.Button)
20+
RestartButton = LD2412_ns.class_("RestartButton", button.Button)
21+
StartDynamicBackgroundCorrectionButton = LD2412_ns.class_(
22+
"StartDynamicBackgroundCorrectionButton", button.Button
23+
)
24+
25+
CONF_QUERY_PARAMS = "query_params"
26+
CONF_START_DYNAMIC_BACKGROUND_CORRECTION = "start_dynamic_background_correction"
27+
28+
CONFIG_SCHEMA = {
29+
cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
30+
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
31+
FactoryResetButton,
32+
device_class=DEVICE_CLASS_RESTART,
33+
entity_category=ENTITY_CATEGORY_CONFIG,
34+
icon=ICON_RESTART_ALERT,
35+
),
36+
cv.Optional(CONF_QUERY_PARAMS): button.button_schema(
37+
QueryButton,
38+
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
39+
icon=ICON_DATABASE,
40+
),
41+
cv.Optional(CONF_RESTART): button.button_schema(
42+
RestartButton,
43+
device_class=DEVICE_CLASS_RESTART,
44+
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
45+
icon=ICON_RESTART,
46+
),
47+
cv.Optional(CONF_START_DYNAMIC_BACKGROUND_CORRECTION): button.button_schema(
48+
StartDynamicBackgroundCorrectionButton,
49+
entity_category=ENTITY_CATEGORY_CONFIG,
50+
icon=ICON_PULSE,
51+
),
52+
}
53+
54+
55+
async def to_code(config):
56+
LD2412_component = await cg.get_variable(config[CONF_LD2412_ID])
57+
if factory_reset_config := config.get(CONF_FACTORY_RESET):
58+
b = await button.new_button(factory_reset_config)
59+
await cg.register_parented(b, config[CONF_LD2412_ID])
60+
cg.add(LD2412_component.set_factory_reset_button(b))
61+
if query_params_config := config.get(CONF_QUERY_PARAMS):
62+
b = await button.new_button(query_params_config)
63+
await cg.register_parented(b, config[CONF_LD2412_ID])
64+
cg.add(LD2412_component.set_query_button(b))
65+
if restart_config := config.get(CONF_RESTART):
66+
b = await button.new_button(restart_config)
67+
await cg.register_parented(b, config[CONF_LD2412_ID])
68+
cg.add(LD2412_component.set_restart_button(b))
69+
if start_dynamic_background_correction_config := config.get(
70+
CONF_START_DYNAMIC_BACKGROUND_CORRECTION
71+
):
72+
b = await button.new_button(start_dynamic_background_correction_config)
73+
await cg.register_parented(b, config[CONF_LD2412_ID])
74+
cg.add(LD2412_component.set_start_dynamic_background_correction_button(b))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "factory_reset_button.h"
2+
3+
namespace esphome {
4+
namespace ld2412 {
5+
6+
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
7+
8+
} // namespace ld2412
9+
} // namespace esphome
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "esphome/components/button/button.h"
4+
#include "../ld2412.h"
5+
6+
namespace esphome {
7+
namespace ld2412 {
8+
9+
class FactoryResetButton : public button::Button, public Parented<LD2412Component> {
10+
public:
11+
FactoryResetButton() = default;
12+
13+
protected:
14+
void press_action() override;
15+
};
16+
17+
} // namespace ld2412
18+
} // namespace esphome
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "query_button.h"
2+
3+
namespace esphome {
4+
namespace ld2412 {
5+
6+
void QueryButton::press_action() { this->parent_->read_all_info(); }
7+
8+
} // namespace ld2412
9+
} // namespace esphome
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "esphome/components/button/button.h"
4+
#include "../ld2412.h"
5+
6+
namespace esphome {
7+
namespace ld2412 {
8+
9+
class QueryButton : public button::Button, public Parented<LD2412Component> {
10+
public:
11+
QueryButton() = default;
12+
13+
protected:
14+
void press_action() override;
15+
};
16+
17+
} // namespace ld2412
18+
} // namespace esphome
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "restart_button.h"
2+
3+
namespace esphome {
4+
namespace ld2412 {
5+
6+
void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); }
7+
8+
} // namespace ld2412
9+
} // namespace esphome
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "esphome/components/button/button.h"
4+
#include "../ld2412.h"
5+
6+
namespace esphome {
7+
namespace ld2412 {
8+
9+
class RestartButton : public button::Button, public Parented<LD2412Component> {
10+
public:
11+
RestartButton() = default;
12+
13+
protected:
14+
void press_action() override;
15+
};
16+
17+
} // namespace ld2412
18+
} // namespace esphome

0 commit comments

Comments
 (0)