From fa5370c2cd3ac954e065dcb0412e6007ba1b4556 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Mon, 15 Aug 2022 01:17:03 +0000 Subject: [PATCH 1/3] Generate better uniqued id, fixed payload --- src/httpserver/http_fns.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 17a85cd03..f017198d8 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1240,6 +1240,19 @@ int http_fn_cfg_quick(http_request_t *request) { return 0; } +//Generate HomeAssistant unique id based +char *build_hass_unique_id(char *type, int index){ + //https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements mentions that mac can be used for + //unique_id and I would think that longDeviceName should contain that. + + //e.g. longDeviceName_relay_1 + int length = strlen(g_cfg.longDeviceName) + strlen(type)+ 1 + 3; + char *unique_id = (char*)malloc (length * sizeof (char)); + + sprintf(unique_id,"%s_%s_%d",g_cfg.longDeviceName,type,index); + return unique_id; +} + int http_fn_cfg_ha(http_request_t *request) { int relayCount = 0; int pwmCount = 0; @@ -1285,13 +1298,13 @@ int http_fn_cfg_ha(http_request_t *request) { switchAdded=1; } - hprintf128(request," - unique_id: \"%s %i\"\n",baseName,i); + hprintf128(request," - unique_id: \"%s\"\n",build_hass_unique_id("relay",i)); hprintf128(request," name: \"%s %i\"\n",baseName,i); hprintf128(request," state_topic: \"%s/%i/get\"\n",baseName,i); hprintf128(request," command_topic: \"%s/%i/set\"\n",baseName,i); poststr(request, " qos: 1\n"); - poststr(request, " payload_on: 0\n"); - poststr(request, " payload_off: 1\n"); + poststr(request, " payload_on: 1\n"); + poststr(request, " payload_off: 0\n"); poststr(request, " retain: true\n"); hprintf128(request," availability:\n"); hprintf128(request," - topic: \"%s/connected\"\n",baseName); @@ -1312,7 +1325,7 @@ int http_fn_cfg_ha(http_request_t *request) { lightAdded=1; } - hprintf128(request," - unique_id: \"%s %i\"\n",baseName,i); + hprintf128(request," - unique_id: \"%s\"\n",build_hass_unique_id("light",i)); hprintf128(request," name: \"%s %i\"\n",baseName,i); hprintf128(request," state_topic: \"%s/%i/get\"\n",baseName,i); hprintf128(request," command_topic: \"%s/%i/set\"\n",baseName,i); From 54a9d0603b02d19d9a9df6aa01a33f2bacfd8bc9 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Tue, 16 Aug 2022 23:02:45 +0000 Subject: [PATCH 2/3] Not using malloc --- src/httpserver/http_fns.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index f017198d8..4ace514b3 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1246,8 +1246,7 @@ char *build_hass_unique_id(char *type, int index){ //unique_id and I would think that longDeviceName should contain that. //e.g. longDeviceName_relay_1 - int length = strlen(g_cfg.longDeviceName) + strlen(type)+ 1 + 3; - char *unique_id = (char*)malloc (length * sizeof (char)); + char unique_id[128]; //64 for longDeviceName, 10 for type,3 for index .. 128 would be sufficient sprintf(unique_id,"%s_%s_%d",g_cfg.longDeviceName,type,index); return unique_id; From 726e8fb4912ff0c8adeeeabf876dc7896b03a598 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Wed, 17 Aug 2022 10:06:03 +0000 Subject: [PATCH 3/3] Using buffer correctly --- src/httpserver/http_fns.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 325e6d836..ccc7fc737 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1241,16 +1241,18 @@ int http_fn_cfg_quick(http_request_t *request) { return 0; } -//Generate HomeAssistant unique id based -char *build_hass_unique_id(char *type, int index){ +/* + * Generates HomeAssistant unique id + * @param outBuffer Output buffer (128 char) + * @param type Entity type (relay, light) + * @param index Entity index + */ +void build_hass_unique_id(char *outBuffer, char *type, int index){ //https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements mentions that mac can be used for //unique_id and I would think that longDeviceName should contain that. //e.g. longDeviceName_relay_1 - char unique_id[128]; //64 for longDeviceName, 10 for type,3 for index .. 128 would be sufficient - - sprintf(unique_id,"%s_%s_%d",g_cfg.longDeviceName,type,index); - return unique_id; + sprintf(outBuffer,"%s_%s_%d",g_cfg.longDeviceName,type,index); } int http_fn_cfg_ha(http_request_t *request) { @@ -1259,6 +1261,7 @@ int http_fn_cfg_ha(http_request_t *request) { const char *baseName; int i; char mqttAdded = 0; + char unique_id[128]; //64 for longDeviceName, 10 for type,3 for index .. 128 would be sufficient baseName = CFG_GetShortDeviceName(); @@ -1298,7 +1301,8 @@ int http_fn_cfg_ha(http_request_t *request) { switchAdded=1; } - hprintf128(request," - unique_id: \"%s\"\n",build_hass_unique_id("relay",i)); + build_hass_unique_id(unique_id,"relay",i); + hprintf128(request," - unique_id: \"%s\"\n",unique_id); hprintf128(request," name: \"%s %i\"\n",baseName,i); hprintf128(request," state_topic: \"%s/%i/get\"\n",baseName,i); hprintf128(request," command_topic: \"%s/%i/set\"\n",baseName,i); @@ -1325,7 +1329,8 @@ int http_fn_cfg_ha(http_request_t *request) { lightAdded=1; } - hprintf128(request," - unique_id: \"%s\"\n",build_hass_unique_id("light",i)); + build_hass_unique_id(unique_id,"light",i); + hprintf128(request," - unique_id: \"%s\"\n",unique_id); hprintf128(request," name: \"%s %i\"\n",baseName,i); hprintf128(request," state_topic: \"%s/%i/get\"\n",baseName,i); hprintf128(request," command_topic: \"%s/%i/set\"\n",baseName,i);