Merge pull request #6737 from effelle/development

Update xdrv_12_home_assistant.ino
This commit is contained in:
Theo Arends 2019-10-25 08:50:32 +02:00 committed by GitHub
commit 3b57806452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 23 deletions

View File

@ -28,17 +28,15 @@ const char HASS_DISCOVER_RELAY[] PROGMEM =
"\"val_tpl\":\"{{value_json.%s}}\"," // POWER2 "\"val_tpl\":\"{{value_json.%s}}\"," // POWER2
"\"pl_off\":\"%s\"," // OFF "\"pl_off\":\"%s\"," // OFF
"\"pl_on\":\"%s\"," // ON "\"pl_on\":\"%s\"," // ON
// "\"optimistic\":\"false\"," // false is Hass default when state_topic is set
"\"avty_t\":\"%s\"," // tele/dualr2/LWT "\"avty_t\":\"%s\"," // tele/dualr2/LWT
"\"pl_avail\":\"" D_ONLINE "\"," // Online "\"pl_avail\":\"" D_ONLINE "\"," // Online
"\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline "\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline
const char HASS_DISCOVER_BUTTON_SWITCH[] PROGMEM = const char HASS_DISCOVER_BUTTON_SWITCH[] PROGMEM =
"{\"name\":\"%s\"," // dualr2 1 BTN "{\"name\":\"%s\"," // dualr2 1 BTN
"\"stat_t\":\"%s\"," // cmnd/dualr2/POWER (implies "\"optimistic\":\"false\",") "\"stat_t\":\"%s\"," // stat/RESULT/ (implies "\"optimistic\":\"false\",")
// "\"value_template\":\"{{value_json.%s}}\"," // POWER2 "\"value_template\":\"{{value_json.%s}}\"," // BUTTON1
"\"pl_on\":\"%s\"," // TOGGLE "\"pl_on\":\"%s\"," // ON
// "\"optimistic\":\"false\"," // false is Hass default when state_topic is set
"\"avty_t\":\"%s\"," // tele/dualr2/LWT "\"avty_t\":\"%s\"," // tele/dualr2/LWT
"\"pl_avail\":\"" D_ONLINE "\"," // Online "\"pl_avail\":\"" D_ONLINE "\"," // Online
"\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline "\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline
@ -91,10 +89,6 @@ const char HASS_DISCOVER_SENSOR_TEMP[] PROGMEM =
"\"val_tpl\":\"{{value_json['%s'].Temperature}}\"," // "SI7021-14":{"Temperature":null,"Humidity":null} -> {{ value_json['SI7021-14'].Temperature }} "\"val_tpl\":\"{{value_json['%s'].Temperature}}\"," // "SI7021-14":{"Temperature":null,"Humidity":null} -> {{ value_json['SI7021-14'].Temperature }}
"\"dev_cla\":\"temperature\""; // temperature "\"dev_cla\":\"temperature\""; // temperature
const char HASS_DISCOVER_DS18X20_MULTI[] PROGMEM =
",\"json_attributes_topic\":\"%s\","
"\"json_attributes_template\":\"{{{'Id':value_json['%s'].Id}|tojson}}\""; // Add DS18X20 Id as information field
const char HASS_DISCOVER_SENSOR_HUM[] PROGMEM = const char HASS_DISCOVER_SENSOR_HUM[] PROGMEM =
",\"unit_of_meas\":\"%%\"," // % ",\"unit_of_meas\":\"%%\"," // %
"\"val_tpl\":\"{{value_json['%s'].Humidity}}\"," // "SI7021-14":{"Temperature":null,"Humidity":null} -> {{ value_json['SI7021-14'].Humidity }} "\"val_tpl\":\"{{value_json['%s'].Humidity}}\"," // "SI7021-14":{"Temperature":null,"Humidity":null} -> {{ value_json['SI7021-14'].Humidity }}
@ -115,10 +109,12 @@ const char HASS_DISCOVER_SENSOR_WATT[] PROGMEM =
",\"unit_of_meas\":\"W\"," // W ",\"unit_of_meas\":\"W\"," // W
"\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].POWER }} "\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].POWER }}
"\"dev_cla\":\"power\""; "\"dev_cla\":\"power\"";
const char HASS_DISCOVER_SENSOR_VOLTAGE[] PROGMEM = const char HASS_DISCOVER_SENSOR_VOLTAGE[] PROGMEM =
",\"unit_of_meas\":\"V\"," // V ",\"unit_of_meas\":\"V\"," // V
"\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].Voltage }} "\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].Voltage }}
"\"dev_cla\":\"power\""; "\"dev_cla\":\"power\"";
const char HASS_DISCOVER_SENSOR_AMPERE[] PROGMEM = const char HASS_DISCOVER_SENSOR_AMPERE[] PROGMEM =
",\"unit_of_meas\":\"A\"," // A ",\"unit_of_meas\":\"A\"," // A
"\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].Current }} "\"val_tpl\":\"{{value_json['%s'].%s}}\"," // "ENERGY":{"TotalStartTime":null,"Total":null,"Yesterday":null,"Today":null,"Power":null,"ApparentPower":null,"ReactivePower":null,"Factor":null,"Voltage":null,"Current":null} -> {{ value_json['ENERGY'].Current }}
@ -136,7 +132,8 @@ const char HASS_DISCOVER_SENSOR_ANY[] PROGMEM =
const char HASS_DISCOVER_SENSOR_HASS_STATUS[] PROGMEM = const char HASS_DISCOVER_SENSOR_HASS_STATUS[] PROGMEM =
",\"json_attributes_topic\":\"%s\"," ",\"json_attributes_topic\":\"%s\","
"\"unit_of_meas\":\" \"," // " " As unit of measurement to get a value graph in Hass "\"unit_of_meas\":\" \"," // " " As unit of measurement to get a value graph in Hass
"\"val_tpl\":\"{{value_json['" D_JSON_RSSI "']}}\""; // "COUNTER":{"C1":0} -> {{ value_json['COUNTER'].C1 }} "\"val_tpl\":\"{{value_json['" D_JSON_RSSI "']}}\"," // "COUNTER":{"C1":0} -> {{ value_json['COUNTER'].C1 }}
"\"ic\":\"mdi:information-outline\"";
const char HASS_DISCOVER_DEVICE_INFO[] PROGMEM = const char HASS_DISCOVER_DEVICE_INFO[] PROGMEM =
",\"uniq_id\":\"%s\"," ",\"uniq_id\":\"%s\","
@ -319,20 +316,20 @@ void HAssAnnounceButtonSwitch(uint8_t device, char* topic, uint8_t present, uint
char prefix[TOPSZ]; char prefix[TOPSZ];
char *state_topic = stemp1; char *state_topic = stemp1;
char *availability_topic = stemp2; char *availability_topic = stemp2;
char jsoname[8];
if (device+1 > MAX_FRIENDLYNAMES) { snprintf_P(name, sizeof(name), PSTR("%s %s%d"), Settings.friendlyname[0], key?"Switch":"Button", device+1);
snprintf_P(name, sizeof(name), PSTR("%s %s %d"), Settings.friendlyname[0], key?"SW":"BTN", device+1);
} else {
snprintf_P(name, sizeof(name), PSTR("%s %s"), Settings.friendlyname[device], key?"SW":"BTN");
}
GetPowerDevice(value_template, device+1, sizeof(value_template), GetPowerDevice(value_template, device+1, sizeof(value_template),
key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by Settings.flag.device_index_enable key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by Settings.flag.device_index_enable
GetTopic_P(state_topic, CMND, topic, value_template); // State of button is sent as CMND TOGGLE, state of switch is sent as ON/OFF //GetTopic_P(state_topic, CMND, topic, value_template); // State of button is sent as CMND TOGGLE, state of switch is sent as ON/OFF
GetTopic_P(state_topic, STAT, mqtt_topic, PSTR(D_RSLT_RESULT));
GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT); GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT);
FindPrefix(state_topic, availability_topic, prefix); FindPrefix(state_topic, availability_topic, prefix);
Shorten(&state_topic, prefix); Shorten(&state_topic, prefix);
Shorten(&availability_topic, prefix); Shorten(&availability_topic, prefix);
Response_P(HASS_DISCOVER_BUTTON_SWITCH, name, state_topic, Settings.state_text[toggle?2:1], availability_topic); snprintf_P(jsoname, sizeof(jsoname), PSTR("%s%d"), key?"SWITCH":"BUTTON", device+1);
Response_P(HASS_DISCOVER_BUTTON_SWITCH, name, state_topic, jsoname, Settings.state_text[toggle?2:1], availability_topic);
TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId(), WiFi.macAddress().c_str()); TryResponseAppend_P(HASS_DISCOVER_DEVICE_INFO_SHORT, unique_id, ESP.getChipId(), WiFi.macAddress().c_str());
if (strlen(prefix) > 0 ) TryResponseAppend_P(HASS_DISCOVER_TOPIC_PREFIX, prefix); if (strlen(prefix) > 0 ) TryResponseAppend_P(HASS_DISCOVER_TOPIC_PREFIX, prefix);
if (toggle) TryResponseAppend_P(HASS_DISCOVER_BUTTON_SWITCH_TOGGLE); if (toggle) TryResponseAppend_P(HASS_DISCOVER_BUTTON_SWITCH_TOGGLE);
@ -350,7 +347,7 @@ void HAssAnnounceSwitches(void)
// Send info about buttons // Send info about buttons
char *tmp = Settings.switch_topic; char *tmp = Settings.switch_topic;
Format(sw_topic, tmp, sizeof(sw_topic)); Format(sw_topic, tmp, sizeof(sw_topic));
if ((strlen(sw_topic) != 0) && strcmp(sw_topic, "0")) { if (!strcmp_P(sw_topic, "0") || strlen(sw_topic) == 0 ) {
for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++) { for (uint32_t switch_index = 0; switch_index < MAX_SWITCHES; switch_index++) {
uint8_t switch_present = 0; uint8_t switch_present = 0;
uint8_t toggle = 1; uint8_t toggle = 1;
@ -379,7 +376,7 @@ void HAssAnnounceButtons(void)
// Send info about buttons // Send info about buttons
char *tmp = Settings.button_topic; char *tmp = Settings.button_topic;
Format(key_topic, tmp, sizeof(key_topic)); Format(key_topic, tmp, sizeof(key_topic));
if ((strlen(key_topic) != 0) && strcmp(key_topic, "0")) { if (!strcmp_P(key_topic, "0") || strlen(key_topic) == 0 ) {
for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) { for (uint32_t button_index = 0; button_index < MAX_KEYS; button_index++) {
uint8_t button_present = 0; uint8_t button_present = 0;
uint8_t toggle = 1; uint8_t toggle = 1;
@ -623,7 +620,7 @@ void HAssAnyKey(void)
snprintf_P(scommand, sizeof(scommand), PSTR("%s%d"), (key) ? "SWITCH" : "BUTTON", device); snprintf_P(scommand, sizeof(scommand), PSTR("%s%d"), (key) ? "SWITCH" : "BUTTON", device);
char stopic[TOPSZ]; char stopic[TOPSZ];
GetTopic_P(stopic, STAT, mqtt_topic, (Settings.flag.mqtt_response) ? scommand : S_RSLT_RESULT); GetTopic_P(stopic, STAT, mqtt_topic, (Settings.flag.mqtt_response) ? scommand : S_RSLT_RESULT);
Response_P(S_JSON_COMMAND_SVALUE, scommand, GetStateText(bitRead(state, device -1))); Response_P(S_JSON_COMMAND_SVALUE, scommand, GetStateText(state));
MqttPublish(stopic); MqttPublish(stopic);
} }