ESPHome  2024.9.2
mqtt_alarm_control_panel.cpp
Go to the documentation of this file.
2 #include "esphome/core/log.h"
3 
4 #include "mqtt_const.h"
5 
6 #ifdef USE_MQTT
7 #ifdef USE_ALARM_CONTROL_PANEL
8 
9 namespace esphome {
10 namespace mqtt {
11 
12 static const char *const TAG = "mqtt.alarm_control_panel";
13 
14 using namespace esphome::alarm_control_panel;
15 
17  : alarm_control_panel_(alarm_control_panel) {}
19  this->alarm_control_panel_->add_on_state_callback([this]() { this->publish_state(); });
20  this->subscribe(this->get_command_topic_(), [this](const std::string &topic, const std::string &payload) {
21  auto call = this->alarm_control_panel_->make_call();
22  if (strcasecmp(payload.c_str(), "ARM_AWAY") == 0) {
23  call.arm_away();
24  } else if (strcasecmp(payload.c_str(), "ARM_HOME") == 0) {
25  call.arm_home();
26  } else if (strcasecmp(payload.c_str(), "ARM_NIGHT") == 0) {
27  call.arm_night();
28  } else if (strcasecmp(payload.c_str(), "ARM_VACATION") == 0) {
29  call.arm_vacation();
30  } else if (strcasecmp(payload.c_str(), "ARM_CUSTOM_BYPASS") == 0) {
31  call.arm_custom_bypass();
32  } else if (strcasecmp(payload.c_str(), "DISARM") == 0) {
33  call.disarm();
34  } else if (strcasecmp(payload.c_str(), "PENDING") == 0) {
35  call.pending();
36  } else if (strcasecmp(payload.c_str(), "TRIGGERED") == 0) {
37  call.triggered();
38  } else {
39  ESP_LOGW(TAG, "'%s': Received unknown command payload %s", this->friendly_name().c_str(), payload.c_str());
40  }
41  call.perform();
42  });
43 }
44 
46  ESP_LOGCONFIG(TAG, "MQTT alarm_control_panel '%s':", this->alarm_control_panel_->get_name().c_str());
47  LOG_MQTT_COMPONENT(true, true)
48  ESP_LOGCONFIG(TAG, " Supported Features: %" PRIu32, this->alarm_control_panel_->get_supported_features());
49  ESP_LOGCONFIG(TAG, " Requires Code to Disarm: %s", YESNO(this->alarm_control_panel_->get_requires_code()));
50  ESP_LOGCONFIG(TAG, " Requires Code To Arm: %s", YESNO(this->alarm_control_panel_->get_requires_code_to_arm()));
51 }
52 
54  JsonArray supported_features = root.createNestedArray(MQTT_SUPPORTED_FEATURES);
55  const uint32_t acp_supported_features = this->alarm_control_panel_->get_supported_features();
56  if (acp_supported_features & ACP_FEAT_ARM_AWAY) {
57  supported_features.add("arm_away");
58  }
59  if (acp_supported_features & ACP_FEAT_ARM_HOME) {
60  supported_features.add("arm_home");
61  }
62  if (acp_supported_features & ACP_FEAT_ARM_NIGHT) {
63  supported_features.add("arm_night");
64  }
65  if (acp_supported_features & ACP_FEAT_ARM_VACATION) {
66  supported_features.add("arm_vacation");
67  }
68  if (acp_supported_features & ACP_FEAT_ARM_CUSTOM_BYPASS) {
69  supported_features.add("arm_custom_bypass");
70  }
71  if (acp_supported_features & ACP_FEAT_TRIGGER) {
72  supported_features.add("trigger");
73  }
76 }
77 
78 std::string MQTTAlarmControlPanelComponent::component_type() const { return "alarm_control_panel"; }
80 
83  bool success = true;
84  const char *state_s = "";
85  switch (this->alarm_control_panel_->get_state()) {
86  case ACP_STATE_DISARMED:
87  state_s = "disarmed";
88  break;
90  state_s = "armed_home";
91  break;
93  state_s = "armed_away";
94  break;
96  state_s = "armed_night";
97  break;
99  state_s = "armed_vacation";
100  break;
102  state_s = "armed_custom_bypass";
103  break;
104  case ACP_STATE_PENDING:
105  state_s = "pending";
106  break;
107  case ACP_STATE_ARMING:
108  state_s = "arming";
109  break;
110  case ACP_STATE_DISARMING:
111  state_s = "disarming";
112  break;
113  case ACP_STATE_TRIGGERED:
114  state_s = "triggered";
115  break;
116  default:
117  state_s = "unknown";
118  }
119  if (!this->publish(this->get_state_topic_(), state_s))
120  success = false;
121  return success;
122 }
123 
124 } // namespace mqtt
125 } // namespace esphome
126 
127 #endif
128 #endif // USE_MQTT
AlarmControlPanelState get_state() const
Get the state.
alarm_control_panel::AlarmControlPanel * alarm_control_panel_
virtual bool get_requires_code() const =0
Returns if the alarm_control_panel has a code.
MQTTAlarmControlPanelComponent(alarm_control_panel::AlarmControlPanel *alarm_control_panel)
AlarmControlPanelCall make_call()
Make a AlarmControlPanelCall.
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
constexpr const char *const MQTT_CODE_ARM_REQUIRED
Definition: mqtt_const.h:36
constexpr const char *const MQTT_CODE_DISARM_REQUIRED
Definition: mqtt_const.h:37
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
void add_on_state_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes.
Simple Helper struct used for Home Assistant MQTT send_discovery().
constexpr const char * c_str() const
Definition: string_ref.h:68
constexpr const char *const MQTT_SUPPORTED_FEATURES
Definition: mqtt_const.h:225
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
virtual uint32_t get_supported_features() const =0
A numeric representation of the supported features as per HomeAssistant.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
const StringRef & get_name() const
Definition: entity_base.cpp:10
virtual bool get_requires_code_to_arm() const =0
Returns if the alarm_control_panel requires a code to arm.