14 static const char *
const TAG =
"template.alarm_control_panel";
18 #ifdef USE_BINARY_SENSOR 25 this->sensor_data_.push_back(sd);
26 this->sensor_map_[
sensor].store_index = this->next_store_index_++;
31 ESP_LOGCONFIG(TAG,
"TemplateAlarmControlPanel:");
33 ESP_LOGCONFIG(TAG,
" Number of Codes: %u", this->codes_.size());
34 if (!this->codes_.empty())
35 ESP_LOGCONFIG(TAG,
" Requires Code To Arm: %s", YESNO(this->requires_code_to_arm_));
36 ESP_LOGCONFIG(TAG,
" Arming Away Time: %" PRIu32
"s", (this->arming_away_time_ / 1000));
37 if (this->arming_home_time_ != 0)
38 ESP_LOGCONFIG(TAG,
" Arming Home Time: %" PRIu32
"s", (this->arming_home_time_ / 1000));
39 if (this->arming_night_time_ != 0)
40 ESP_LOGCONFIG(TAG,
" Arming Night Time: %" PRIu32
"s", (this->arming_night_time_ / 1000));
41 ESP_LOGCONFIG(TAG,
" Pending Time: %" PRIu32
"s", (this->pending_time_ / 1000));
42 ESP_LOGCONFIG(TAG,
" Trigger Time: %" PRIu32
"s", (this->trigger_time_ / 1000));
43 ESP_LOGCONFIG(TAG,
" Supported Features: %" PRIu32, this->get_supported_features());
44 #ifdef USE_BINARY_SENSOR 45 for (
auto sensor_info : this->sensor_map_) {
46 ESP_LOGCONFIG(TAG,
" Binary Sensor:");
47 ESP_LOGCONFIG(TAG,
" Name: %s", sensor_info.first->get_name().c_str());
48 ESP_LOGCONFIG(TAG,
" Armed home bypass: %s",
50 ESP_LOGCONFIG(TAG,
" Armed night bypass: %s",
53 const char *sensor_type;
54 switch (sensor_info.second.type) {
56 sensor_type =
"instant";
59 sensor_type =
"delayed_follower";
63 sensor_type =
"delayed";
65 ESP_LOGCONFIG(TAG,
" Sensor type: %s", sensor_type);
71 ESP_LOGCONFIG(TAG,
"Setting up Template AlarmControlPanel '%s'...", this->name_.c_str());
72 switch (this->restore_mode_) {
79 if (this->pref_.load(&value)) {
87 this->desired_state_ = this->current_state_;
93 auto delay = this->arming_away_time_;
95 delay = this->arming_home_time_;
98 delay = this->arming_night_time_;
101 this->publish_state(this->desired_state_);
110 auto future_state = this->current_state_;
113 (
millis() - this->last_update_) > this->trigger_time_) {
114 future_state = this->desired_state_;
117 bool delayed_sensor_not_ready =
false;
118 bool instant_sensor_not_ready =
false;
120 #ifdef USE_BINARY_SENSOR 122 for (
auto sensor_info : this->sensor_map_) {
126 if ((!this->sensor_data_[sensor_info.second.store_index].last_chime_state) && (sensor_info.first->state)) {
129 this->chime_callback_.call();
133 this->sensor_data_[sensor_info.second.store_index].last_chime_state = sensor_info.first->state;
136 if (sensor_info.first->state) {
150 instant_sensor_not_ready =
true;
157 delayed_sensor_not_ready =
true;
159 instant_sensor_not_ready =
true;
164 delayed_sensor_not_ready =
true;
170 this->sensors_ready_ = ((!instant_sensor_not_ready) && (!delayed_sensor_not_ready));
173 if (this->sensors_ready_ != this->sensors_ready_last_) {
174 this->ready_callback_.call();
175 this->sensors_ready_last_ = this->sensors_ready_;
179 if (this->is_state_armed(future_state) && (!this->sensors_ready_)) {
181 if (instant_sensor_not_ready) {
183 }
else if (delayed_sensor_not_ready) {
191 }
else if (future_state != this->current_state_) {
192 this->publish_state(future_state);
197 if (!this->codes_.empty()) {
199 ESP_LOGVV(TAG,
"Checking code: %s", code.
value().c_str());
200 return (std::count(this->codes_.begin(), this->codes_.end(), code.
value()) == 1);
202 ESP_LOGD(TAG,
"No code provided");
210 if (this->supports_arm_home_) {
213 if (this->supports_arm_night_) {
224 ESP_LOGW(TAG,
"Cannot arm when not disarmed");
227 if (this->requires_code_to_arm_ && !this->is_code_valid_(std::move(code))) {
228 ESP_LOGW(TAG,
"Not arming code doesn't match");
231 this->desired_state_ =
state;
235 this->publish_state(state);
248 if (!this->is_code_valid_(call.
get_code())) {
249 ESP_LOGW(TAG,
"Not disarming code doesn't match");
259 ESP_LOGE(TAG,
"State not yet implemented: %s",
value_type const & value() const
void add_sensor(binary_sensor::BinarySensor *sensor, uint16_t flags=0, AlarmSensorType type=ALARM_SENSOR_TYPE_DELAYED)
Add a binary_sensor to the alarm_panel.
void control(const alarm_control_panel::AlarmControlPanelCall &call) override
uint32_t get_supported_features() const override
void dump_config() override
bool get_requires_code() const override
bool is_code_valid_(optional< std::string > code)
uint32_t IRAM_ATTR HOT millis()
const LogString * alarm_control_panel_state_to_string(AlarmControlPanelState state)
Returns a string representation of the state.
const optional< std::string > & get_code() const
ESPPreferences * global_preferences
void arm_(optional< std::string > code, alarm_control_panel::AlarmControlPanelState state, uint32_t delay)
const optional< AlarmControlPanelState > & get_state() const
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
Implementation of SPI Controller mode.
Base class for all binary_sensor-type classes.
TemplateAlarmControlPanel()
esphome::sensor::Sensor * sensor
void IRAM_ATTR HOT delay(uint32_t ms)