mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 04:55:48 +08:00
[ltr_als_ps] Migrate triggers to callback automation (#15224)
This commit is contained in:
@@ -58,6 +58,14 @@ class LTRAlsPsComponent : public PollingComponent, public i2c::I2CDevice {
|
|||||||
void set_actual_integration_time_sensor(sensor::Sensor *sensor) { this->actual_integration_time_sensor_ = sensor; }
|
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; }
|
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:
|
protected:
|
||||||
//
|
//
|
||||||
// Internal state machine, used to split all the actions into
|
// Internal state machine, used to split all the actions into
|
||||||
@@ -151,36 +159,8 @@ class LTRAlsPsComponent : public PollingComponent, public i2c::I2CDevice {
|
|||||||
}
|
}
|
||||||
bool is_any_ps_sensor_enabled_() const { return this->proximity_counts_sensor_ != nullptr; }
|
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_high_trigger_callback_;
|
||||||
CallbackManager<void()> on_ps_low_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(LTRAlsPsComponent *parent) {
|
|
||||||
parent->add_on_ps_high_trigger_callback_([this]() { this->trigger(); });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class LTRPsLowTrigger : public Trigger<> {
|
|
||||||
public:
|
|
||||||
explicit LTRPsLowTrigger(LTRAlsPsComponent *parent) {
|
|
||||||
parent->add_on_ps_low_trigger_callback_([this]() { this->trigger(); });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
} // namespace ltr_als_ps
|
} // namespace ltr_als_ps
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ from esphome.const import (
|
|||||||
CONF_INTEGRATION_TIME,
|
CONF_INTEGRATION_TIME,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_REPEAT,
|
CONF_REPEAT,
|
||||||
CONF_TRIGGER_ID,
|
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
DEVICE_CLASS_ILLUMINANCE,
|
||||||
ICON_BRIGHTNESS_5,
|
ICON_BRIGHTNESS_5,
|
||||||
@@ -93,11 +92,6 @@ PS_GAINS = {
|
|||||||
"64X": PsGain.PS_GAIN_64,
|
"64X": PsGain.PS_GAIN_64,
|
||||||
}
|
}
|
||||||
|
|
||||||
LTRPsHighTrigger = ltr_als_ps_ns.class_(
|
|
||||||
"LTRPsHighTrigger", automation.Trigger.template()
|
|
||||||
)
|
|
||||||
LTRPsLowTrigger = ltr_als_ps_ns.class_("LTRPsLowTrigger", automation.Trigger.template())
|
|
||||||
|
|
||||||
|
|
||||||
def validate_integration_time(value):
|
def validate_integration_time(value):
|
||||||
value = cv.positive_time_period_milliseconds(value).total_milliseconds
|
value = cv.positive_time_period_milliseconds(value).total_milliseconds
|
||||||
@@ -143,16 +137,8 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
cv.Optional(CONF_PS_LOW_THRESHOLD, default=0): cv.int_range(
|
cv.Optional(CONF_PS_LOW_THRESHOLD, default=0): cv.int_range(
|
||||||
min=0, max=65535
|
min=0, max=65535
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_ON_PS_HIGH_THRESHOLD): automation.validate_automation(
|
cv.Optional(CONF_ON_PS_HIGH_THRESHOLD): automation.validate_automation({}),
|
||||||
{
|
cv.Optional(CONF_ON_PS_LOW_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_AMBIENT_LIGHT): cv.maybe_simple_value(
|
cv.Optional(CONF_AMBIENT_LIGHT): cv.maybe_simple_value(
|
||||||
sensor.sensor_schema(
|
sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_LUX,
|
unit_of_measurement=UNIT_LUX,
|
||||||
@@ -244,13 +230,14 @@ async def to_code(config):
|
|||||||
sens = await sensor.new_sensor(prox_cnt_config)
|
sens = await sensor.new_sensor(prox_cnt_config)
|
||||||
cg.add(var.set_proximity_counts_sensor(sens))
|
cg.add(var.set_proximity_counts_sensor(sens))
|
||||||
|
|
||||||
for prox_high_tr in config.get(CONF_ON_PS_HIGH_THRESHOLD, []):
|
for conf in config.get(CONF_ON_PS_HIGH_THRESHOLD, []):
|
||||||
trigger = cg.new_Pvariable(prox_high_tr[CONF_TRIGGER_ID], var)
|
await automation.build_callback_automation(
|
||||||
await automation.build_automation(trigger, [], prox_high_tr)
|
var, "add_on_ps_high_trigger_callback", [], conf
|
||||||
|
)
|
||||||
for prox_low_tr in config.get(CONF_ON_PS_LOW_THRESHOLD, []):
|
for conf in config.get(CONF_ON_PS_LOW_THRESHOLD, []):
|
||||||
trigger = cg.new_Pvariable(prox_low_tr[CONF_TRIGGER_ID], var)
|
await automation.build_callback_automation(
|
||||||
await automation.build_automation(trigger, [], prox_low_tr)
|
var, "add_on_ps_low_trigger_callback", [], conf
|
||||||
|
)
|
||||||
|
|
||||||
cg.add(var.set_ltr_type(config[CONF_TYPE]))
|
cg.add(var.set_ltr_type(config[CONF_TYPE]))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user