mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 17:59:36 +01:00
.github
docker
esphome
api
components
a4988
adc
ade7953
ads1115
am2320
apds9960
api
as3935
as3935_i2c
as3935_spi
async_tcp
atm90e32
bang_bang
bh1750
binary
binary_sensor
__init__.py
automation.cpp
automation.h
binary_sensor.cpp
binary_sensor.h
filter.cpp
filter.h
binary_sensor_map
ble_presence
ble_rssi
bme280
bme680
bmp085
bmp280
captive_portal
ccs811
climate
climate_ir
coolix
cover
cse7766
ct_clamp
custom
custom_component
cwww
dallas
debug
deep_sleep
dfplayer
dht
dht12
display
duty_cycle
endstop
esp32_ble_beacon
esp32_ble_tracker
esp32_camera
esp32_hall
esp32_touch
esp8266_pwm
ethernet
fan
fastled_base
fastled_clockless
fastled_spi
font
fujitsu_general
globals
gpio
gps
hdc1080
hlw8012
hmc5883l
homeassistant
http_request
htu21d
hx711
i2c
image
ina219
ina226
ina3221
integration
interval
json
lcd_base
lcd_gpio
lcd_pcf8574
ledc
light
logger
max31855
max31865
max6675
max7219
mcp23008
mcp23017
mhz19
modbus
monochromatic
mpr121
mpu6050
mqtt
mqtt_subscribe
ms5611
my9231
neopixelbus
network
nextion
ntc
ota
output
partition
pca9685
pcf8574
pmsx003
pn532
power_supply
pulse_counter
pulse_width
pzem004t
pzemac
pzemdc
qmc5883l
rdm6300
remote_base
remote_receiver
remote_transmitter
resistance
restart
rgb
rgbw
rgbww
rotary_encoder
ruuvi_ble
ruuvitag
scd30
script
sds011
senseair
sensor
servo
sgp30
sht3xd
shtcx
shutdown
sim800l
slow_pwm
sm16716
sntp
speed
spi
sps30
ssd1306_base
ssd1306_i2c
ssd1306_spi
ssd1325_base
ssd1325_spi
status
status_led
stepper
sts3x
substitutions
sun
switch
sx1509
tcl112
tcs34725
template
text_sensor
time
time_based
tlc59208f
tm1651
total_daily_energy
tsl2561
ttp229_bsf
ttp229_lsf
tuya
tx20
uart
uln2003
ultrasonic
uptime
version
vl53l0x
voltage_sampler
waveshare_epaper
web_server
web_server_base
wifi
wifi_info
wifi_signal
xiaomi_ble
xiaomi_cgg1
xiaomi_hhccjcy01
xiaomi_lywsd02
xiaomi_lywsdcgq
xiaomi_miflora
xiaomi_mijia
yashima
zyaura
__init__.py
core
dashboard
__init__.py
__main__.py
automation.py
codegen.py
config.py
config_helpers.py
config_validation.py
const.py
core.py
core_config.py
cpp_generator.py
cpp_helpers.py
cpp_types.py
espota2.py
helpers.py
legacy.py
mqtt.py
pins.py
platformio_api.py
py_compat.py
storage_json.py
symlink_ops.py
util.py
voluptuous_schema.py
vscode.py
wizard.py
writer.py
yaml_util.py
zeroconf.py
script
tests
.clang-format
.clang-tidy
.dockerignore
.editorconfig
.gitignore
.gitlab-ci.yml
.gitpod.yml
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
MANIFEST.in
README.md
platformio.ini
pylintrc
requirements.txt
requirements_test.txt
setup.cfg
setup.py
## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] Tests have been added to verify that the new code works (under `tests/` folder). If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
114 lines
3.8 KiB
C++
114 lines
3.8 KiB
C++
#include "automation.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace binary_sensor {
|
|
|
|
static const char *TAG = "binary_sensor.automation";
|
|
|
|
void binary_sensor::MultiClickTrigger::on_state_(bool state) {
|
|
// Handle duplicate events
|
|
if (state == this->last_state_) {
|
|
return;
|
|
}
|
|
this->last_state_ = state;
|
|
|
|
// Cooldown: Do not immediately try matching after having invalid timing
|
|
if (this->is_in_cooldown_) {
|
|
return;
|
|
}
|
|
|
|
if (!this->at_index_.has_value()) {
|
|
// Start matching
|
|
MultiClickTriggerEvent evt = this->timing_[0];
|
|
if (evt.state == state) {
|
|
ESP_LOGV(TAG, "START min=%u max=%u", evt.min_length, evt.max_length);
|
|
ESP_LOGV(TAG, "Multi Click: Starting multi click action!");
|
|
this->at_index_ = 1;
|
|
if (this->timing_.size() == 1 && evt.max_length == 4294967294UL) {
|
|
this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
|
|
} else {
|
|
this->schedule_is_valid_(evt.min_length);
|
|
this->schedule_is_not_valid_(evt.max_length);
|
|
}
|
|
} else {
|
|
ESP_LOGV(TAG, "Multi Click: action not started because first level does not match!");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (!this->is_valid_) {
|
|
this->schedule_cooldown_();
|
|
return;
|
|
}
|
|
|
|
if (*this->at_index_ == this->timing_.size()) {
|
|
this->trigger_();
|
|
return;
|
|
}
|
|
|
|
MultiClickTriggerEvent evt = this->timing_[*this->at_index_];
|
|
|
|
if (evt.max_length != 4294967294UL) {
|
|
ESP_LOGV(TAG, "A i=%u min=%u max=%u", *this->at_index_, evt.min_length, evt.max_length); // NOLINT
|
|
this->schedule_is_valid_(evt.min_length);
|
|
this->schedule_is_not_valid_(evt.max_length);
|
|
} else if (*this->at_index_ + 1 != this->timing_.size()) {
|
|
ESP_LOGV(TAG, "B i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
|
|
this->cancel_timeout("is_not_valid");
|
|
this->schedule_is_valid_(evt.min_length);
|
|
} else {
|
|
ESP_LOGV(TAG, "C i=%u min=%u", *this->at_index_, evt.min_length); // NOLINT
|
|
this->is_valid_ = false;
|
|
this->cancel_timeout("is_not_valid");
|
|
this->set_timeout("trigger", evt.min_length, [this]() { this->trigger_(); });
|
|
}
|
|
|
|
*this->at_index_ = *this->at_index_ + 1;
|
|
}
|
|
void binary_sensor::MultiClickTrigger::schedule_cooldown_() {
|
|
ESP_LOGV(TAG, "Multi Click: Invalid length of press, starting cooldown of %u ms...", this->invalid_cooldown_);
|
|
this->is_in_cooldown_ = true;
|
|
this->set_timeout("cooldown", this->invalid_cooldown_, [this]() {
|
|
ESP_LOGV(TAG, "Multi Click: Cooldown ended, matching is now enabled again.");
|
|
this->is_in_cooldown_ = false;
|
|
});
|
|
this->at_index_.reset();
|
|
this->cancel_timeout("trigger");
|
|
this->cancel_timeout("is_valid");
|
|
this->cancel_timeout("is_not_valid");
|
|
}
|
|
void binary_sensor::MultiClickTrigger::schedule_is_valid_(uint32_t min_length) {
|
|
this->is_valid_ = false;
|
|
this->set_timeout("is_valid", min_length, [this]() {
|
|
ESP_LOGV(TAG, "Multi Click: You can now %s the button.", this->parent_->state ? "RELEASE" : "PRESS");
|
|
this->is_valid_ = true;
|
|
});
|
|
}
|
|
void binary_sensor::MultiClickTrigger::schedule_is_not_valid_(uint32_t max_length) {
|
|
this->set_timeout("is_not_valid", max_length, [this]() {
|
|
ESP_LOGV(TAG, "Multi Click: You waited too long to %s.", this->parent_->state ? "RELEASE" : "PRESS");
|
|
this->is_valid_ = false;
|
|
this->schedule_cooldown_();
|
|
});
|
|
}
|
|
void binary_sensor::MultiClickTrigger::trigger_() {
|
|
ESP_LOGV(TAG, "Multi Click: Hooray, multi click is valid. Triggering!");
|
|
this->at_index_.reset();
|
|
this->cancel_timeout("trigger");
|
|
this->cancel_timeout("is_valid");
|
|
this->cancel_timeout("is_not_valid");
|
|
this->trigger();
|
|
}
|
|
|
|
bool match_interval(uint32_t min_length, uint32_t max_length, uint32_t length) {
|
|
if (max_length == 0) {
|
|
return length >= min_length;
|
|
} else {
|
|
return length >= min_length && length <= max_length;
|
|
}
|
|
}
|
|
} // namespace binary_sensor
|
|
} // namespace esphome
|