ESPHome 2025.5.0
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, " 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
78std::string MQTTAlarmControlPanelComponent::component_type() const { return "alarm_control_panel"; }
80
83 const char *state_s;
84 switch (this->alarm_control_panel_->get_state()) {
86 state_s = "disarmed";
87 break;
89 state_s = "armed_home";
90 break;
92 state_s = "armed_away";
93 break;
95 state_s = "armed_night";
96 break;
98 state_s = "armed_vacation";
99 break;
101 state_s = "armed_custom_bypass";
102 break;
104 state_s = "pending";
105 break;
106 case ACP_STATE_ARMING:
107 state_s = "arming";
108 break;
110 state_s = "disarming";
111 break;
113 state_s = "triggered";
114 break;
115 default:
116 state_s = "unknown";
117 }
118 return this->publish(this->get_state_topic_(), state_s);
119}
120
121} // namespace mqtt
122} // namespace esphome
123
124#endif
125#endif // USE_MQTT
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
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().