mirror of
https://github.com/esphome/esphome.git
synced 2026-05-23 11:16:52 +08:00
[ltr501] Migrate triggers to callback automation (#15225)
This commit is contained in:
@@ -58,6 +58,14 @@ class LTRAlsPs501Component : public PollingComponent, public i2c::I2CDevice {
|
||||
void set_actual_integration_time_sensor(sensor::Sensor *sensor) { this->actual_integration_time_sensor_ = sensor; }
|
||||
void set_proximity_counts_sensor(sensor::Sensor *sensor) { this->proximity_counts_sensor_ = sensor; }
|
||||
|
||||
template<typename F> void add_on_ps_high_trigger_callback(F &&callback) {
|
||||
this->on_ps_high_trigger_callback_.add(std::forward<F>(callback));
|
||||
}
|
||||
|
||||
template<typename F> void add_on_ps_low_trigger_callback(F &&callback) {
|
||||
this->on_ps_low_trigger_callback_.add(std::forward<F>(callback));
|
||||
}
|
||||
|
||||
protected:
|
||||
//
|
||||
// Internal state machine, used to split all the actions into
|
||||
@@ -151,36 +159,8 @@ class LTRAlsPs501Component : public PollingComponent, public i2c::I2CDevice {
|
||||
}
|
||||
bool is_any_ps_sensor_enabled_() const { return this->proximity_counts_sensor_ != nullptr; }
|
||||
|
||||
//
|
||||
// Trigger section for the automations
|
||||
//
|
||||
friend class LTRPsHighTrigger;
|
||||
friend class LTRPsLowTrigger;
|
||||
|
||||
CallbackManager<void()> on_ps_high_trigger_callback_;
|
||||
CallbackManager<void()> on_ps_low_trigger_callback_;
|
||||
|
||||
template<typename F> void add_on_ps_high_trigger_callback_(F &&callback) {
|
||||
this->on_ps_high_trigger_callback_.add(std::forward<F>(callback));
|
||||
}
|
||||
|
||||
template<typename F> void add_on_ps_low_trigger_callback_(F &&callback) {
|
||||
this->on_ps_low_trigger_callback_.add(std::forward<F>(callback));
|
||||
}
|
||||
};
|
||||
|
||||
class LTRPsHighTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit LTRPsHighTrigger(LTRAlsPs501Component *parent) {
|
||||
parent->add_on_ps_high_trigger_callback_([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
|
||||
class LTRPsLowTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit LTRPsLowTrigger(LTRAlsPs501Component *parent) {
|
||||
parent->add_on_ps_low_trigger_callback_([this]() { this->trigger(); });
|
||||
}
|
||||
};
|
||||
} // namespace ltr501
|
||||
} // namespace esphome
|
||||
|
||||
@@ -14,7 +14,6 @@ from esphome.const import (
|
||||
CONF_INTEGRATION_TIME,
|
||||
CONF_NAME,
|
||||
CONF_REPEAT,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_TYPE,
|
||||
DEVICE_CLASS_DISTANCE,
|
||||
DEVICE_CLASS_ILLUMINANCE,
|
||||
@@ -87,9 +86,6 @@ PS_GAINS = {
|
||||
"16X": PsGain.PS_GAIN_16,
|
||||
}
|
||||
|
||||
LTRPsHighTrigger = ltr501_ns.class_("LTRPsHighTrigger", automation.Trigger.template())
|
||||
LTRPsLowTrigger = ltr501_ns.class_("LTRPsLowTrigger", automation.Trigger.template())
|
||||
|
||||
|
||||
def validate_integration_time(value):
|
||||
value = cv.positive_time_period_milliseconds(value).total_milliseconds
|
||||
@@ -146,16 +142,8 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_PS_LOW_THRESHOLD, default=0): cv.int_range(
|
||||
min=0, max=65535
|
||||
),
|
||||
cv.Optional(CONF_ON_PS_HIGH_THRESHOLD): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LTRPsHighTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PS_LOW_THRESHOLD): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LTRPsLowTrigger),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_PS_HIGH_THRESHOLD): automation.validate_automation({}),
|
||||
cv.Optional(CONF_ON_PS_LOW_THRESHOLD): automation.validate_automation({}),
|
||||
cv.Optional(CONF_AMBIENT_LIGHT): cv.maybe_simple_value(
|
||||
sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_LUX,
|
||||
@@ -252,13 +240,14 @@ async def to_code(config):
|
||||
sens = await sensor.new_sensor(prox_cnt_config)
|
||||
cg.add(var.set_proximity_counts_sensor(sens))
|
||||
|
||||
for prox_high_tr in config.get(CONF_ON_PS_HIGH_THRESHOLD, []):
|
||||
trigger = cg.new_Pvariable(prox_high_tr[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], prox_high_tr)
|
||||
|
||||
for prox_low_tr in config.get(CONF_ON_PS_LOW_THRESHOLD, []):
|
||||
trigger = cg.new_Pvariable(prox_low_tr[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], prox_low_tr)
|
||||
for conf in config.get(CONF_ON_PS_HIGH_THRESHOLD, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_ps_high_trigger_callback", [], conf
|
||||
)
|
||||
for conf in config.get(CONF_ON_PS_LOW_THRESHOLD, []):
|
||||
await automation.build_callback_automation(
|
||||
var, "add_on_ps_low_trigger_callback", [], conf
|
||||
)
|
||||
|
||||
cg.add(var.set_ltr_type(config[CONF_TYPE]))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user