ESPHome  2024.10.2
alarm_control_panel.cpp
Go to the documentation of this file.
1 #include <utility>
2 
3 #include "alarm_control_panel.h"
4 
6 #include "esphome/core/helpers.h"
7 #include "esphome/core/log.h"
8 
9 namespace esphome {
10 namespace alarm_control_panel {
11 
12 static const char *const TAG = "alarm_control_panel";
13 
15 
17  switch (state) {
23  return true;
24  default:
25  return false;
26  }
27 };
28 
30  this->last_update_ = millis();
31  if (state != this->current_state_) {
32  auto prev_state = this->current_state_;
33  ESP_LOGD(TAG, "Set state to: %s, previous: %s", LOG_STR_ARG(alarm_control_panel_state_to_string(state)),
34  LOG_STR_ARG(alarm_control_panel_state_to_string(prev_state)));
35  this->current_state_ = state;
36  this->state_callback_.call();
37  if (state == ACP_STATE_TRIGGERED) {
38  this->triggered_callback_.call();
39  } else if (state == ACP_STATE_ARMING) {
40  this->arming_callback_.call();
41  } else if (state == ACP_STATE_PENDING) {
42  this->pending_callback_.call();
43  } else if (state == ACP_STATE_ARMED_HOME) {
44  this->armed_home_callback_.call();
45  } else if (state == ACP_STATE_ARMED_NIGHT) {
46  this->armed_night_callback_.call();
47  } else if (state == ACP_STATE_ARMED_AWAY) {
48  this->armed_away_callback_.call();
49  } else if (state == ACP_STATE_DISARMED) {
50  this->disarmed_callback_.call();
51  }
52 
53  if (prev_state == ACP_STATE_TRIGGERED) {
54  this->cleared_callback_.call();
55  }
56  if (state == this->desired_state_) {
57  // only store when in the desired state
58  this->pref_.save(&state);
59  }
60  }
61 }
62 
63 void AlarmControlPanel::add_on_state_callback(std::function<void()> &&callback) {
64  this->state_callback_.add(std::move(callback));
65 }
66 
67 void AlarmControlPanel::add_on_triggered_callback(std::function<void()> &&callback) {
68  this->triggered_callback_.add(std::move(callback));
69 }
70 
71 void AlarmControlPanel::add_on_arming_callback(std::function<void()> &&callback) {
72  this->arming_callback_.add(std::move(callback));
73 }
74 
75 void AlarmControlPanel::add_on_armed_home_callback(std::function<void()> &&callback) {
76  this->armed_home_callback_.add(std::move(callback));
77 }
78 
79 void AlarmControlPanel::add_on_armed_night_callback(std::function<void()> &&callback) {
80  this->armed_night_callback_.add(std::move(callback));
81 }
82 
83 void AlarmControlPanel::add_on_armed_away_callback(std::function<void()> &&callback) {
84  this->armed_away_callback_.add(std::move(callback));
85 }
86 
87 void AlarmControlPanel::add_on_pending_callback(std::function<void()> &&callback) {
88  this->pending_callback_.add(std::move(callback));
89 }
90 
91 void AlarmControlPanel::add_on_disarmed_callback(std::function<void()> &&callback) {
92  this->disarmed_callback_.add(std::move(callback));
93 }
94 
95 void AlarmControlPanel::add_on_cleared_callback(std::function<void()> &&callback) {
96  this->cleared_callback_.add(std::move(callback));
97 }
98 
99 void AlarmControlPanel::add_on_chime_callback(std::function<void()> &&callback) {
100  this->chime_callback_.add(std::move(callback));
101 }
102 
103 void AlarmControlPanel::add_on_ready_callback(std::function<void()> &&callback) {
104  this->ready_callback_.add(std::move(callback));
105 }
106 
108  auto call = this->make_call();
109  call.arm_away();
110  if (code.has_value())
111  call.set_code(code.value());
112  call.perform();
113 }
114 
116  auto call = this->make_call();
117  call.arm_home();
118  if (code.has_value())
119  call.set_code(code.value());
120  call.perform();
121 }
122 
124  auto call = this->make_call();
125  call.arm_night();
126  if (code.has_value())
127  call.set_code(code.value());
128  call.perform();
129 }
130 
132  auto call = this->make_call();
133  call.arm_vacation();
134  if (code.has_value())
135  call.set_code(code.value());
136  call.perform();
137 }
138 
140  auto call = this->make_call();
141  call.arm_custom_bypass();
142  if (code.has_value())
143  call.set_code(code.value());
144  call.perform();
145 }
146 
148  auto call = this->make_call();
149  call.disarm();
150  if (code.has_value())
151  call.set_code(code.value());
152  call.perform();
153 }
154 
155 } // namespace alarm_control_panel
156 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
void add_on_armed_night_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes to armed_night.
void add_on_cleared_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel clears from triggered.
void add_on_triggered_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel chanes to triggered. ...
void arm_vacation(optional< std::string > code=nullopt)
arm the alarm in vacation mode
void add_on_armed_home_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes to armed_home.
void add_on_disarmed_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes to disarmed. ...
void arm_night(optional< std::string > code=nullopt)
arm the alarm in night mode
bool is_state_armed(AlarmControlPanelState state)
void publish_state(AlarmControlPanelState state)
Set the state of the alarm_control_panel.
bool has_value() const
Definition: optional.h:87
AlarmControlPanelCall make_call()
Make a AlarmControlPanelCall.
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void add_on_chime_callback(std::function< void()> &&callback)
Add a callback for when a chime zone goes from closed to open.
void disarm(optional< std::string > code=nullopt)
disarm the alarm
bool save(const T *src)
Definition: preferences.h:21
void arm_home(optional< std::string > code=nullopt)
arm the alarm in home mode
const LogString * alarm_control_panel_state_to_string(AlarmControlPanelState state)
Returns a string representation of the state.
void arm_custom_bypass(optional< std::string > code=nullopt)
arm the alarm in custom bypass mode
void add_on_state_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes.
void arm_away(optional< std::string > code=nullopt)
arm the alarm in away mode
void add_on_pending_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes to pending.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void add_on_armed_away_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes to armed_away.
void add_on_ready_callback(std::function< void()> &&callback)
Add a callback for when a ready state changes.
void add_on_arming_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel chanes to arming.
bool state
Definition: fan.h:34