Skip to content

Commit 9969a8c

Browse files
committed
Add LD2450 component
Based on LD2450 components. Adds installation_angle and flip_x_axis to configuration.
1 parent 0b1c117 commit 9969a8c

28 files changed

+2241
-0
lines changed

components/ld2450/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
DEPENDENCIES = ["uart"]
7+
CODEOWNERS = ["@hareeshmu"]
8+
MULTI_CONF = True
9+
10+
ld2450_ns = cg.esphome_ns.namespace("ld2450")
11+
LD2450Component = ld2450_ns.class_("LD2450Component", cg.Component, uart.UARTDevice)
12+
13+
CONF_LD2450_ID = "ld2450_id"
14+
CONF_FLIP_X_AXIS = "flip_x_axis"
15+
16+
CONFIG_SCHEMA = cv.All(
17+
cv.Schema(
18+
{
19+
cv.GenerateID(): cv.declare_id(LD2450Component),
20+
cv.Optional(CONF_THROTTLE, default="1000ms"): cv.All(
21+
cv.positive_time_period_milliseconds,
22+
cv.Range(min=cv.TimePeriod(milliseconds=1)),
23+
),
24+
cv.Optional(CONF_FLIP_X_AXIS, default=False): cv.boolean,
25+
}
26+
)
27+
.extend(uart.UART_DEVICE_SCHEMA)
28+
.extend(cv.COMPONENT_SCHEMA)
29+
)
30+
31+
LD2450BaseSchema = cv.Schema(
32+
{
33+
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
34+
},
35+
)
36+
37+
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
38+
"ld2450",
39+
require_tx=True,
40+
require_rx=True,
41+
parity="NONE",
42+
stop_bits=1,
43+
)
44+
45+
46+
async def to_code(config):
47+
var = cg.new_Pvariable(config[CONF_ID])
48+
await cg.register_component(var, config)
49+
await uart.register_uart_device(var, config)
50+
cg.add(var.set_throttle(config[CONF_THROTTLE]))
51+
cg.add(var.set_flip_x_axis(config[CONF_FLIP_X_AXIS]))

components/ld2450/binary_sensor.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
)
11+
12+
from . import CONF_LD2450_ID, LD2450Component
13+
14+
DEPENDENCIES = ["ld2450"]
15+
16+
ICON_MEDITATION = "mdi:meditation"
17+
ICON_SHIELD_ACCOUNT = "mdi:shield-account"
18+
ICON_TARGET_ACCOUNT = "mdi:target-account"
19+
20+
CONFIG_SCHEMA = {
21+
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
22+
cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
23+
device_class=DEVICE_CLASS_OCCUPANCY,
24+
icon=ICON_SHIELD_ACCOUNT,
25+
),
26+
cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
27+
device_class=DEVICE_CLASS_MOTION,
28+
icon=ICON_TARGET_ACCOUNT,
29+
),
30+
cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
31+
device_class=DEVICE_CLASS_OCCUPANCY,
32+
icon=ICON_MEDITATION,
33+
),
34+
}
35+
36+
37+
async def to_code(config):
38+
ld2450_component = await cg.get_variable(config[CONF_LD2450_ID])
39+
if has_target_config := config.get(CONF_HAS_TARGET):
40+
sens = await binary_sensor.new_binary_sensor(has_target_config)
41+
cg.add(ld2450_component.set_target_binary_sensor(sens))
42+
if has_moving_target_config := config.get(CONF_HAS_MOVING_TARGET):
43+
sens = await binary_sensor.new_binary_sensor(has_moving_target_config)
44+
cg.add(ld2450_component.set_moving_target_binary_sensor(sens))
45+
if has_still_target_config := config.get(CONF_HAS_STILL_TARGET):
46+
sens = await binary_sensor.new_binary_sensor(has_still_target_config)
47+
cg.add(ld2450_component.set_still_target_binary_sensor(sens))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_RESTART,
11+
ICON_RESTART_ALERT,
12+
)
13+
14+
from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns
15+
16+
ResetButton = ld2450_ns.class_("ResetButton", button.Button)
17+
RestartButton = ld2450_ns.class_("RestartButton", button.Button)
18+
19+
CONFIG_SCHEMA = {
20+
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
21+
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
22+
ResetButton,
23+
device_class=DEVICE_CLASS_RESTART,
24+
entity_category=ENTITY_CATEGORY_CONFIG,
25+
icon=ICON_RESTART_ALERT,
26+
),
27+
cv.Optional(CONF_RESTART): button.button_schema(
28+
RestartButton,
29+
device_class=DEVICE_CLASS_RESTART,
30+
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
31+
icon=ICON_RESTART,
32+
),
33+
}
34+
35+
36+
async def to_code(config):
37+
ld2450_component = await cg.get_variable(config[CONF_LD2450_ID])
38+
if factory_reset_config := config.get(CONF_FACTORY_RESET):
39+
b = await button.new_button(factory_reset_config)
40+
await cg.register_parented(b, config[CONF_LD2450_ID])
41+
cg.add(ld2450_component.set_reset_button(b))
42+
if restart_config := config.get(CONF_RESTART):
43+
b = await button.new_button(restart_config)
44+
await cg.register_parented(b, config[CONF_LD2450_ID])
45+
cg.add(ld2450_component.set_restart_button(b))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "reset_button.h"
2+
3+
namespace esphome {
4+
namespace ld2450 {
5+
6+
void ResetButton::press_action() { this->parent_->factory_reset(); }
7+
8+
} // namespace ld2450
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 "../ld2450.h"
5+
6+
namespace esphome {
7+
namespace ld2450 {
8+
9+
class ResetButton : public button::Button, public Parented<LD2450Component> {
10+
public:
11+
ResetButton() = default;
12+
13+
protected:
14+
void press_action() override;
15+
};
16+
17+
} // namespace ld2450
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 ld2450 {
5+
6+
void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); }
7+
8+
} // namespace ld2450
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 "../ld2450.h"
5+
6+
namespace esphome {
7+
namespace ld2450 {
8+
9+
class RestartButton : public button::Button, public Parented<LD2450Component> {
10+
public:
11+
RestartButton() = default;
12+
13+
protected:
14+
void press_action() override;
15+
};
16+
17+
} // namespace ld2450
18+
} // namespace esphome

0 commit comments

Comments
 (0)