From c6566ecdaded685a7797f7c0b3bc1bea822a2f97 Mon Sep 17 00:00:00 2001 From: Jimmy Wennlund Date: Fri, 1 Jan 2021 21:33:18 +0100 Subject: [PATCH 1/3] Add option to move ZbReceived from json into the subtopic of the MQTT message. This will make parsing simpler in homeassistant --- tasmota/settings.h | 2 +- tasmota/xdrv_23_zigbee_2a_devices_impl.ino | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 4842103b42bb..0a18d427651e 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -143,7 +143,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t mi32_enable : 1; // bit 1 (v9.1.0.1) - SetOption115 - (ESP32 BLE) Enable ESP32 MI32 BLE (1) uint32_t zb_disable_autoquery : 1; // bit 2 (v9.1.0.1) - SetOption116 - (Zigbee) Disable auto-query of zigbee lights and devices (1) uint32_t fade_fixed_duration : 1; // bit 3 (v9.1.0.2) - SetOption117 - (Light) run fading at fixed duration instead of fixed slew rate - uint32_t spare04 : 1; // bit 4 + uint32_t zbreceived_as_subtopic : 1; // bit 4 (v9.2.0.3) - SetOption118 - (Zigbee) Move ZbReceived form JSON message and into the subtopic replacing "SENSOR" default uint32_t spare05 : 1; // bit 5 uint32_t spare06 : 1; // bit 6 uint32_t spare07 : 1; // bit 7 diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index 0a97a9938fe5..1c788cde8f14 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -511,7 +511,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l TasmotaGlobal.mqtt_data[0] = 0; // clear string // Do we prefix with `ZbReceived`? - if (!Settings.flag4.remove_zbreceived) { + if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zbreceived_as_subtopic) { Response_P(PSTR("{\"%s\":"), json_prefix); } // What key do we use, shortaddr or name? @@ -529,7 +529,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l // Add all other attributes ResponseAppend_P(PSTR("%s}}"), attr_list.toString(false).c_str()); - if (!Settings.flag4.remove_zbreceived) { + if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zbreceived_as_subtopic) { ResponseAppend_P(PSTR("}")); } @@ -545,7 +545,10 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l snprintf_P(subtopic, sizeof(subtopic), PSTR("%s/%04X"), TasmotaGlobal.mqtt_topic, shortaddr); } char stopic[TOPSZ]; - GetTopic_P(stopic, TELE, subtopic, D_RSLT_SENSOR); + if (Settings.flag5.zbreceived_as_subtopic) + GetTopic_P(stopic, TELE, subtopic, json_prefix); + else + GetTopic_P(stopic, TELE, subtopic, D_RSLT_SENSOR); MqttPublish(stopic, Settings.flag.mqtt_sensor_retain); } else { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); From 11a76f0fbba767dfe46fccd8f6a65e24324ddd75 Mon Sep 17 00:00:00 2001 From: Jimmy Wennlund Date: Fri, 1 Jan 2021 21:58:45 +0100 Subject: [PATCH 2/3] Follow the setting naming conv. --- tasmota/settings.h | 2 +- tasmota/xdrv_23_zigbee_2a_devices_impl.ino | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 0a18d427651e..9016b01921f5 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -143,7 +143,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t mi32_enable : 1; // bit 1 (v9.1.0.1) - SetOption115 - (ESP32 BLE) Enable ESP32 MI32 BLE (1) uint32_t zb_disable_autoquery : 1; // bit 2 (v9.1.0.1) - SetOption116 - (Zigbee) Disable auto-query of zigbee lights and devices (1) uint32_t fade_fixed_duration : 1; // bit 3 (v9.1.0.2) - SetOption117 - (Light) run fading at fixed duration instead of fixed slew rate - uint32_t zbreceived_as_subtopic : 1; // bit 4 (v9.2.0.3) - SetOption118 - (Zigbee) Move ZbReceived form JSON message and into the subtopic replacing "SENSOR" default + uint32_t zb_received_as_subtopic : 1; // bit 4 (v9.2.0.3) - SetOption118 - (Zigbee) Move ZbReceived form JSON message and into the subtopic replacing "SENSOR" default uint32_t spare05 : 1; // bit 5 uint32_t spare06 : 1; // bit 6 uint32_t spare07 : 1; // bit 7 diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index 1c788cde8f14..0f175e688473 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -511,7 +511,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l TasmotaGlobal.mqtt_data[0] = 0; // clear string // Do we prefix with `ZbReceived`? - if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zbreceived_as_subtopic) { + if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) { Response_P(PSTR("{\"%s\":"), json_prefix); } // What key do we use, shortaddr or name? @@ -529,7 +529,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l // Add all other attributes ResponseAppend_P(PSTR("%s}}"), attr_list.toString(false).c_str()); - if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zbreceived_as_subtopic) { + if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) { ResponseAppend_P(PSTR("}")); } @@ -545,7 +545,7 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l snprintf_P(subtopic, sizeof(subtopic), PSTR("%s/%04X"), TasmotaGlobal.mqtt_topic, shortaddr); } char stopic[TOPSZ]; - if (Settings.flag5.zbreceived_as_subtopic) + if (Settings.flag5.zb_received_as_subtopic) GetTopic_P(stopic, TELE, subtopic, json_prefix); else GetTopic_P(stopic, TELE, subtopic, D_RSLT_SENSOR); From 78814c8bf03ff21f9dd40d263442aa170d567e5e Mon Sep 17 00:00:00 2001 From: Jimmy Wennlund Date: Fri, 1 Jan 2021 22:03:48 +0100 Subject: [PATCH 3/3] Zigbee: Remove the device addr from json payload, can be used with zb_topic_fname where the addr is already known from the topic --- tasmota/settings.h | 2 +- tasmota/xdrv_23_zigbee_2a_devices_impl.ino | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tasmota/settings.h b/tasmota/settings.h index 9016b01921f5..81a62c8350ff 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -144,7 +144,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t zb_disable_autoquery : 1; // bit 2 (v9.1.0.1) - SetOption116 - (Zigbee) Disable auto-query of zigbee lights and devices (1) uint32_t fade_fixed_duration : 1; // bit 3 (v9.1.0.2) - SetOption117 - (Light) run fading at fixed duration instead of fixed slew rate uint32_t zb_received_as_subtopic : 1; // bit 4 (v9.2.0.3) - SetOption118 - (Zigbee) Move ZbReceived form JSON message and into the subtopic replacing "SENSOR" default - uint32_t spare05 : 1; // bit 5 + uint32_t zb_omit_json_addr : 1; // bit 5 (v9.2.0.3) - SetOption119 - (Zigbee) Remove the device addr from json payload, can be used with zb_topic_fname where the addr is already known from the topic uint32_t spare06 : 1; // bit 6 uint32_t spare07 : 1; // bit 7 uint32_t spare08 : 1; // bit 8 diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index 0f175e688473..80a69441c7d8 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -515,11 +515,15 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l Response_P(PSTR("{\"%s\":"), json_prefix); } // What key do we use, shortaddr or name? - if (use_fname) { - Response_P(PSTR("%s{\"%s\":{"), TasmotaGlobal.mqtt_data, friendlyName); - } else { - Response_P(PSTR("%s{\"0x%04X\":{"), TasmotaGlobal.mqtt_data, shortaddr); + if (!Settings.flag5.zb_omit_json_addr) { + if (use_fname) { + Response_P(PSTR("%s{\"%s\":"), TasmotaGlobal.mqtt_data, friendlyName); + } else { + Response_P(PSTR("%s{\"0x%04X\":"), TasmotaGlobal.mqtt_data, shortaddr); + } } + ResponseAppend_P(PSTR("{")); + // Add "Device":"0x...." ResponseAppend_P(PSTR("\"" D_JSON_ZIGBEE_DEVICE "\":\"0x%04X\","), shortaddr); // Add "Name":"xxx" if name is present @@ -527,7 +531,11 @@ void Z_Device::jsonPublishAttrList(const char * json_prefix, const Z_attribute_l ResponseAppend_P(PSTR("\"" D_JSON_ZIGBEE_NAME "\":\"%s\","), EscapeJSONString(friendlyName).c_str()); } // Add all other attributes - ResponseAppend_P(PSTR("%s}}"), attr_list.toString(false).c_str()); + ResponseAppend_P(PSTR("%s}"), attr_list.toString(false).c_str()); + + if (!Settings.flag5.zb_omit_json_addr) { + ResponseAppend_P(PSTR("}")); + } if (!Settings.flag4.remove_zbreceived && !Settings.flag5.zb_received_as_subtopic) { ResponseAppend_P(PSTR("}"));