mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 12:30:28 +02:00
Add configuration flow abilites to the ld2410 component (#4434)
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import number
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_TIMEOUT,
|
||||
DEVICE_CLASS_DISTANCE,
|
||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
UNIT_SECOND,
|
||||
UNIT_PERCENT,
|
||||
ENTITY_CATEGORY_CONFIG,
|
||||
ICON_MOTION_SENSOR,
|
||||
ICON_TIMELAPSE,
|
||||
ICON_LIGHTBULB,
|
||||
)
|
||||
from .. import CONF_LD2410_ID, LD2410Component, ld2410_ns
|
||||
|
||||
GateThresholdNumber = ld2410_ns.class_("GateThresholdNumber", number.Number)
|
||||
LightThresholdNumber = ld2410_ns.class_("LightThresholdNumber", number.Number)
|
||||
MaxDistanceTimeoutNumber = ld2410_ns.class_("MaxDistanceTimeoutNumber", number.Number)
|
||||
|
||||
CONF_MAX_MOVE_DISTANCE_GATE = "max_move_distance_gate"
|
||||
CONF_MAX_STILL_DISTANCE_GATE = "max_still_distance_gate"
|
||||
CONF_LIGHT_THRESHOLD = "light_threshold"
|
||||
CONF_STILL_THRESHOLD = "still_threshold"
|
||||
CONF_MOVE_THRESHOLD = "move_threshold"
|
||||
|
||||
TIMEOUT_GROUP = "timeout"
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
|
||||
cv.Inclusive(CONF_TIMEOUT, TIMEOUT_GROUP): number.number_schema(
|
||||
MaxDistanceTimeoutNumber,
|
||||
unit_of_measurement=UNIT_SECOND,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_TIMELAPSE,
|
||||
),
|
||||
cv.Inclusive(CONF_MAX_MOVE_DISTANCE_GATE, TIMEOUT_GROUP): number.number_schema(
|
||||
MaxDistanceTimeoutNumber,
|
||||
device_class=DEVICE_CLASS_DISTANCE,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_MOTION_SENSOR,
|
||||
),
|
||||
cv.Inclusive(CONF_MAX_STILL_DISTANCE_GATE, TIMEOUT_GROUP): number.number_schema(
|
||||
MaxDistanceTimeoutNumber,
|
||||
device_class=DEVICE_CLASS_DISTANCE,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_MOTION_SENSOR,
|
||||
),
|
||||
cv.Optional(CONF_LIGHT_THRESHOLD): number.number_schema(
|
||||
LightThresholdNumber,
|
||||
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_LIGHTBULB,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = CONFIG_SCHEMA.extend(
|
||||
{
|
||||
cv.Optional(f"g{x}"): cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_MOVE_THRESHOLD): number.number_schema(
|
||||
GateThresholdNumber,
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_MOTION_SENSOR,
|
||||
),
|
||||
cv.Required(CONF_STILL_THRESHOLD): number.number_schema(
|
||||
GateThresholdNumber,
|
||||
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_MOTION_SENSOR,
|
||||
),
|
||||
}
|
||||
)
|
||||
for x in range(9)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
ld2410_component = await cg.get_variable(config[CONF_LD2410_ID])
|
||||
if timeout_config := config.get(CONF_TIMEOUT):
|
||||
n = await number.new_number(
|
||||
timeout_config, min_value=0, max_value=65535, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_timeout_number(n))
|
||||
if max_move_distance_gate_config := config.get(CONF_MAX_MOVE_DISTANCE_GATE):
|
||||
n = await number.new_number(
|
||||
max_move_distance_gate_config, min_value=2, max_value=8, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_max_move_distance_gate_number(n))
|
||||
if max_still_distance_gate_config := config.get(CONF_MAX_STILL_DISTANCE_GATE):
|
||||
n = await number.new_number(
|
||||
max_still_distance_gate_config, min_value=2, max_value=8, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_max_still_distance_gate_number(n))
|
||||
if light_threshold_config := config.get(CONF_LIGHT_THRESHOLD):
|
||||
n = await number.new_number(
|
||||
light_threshold_config, min_value=0, max_value=255, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_light_threshold_number(n))
|
||||
for x in range(9):
|
||||
if gate_conf := config.get(f"g{x}"):
|
||||
move_config = gate_conf[CONF_MOVE_THRESHOLD]
|
||||
n = cg.new_Pvariable(move_config[CONF_ID], x)
|
||||
await number.register_number(
|
||||
n, move_config, min_value=0, max_value=100, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_gate_move_threshold_number(x, n))
|
||||
|
||||
still_config = gate_conf[CONF_STILL_THRESHOLD]
|
||||
n = cg.new_Pvariable(still_config[CONF_ID], x)
|
||||
await number.register_number(
|
||||
n, still_config, min_value=0, max_value=100, step=1
|
||||
)
|
||||
await cg.register_parented(n, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_gate_still_threshold_number(x, n))
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "gate_threshold_number.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
GateThresholdNumber::GateThresholdNumber(uint8_t gate) : gate_(gate) {}
|
||||
|
||||
void GateThresholdNumber::control(float value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_gate_threshold(this->gate_);
|
||||
}
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/number/number.h"
|
||||
#include "../ld2410.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
class GateThresholdNumber : public number::Number, public Parented<LD2410Component> {
|
||||
public:
|
||||
GateThresholdNumber(uint8_t gate);
|
||||
|
||||
protected:
|
||||
uint8_t gate_;
|
||||
void control(float value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "light_threshold_number.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void LightThresholdNumber::control(float value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_light_out_control();
|
||||
}
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/number/number.h"
|
||||
#include "../ld2410.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
class LightThresholdNumber : public number::Number, public Parented<LD2410Component> {
|
||||
public:
|
||||
LightThresholdNumber() = default;
|
||||
|
||||
protected:
|
||||
void control(float value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "max_distance_timeout_number.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void MaxDistanceTimeoutNumber::control(float value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_max_distances_timeout();
|
||||
}
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/number/number.h"
|
||||
#include "../ld2410.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
class MaxDistanceTimeoutNumber : public number::Number, public Parented<LD2410Component> {
|
||||
public:
|
||||
MaxDistanceTimeoutNumber() = default;
|
||||
|
||||
protected:
|
||||
void control(float value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user