From 3047ae8f589e7149fa22e025b7232a287a1d6b94 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Thu, 24 Oct 2019 18:29:06 -0300 Subject: [PATCH] Update xdrv_12_home_assistant.ino --- sonoff/xdrv_12_home_assistant.ino | 43 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/sonoff/xdrv_12_home_assistant.ino b/sonoff/xdrv_12_home_assistant.ino index b90a52fee..e27f4a4d7 100644 --- a/sonoff/xdrv_12_home_assistant.ino +++ b/sonoff/xdrv_12_home_assistant.ino @@ -28,17 +28,15 @@ const char HASS_DISCOVER_RELAY[] PROGMEM = "\"val_tpl\":\"{{value_json.%s}}\"," // POWER2 "\"pl_off\":\"%s\"," // OFF "\"pl_on\":\"%s\"," // ON -// "\"optimistic\":\"false\"," // false is Hass default when state_topic is set "\"avty_t\":\"%s\"," // tele/dualr2/LWT "\"pl_avail\":\"" D_ONLINE "\"," // Online "\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline const char HASS_DISCOVER_BUTTON_SWITCH[] PROGMEM = "{\"name\":\"%s\"," // dualr2 1 BTN - "\"stat_t\":\"%s\"," // cmnd/dualr2/POWER (implies "\"optimistic\":\"false\",") -// "\"value_template\":\"{{value_json.%s}}\"," // POWER2 - "\"pl_on\":\"%s\"," // TOGGLE -// "\"optimistic\":\"false\"," // false is Hass default when state_topic is set + "\"stat_t\":\"%s\"," // stat/RESULT/ (implies "\"optimistic\":\"false\",") + "\"value_template\":\"{{value_json.%s}}\"," // BUTTON1 + "\"pl_on\":\"%s\"," // ON "\"avty_t\":\"%s\"," // tele/dualr2/LWT "\"pl_avail\":\"" D_ONLINE "\"," // Online "\"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 }} "\"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 = ",\"unit_of_meas\":\"%%\"," // % "\"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 "\"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\""; + const char HASS_DISCOVER_SENSOR_VOLTAGE[] PROGMEM = ",\"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 }} "\"dev_cla\":\"power\""; + const char HASS_DISCOVER_SENSOR_AMPERE[] PROGMEM = ",\"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 }} @@ -127,7 +123,7 @@ const char HASS_DISCOVER_SENSOR_AMPERE[] PROGMEM = //ILLUMINANCE const char HASS_DISCOVER_SENSOR_ILLUMINANCE[] PROGMEM = ",\"unit_of_meas\":\"LX\"," // LX by default - "\"val_tpl\":\"{{value_json['%s'].Illuminance}}\"," // "ANALOG":{"Illuminance":34}} + "\"val_tpl\":\"{{value_json['%s'].Illuminance}}\"," // "ANALOG":{"Illuminance":34}} "\"dev_cla\":\"illuminance\""; // illuminance const char HASS_DISCOVER_SENSOR_ANY[] PROGMEM = @@ -135,8 +131,9 @@ const char HASS_DISCOVER_SENSOR_ANY[] PROGMEM = const char HASS_DISCOVER_SENSOR_HASS_STATUS[] PROGMEM = ",\"json_attributes_topic\":\"%s\"," - "\"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 }} + "\"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 }} + "\"ic\":\"mdi:information-outline\""; const char HASS_DISCOVER_DEVICE_INFO[] PROGMEM = ",\"uniq_id\":\"%s\"," @@ -319,20 +316,20 @@ void HAssAnnounceButtonSwitch(uint8_t device, char* topic, uint8_t present, uint char prefix[TOPSZ]; char *state_topic = stemp1; 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?"SW":"BTN", device+1); - } else { - snprintf_P(name, sizeof(name), PSTR("%s %s"), Settings.friendlyname[device], key?"SW":"BTN"); - } + snprintf_P(name, sizeof(name), PSTR("%s %s%d"), Settings.friendlyname[0], key?"Switch":"Button", device+1); 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 - 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); FindPrefix(state_topic, availability_topic, prefix); + Shorten(&state_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()); if (strlen(prefix) > 0 ) TryResponseAppend_P(HASS_DISCOVER_TOPIC_PREFIX, prefix); if (toggle) TryResponseAppend_P(HASS_DISCOVER_BUTTON_SWITCH_TOGGLE); @@ -350,7 +347,7 @@ void HAssAnnounceSwitches(void) // Send info about buttons char *tmp = Settings.switch_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++) { uint8_t switch_present = 0; uint8_t toggle = 1; @@ -379,7 +376,7 @@ void HAssAnnounceButtons(void) // Send info about buttons char *tmp = Settings.button_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++) { uint8_t button_present = 0; uint8_t toggle = 1; @@ -623,8 +620,8 @@ void HAssAnyKey(void) snprintf_P(scommand, sizeof(scommand), PSTR("%s%d"), (key) ? "SWITCH" : "BUTTON", device); char stopic[TOPSZ]; 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))); - MqttPublish(stopic); + Response_P(S_JSON_COMMAND_SVALUE, scommand, GetStateText(state)); + MqttPublish(stopic); } /*********************************************************************************************\