ESPHome 2025.6.3
Loading...
Searching...
No Matches
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
9namespace esphome {
10namespace mqtt {
11
12static const char *const TAG = "mqtt.alarm_control_panel";
13
14using 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,
49 " Supported Features: %" PRIu32 "\n"
50 " Requires Code to Disarm: %s\n"
51 " Requires Code To Arm: %s",
55}
56
58 JsonArray supported_features = root.createNestedArray(MQTT_SUPPORTED_FEATURES);
59 const uint32_t acp_supported_features = this->alarm_control_panel_->get_supported_features();
60 if (acp_supported_features & ACP_FEAT_ARM_AWAY) {
61 supported_features.add("arm_away");
62 }
63 if (acp_supported_features & ACP_FEAT_ARM_HOME) {
64 supported_features.add("arm_home");
65 }
66 if (acp_supported_features & ACP_FEAT_ARM_NIGHT) {
67 supported_features.add("arm_night");
68 }
69 if (acp_supported_features & ACP_FEAT_ARM_VACATION) {
70 supported_features.add("arm_vacation");
71 }
72 if (acp_supported_features & ACP_FEAT_ARM_CUSTOM_BYPASS) {
73 supported_features.add("arm_custom_bypass");
74 }
75 if (acp_supported_features & ACP_FEAT_TRIGGER) {
76 supported_features.add("trigger");
77 }
80}
81
82std::string MQTTAlarmControlPanelComponent::component_type() const { return "alarm_control_panel"; }
84
87 const char *state_s;
88 switch (this->alarm_control_panel_->get_state()) {
90 state_s = "disarmed";
91 break;
93 state_s = "armed_home";
94 break;
96 state_s = "armed_away";
97 break;
99 state_s = "armed_night";
100 break;
102 state_s = "armed_vacation";
103 break;
105 state_s = "armed_custom_bypass";
106 break;
108 state_s = "pending";
109 break;
110 case ACP_STATE_ARMING:
111 state_s = "arming";
112 break;
114 state_s = "disarming";
115 break;
117 state_s = "triggered";
118 break;
119 default:
120 state_s = "unknown";
121 }
122 return this->publish(this->get_state_topic_(), state_s);
123}
124
125} // namespace mqtt
126} // namespace esphome
127
128#endif
129#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
void add_on_state_callback(std::function< void()> &&callback)
Add a callback for when the state of the alarm_control_panel changes.
virtual uint32_t get_supported_features() const =0
A numeric representation of the supported features as per HomeAssistant.
AlarmControlPanelState get_state() const
Get the state.
virtual bool get_requires_code_to_arm() const =0
Returns if the alarm_control_panel requires a code to arm.
virtual bool get_requires_code() const =0
Returns if the alarm_control_panel has a code.
AlarmControlPanelCall make_call()
Make a AlarmControlPanelCall.
MQTTAlarmControlPanelComponent(alarm_control_panel::AlarmControlPanel *alarm_control_panel)
void send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) override
alarm_control_panel::AlarmControlPanel * alarm_control_panel_
bool publish(const std::string &topic, const std::string &payload)
Send a MQTT message.
virtual std::string friendly_name() const
Get the friendly name of this MQTT component.
std::string get_state_topic_() const
Get the MQTT topic that new states will be shared to.
std::string get_command_topic_() const
Get the MQTT topic for listening to commands.
void subscribe(const std::string &topic, mqtt_callback_t callback, uint8_t qos=0)
Subscribe to a MQTT topic.
constexpr const char *const MQTT_CODE_DISARM_REQUIRED
Definition mqtt_const.h:37
constexpr const char *const MQTT_SUPPORTED_FEATURES
Definition mqtt_const.h:229
constexpr const char *const MQTT_CODE_ARM_REQUIRED
Definition mqtt_const.h:36
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Simple Helper struct used for Home Assistant MQTT send_discovery().