From 79fdc265b7b6500e30b9905c479b28a5af0b4496 Mon Sep 17 00:00:00 2001 From: Hadinger Date: Sun, 19 Jan 2020 22:59:02 +0100 Subject: [PATCH] Minor fixes to Zigbee friendly names --- tasmota/xdrv_23_zigbee_3_devices.ino | 29 ++++++++++++++++++----- tasmota/xdrv_23_zigbee_7_statemachine.ino | 1 - tasmota/xdrv_23_zigbee_8_parsers.ino | 26 ++++---------------- tasmota/xdrv_23_zigbee_9_impl.ino | 4 ++-- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/tasmota/xdrv_23_zigbee_3_devices.ino b/tasmota/xdrv_23_zigbee_3_devices.ino index 97c995aaf..e034868e6 100644 --- a/tasmota/xdrv_23_zigbee_3_devices.ino +++ b/tasmota/xdrv_23_zigbee_3_devices.ino @@ -624,6 +624,16 @@ void Z_Devices::jsonAppend(uint16_t shortaddr, const JsonObject &values) { if (nullptr == device.json) { device.json = &(device.json_buffer->createObject()); } + // Prepend Device, will be removed later if redundant + char sa[8]; + snprintf_P(sa, sizeof(sa), PSTR("0x%04X"), shortaddr); + device.json->set(F(D_JSON_ZIGBEE_DEVICE), sa); + // Prepend Friendly Name if it has one + const String * fname = zigbee_devices.getFriendlyName(shortaddr); + if (fname) { + device.json->set(F(D_JSON_ZIGBEE_NAME), (char*)fname->c_str()); // (char*) forces ArduinoJson to make a copy of the cstring + } + // copy all values from 'values' to 'json' CopyJsonObject(*device.json, values); } @@ -643,13 +653,20 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { const String * fname = zigbee_devices.getFriendlyName(shortaddr); bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname? + // if (use_fname) { + // // we need to add the Device short_addr inside the JSON + // char sa[8]; + // snprintf_P(sa, sizeof(sa), PSTR("0x%04X"), shortaddr); + // json->set(F(D_JSON_ZIGBEE_DEVICE), sa); + // } else if (fname) { + // json->set(F(D_JSON_NAME), (char*) fname); + // } + + // Remove redundant "Name" or "Device" if (use_fname) { - // we need to add the Device short_addr inside the JSON - char sa[8]; - snprintf_P(sa, sizeof(sa), PSTR("0x%04X"), shortaddr); - json->set(F(D_JSON_ZIGBEE_DEVICE), sa); - } else if (fname) { - json->set(F(D_JSON_NAME), (char*) fname); + json->remove(F(D_JSON_ZIGBEE_NAME)); + } else { + json->remove(F(D_JSON_ZIGBEE_DEVICE)); } String msg = ""; diff --git a/tasmota/xdrv_23_zigbee_7_statemachine.ino b/tasmota/xdrv_23_zigbee_7_statemachine.ino index c32634f6b..1e683d059 100644 --- a/tasmota/xdrv_23_zigbee_7_statemachine.ino +++ b/tasmota/xdrv_23_zigbee_7_statemachine.ino @@ -32,7 +32,6 @@ const uint8_t ZIGBEE_STATUS_DEVICE_ANNOUNCE = 30; // Device announces its const uint8_t ZIGBEE_STATUS_NODE_DESC = 31; // Node descriptor const uint8_t ZIGBEE_STATUS_ACTIVE_EP = 32; // Endpoints descriptor const uint8_t ZIGBEE_STATUS_SIMPLE_DESC = 33; // Simple Descriptor (clusters) -const uint8_t ZIGBEE_STATUS_DEVICE_INDICATION = 34; // Device announces its address const uint8_t ZIGBEE_STATUS_CC_VERSION = 50; // Status: CC2530 ZNP Version const uint8_t ZIGBEE_STATUS_CC_INFO = 51; // Status: CC2530 Device Configuration const uint8_t ZIGBEE_STATUS_UNSUPPORTED_VERSION = 98; // Unsupported ZNP version diff --git a/tasmota/xdrv_23_zigbee_8_parsers.ino b/tasmota/xdrv_23_zigbee_8_parsers.ino index 90d272a00..26185fd6c 100644 --- a/tasmota/xdrv_23_zigbee_8_parsers.ino +++ b/tasmota/xdrv_23_zigbee_8_parsers.ino @@ -432,16 +432,7 @@ int32_t Z_ReceiveAfIncomingMessage(int32_t res, const class SBuffer &buf) { snprintf_P(shortaddr, sizeof(shortaddr), PSTR("0x%04X"), srcaddr); DynamicJsonBuffer jsonBuffer; - JsonObject& json_root = jsonBuffer.createObject(); - - JsonObject& json1 = json_root.createNestedObject(F(D_CMND_ZIGBEE_RECEIVED)); - - const String * fname = zigbee_devices.getFriendlyName(srcaddr); - bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname? - JsonObject& json = json1.createNestedObject(use_fname ? fname->c_str() : shortaddr); - if (use_fname) { - json[F(D_JSON_ZIGBEE_DEVICE)] = shortaddr; - } + JsonObject& json = jsonBuffer.createObject(); if ( (!zcl_received.isClusterSpecificCommand()) && (ZCL_REPORT_ATTRIBUTES == zcl_received.getCmdId())) { zcl_received.parseRawAttributes(json); @@ -453,13 +444,8 @@ int32_t Z_ReceiveAfIncomingMessage(int32_t res, const class SBuffer &buf) { } String msg(""); msg.reserve(100); - json_root.printTo(msg); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("ZigbeeZCLRawReceived: %s"), msg.c_str()); - - // Add friendly name - if ((!use_fname) && (fname)) { - json[F(D_JSON_ZIGBEE_NAME)] = (char*)fname->c_str(); // (char*) will force a copy of the string - } + json.printTo(msg); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_ZIGBEE "ZigbeeZCLRawReceived: {\"0x%04X\":%s}"), srcaddr, msg.c_str()); zcl_received.postProcessAttributes(srcaddr, json); // Add linkquality @@ -476,11 +462,7 @@ int32_t Z_ReceiveAfIncomingMessage(int32_t res, const class SBuffer &buf) { } } else { // Publish immediately - msg = ""; - json_root.printTo(msg); - Response_P(PSTR("%s"), msg.c_str()); - MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR)); - XdrvRulesProcess(); + zigbee_devices.jsonPublishNow(srcaddr, json); } return -1; } diff --git a/tasmota/xdrv_23_zigbee_9_impl.ino b/tasmota/xdrv_23_zigbee_9_impl.ino index 4464ab525..da73f9db7 100644 --- a/tasmota/xdrv_23_zigbee_9_impl.ino +++ b/tasmota/xdrv_23_zigbee_9_impl.ino @@ -575,10 +575,10 @@ void CmndZigbeeName(void) { if (p == nullptr) { const String * friendlyName = zigbee_devices.getFriendlyName(shortaddr); - Response_P(PSTR("{\"0x%04X\":{\"name\":\"%s\"}}"), shortaddr, friendlyName ? friendlyName->c_str() : ""); + Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_NAME "\":\"%s\"}}"), shortaddr, friendlyName ? friendlyName->c_str() : ""); } else { zigbee_devices.setFriendlyName(shortaddr, p); - Response_P(PSTR("{\"0x%04X\":{\"name\":\"%s\"}}"), shortaddr, p); + Response_P(PSTR("{\"0x%04X\":{\"" D_JSON_ZIGBEE_NAME "\":\"%s\"}}"), shortaddr, p); } }