EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
haTrigger.cpp
Go to the documentation of this file.
1 
9  #include "haTrigger.h"
10 
11 #if SUPPORT_HA_DISCOVERY
12 
13 void HATrigger::setPayload (const char* payload) {
14  if (payload) {
15  (*entityConfig)[ha_payload] = payload;
16  }
17 }
18 
19 /* Discovery JSON template for trigger
20 {
21  "pl":<payload_value>,
22  "type":<type>,
23  "stype":<subtype>
24 }
25 
26 {
27  "automation_type":"trigger",
28  "topic":"<network_name>/<node_name>/data",
29  "payload":<payload_value>,
30  "type":<type>,
31  "subtype":<subtype>,
32  "device":{
33  "name":<node_name>_<type>_<subtype>,
34  "identifiers":<node_name>_<type>_<subtype>
35  }
36 }
37 */
38 
39 size_t HATrigger::getDiscoveryJson (char* buffer, size_t buflen, const char* nodeName, const char* networkName, DynamicJsonDocument* inputJSON) {
40  //DynamicJsonDocument inputJSON (1024);
41  DynamicJsonDocument outputJSON (1024);
42  String type;
43  String subtype;
44 
45  //deserializeMsgPack (inputJSON, msgPack, len);
46 
47  if (!nodeName || !networkName || !inputJSON || !buffer) {
48  DEBUG_WARN ("Whrong parameters");
49  return 0;
50  }
51 
52  outputJSON["automation_type"] = "trigger";
53  outputJSON["topic"] = String (networkName) + "/" + String (nodeName) + "/data";
54 
55  if (inputJSON->containsKey (ha_payload)) {
56  outputJSON["payload"] = (*inputJSON)[ha_payload].as<String> ();
57  }
58  if (inputJSON->containsKey (ha_type)) {
59  if ((*inputJSON)[ha_type].is<int> ()) {
60  type = getTriggerTypeStr ((*inputJSON)[ha_type].as<int> ());
61  outputJSON["type"] = type;
62  } else {
63  type = (*inputJSON)[ha_type].as<String>();
64  outputJSON["type"] = type;
65  }
66  }
67  if (inputJSON->containsKey (ha_subtype)) {
68  if ((*inputJSON)[ha_subtype].is<int> ()) {
69  subtype = getTriggerSubtypeStr ((*inputJSON)[ha_subtype].as<int> ());
70  outputJSON["subtype"] = subtype;
71  } else {
72  subtype = (*inputJSON)[ha_subtype].as<String> ();
73  outputJSON["subtype"] = subtype;
74  }
75  }
76 
77  JsonObject device = outputJSON.createNestedObject ("device");
78  device["name"] = String (nodeName) + (type != "" ? ("_" + type) : "") + (subtype != "" ? ("_" + subtype) : "");
79  device["identifiers"] = device["name"];
80 
81  size_t jsonLen = measureJson (outputJSON);
82 
83  if (jsonLen > buflen) {
84  DEBUG_WARN ("Too small buffer. Required %u Has %u", jsonLen, buflen);
85  return 0;
86  }
87 
88  //buffer[jsonLen - 1] = '\0';
89  serializeJson (outputJSON, buffer, buflen);
90 
91  return jsonLen;
92 }
93 
94 #endif // SUPPORT_HA_DISCOVERY
HATrigger::getTriggerSubtypeStr
static const char * getTriggerSubtypeStr(int subtype)
Returns string that correspond with trigger subtype in ha_triggerSubtype_t format https://www....
Definition: haTrigger.h:158
HATrigger::setPayload
void setPayload(const char *payload)
Optional payload to match the payload being sent over the topic https://www.home-assistant....
Definition: haTrigger.cpp:13
ha_payload
constexpr auto ha_payload
Definition: haEntity.h:37
ha_subtype
constexpr auto ha_subtype
Definition: haEntity.h:50
haTrigger.h
Home Assistant trigger integration.
ha_type
constexpr auto ha_type
Definition: haEntity.h:49
HATrigger::getDiscoveryJson
static size_t getDiscoveryJson(char *buffer, size_t buflen, const char *nodeName, const char *networkName, DynamicJsonDocument *inputJSON)
Allows Gateway to get Home Assistant discovery message using Trigger template.
Definition: haTrigger.cpp:39
HATrigger::getTriggerTypeStr
static const char * getTriggerTypeStr(int type)
Returns string that correspond with trigger type in ha_triggerType_t format https://www....
Definition: haTrigger.h:145