From 555df7e3710fe217634842698de1d7c7642b852f Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Mon, 2 Jan 2023 06:25:47 -0600 Subject: [PATCH 1/5] Invoke discovery on ip change --- src/user_main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/user_main.c b/src/user_main.c index 3e5f46be8..257c2b7a4 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -383,6 +383,8 @@ void Main_OnEverySecond() MQTT_DoItemPublish(PUBLISHITEM_SELF_IP); } EventHandlers_FireEvent(CMD_EVENT_IPCHANGE, 0); + + Main_ScheduleHomeAssistantDiscovery(1); //Invoke Hass discovery in the next pass } } From a9852663945f0ea5d4fd178317a16ececd7b7007 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Mon, 2 Jan 2023 06:29:32 -0600 Subject: [PATCH 2/5] Fixed message --- src/user_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/user_main.c b/src/user_main.c index 257c2b7a4..2711fe83b 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -383,7 +383,7 @@ void Main_OnEverySecond() MQTT_DoItemPublish(PUBLISHITEM_SELF_IP); } EventHandlers_FireEvent(CMD_EVENT_IPCHANGE, 0); - + Main_ScheduleHomeAssistantDiscovery(1); //Invoke Hass discovery in the next pass } } @@ -502,7 +502,7 @@ void Main_OnEverySecond() } } else { - ADDLOGF_INFO("HA discovery is scheduled, but MQTT connection is present yet\n"); + ADDLOGF_INFO("HA discovery is scheduled, but MQTT connection is not present yet\n"); } } if (g_openAP) From 65b9f7f4be39711750f4e9c8934a332ae961c5c4 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Mon, 2 Jan 2023 20:26:16 -0600 Subject: [PATCH 3/5] Added a controlling flag --- src/httpserver/http_fns.c | 7 +++++++ src/new_pins.h | 4 ++-- src/user_main.c | 14 +++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 1bc928f56..b38caa3ef 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2556,6 +2556,12 @@ int http_fn_cfg_pins(http_request_t* request) { // saving the configuration instead of waiting. //CFG_Save_SetupTimer(); CFG_Save_IfThereArePendingChanges(); + + //Invoke Hass discovery if configuration has changed + if(!bSafeMode && CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } + hprintf255(request, "Pins update - %i reqs, %i changed!

", iChangedRequested, iChanged); } // strcat(outbuf,""); @@ -2658,6 +2664,7 @@ const char* g_obk_flagNames[] = { #else "[UART] Use alternate UART for BL0942, CSE, TuyaMCU, etc", #endif + "[HASS] Invoke HomeAssistant discovery on change to ip address, configuration", "error", "error", "error", diff --git a/src/new_pins.h b/src/new_pins.h index 6088a212f..0f7054cf7 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -184,9 +184,9 @@ typedef struct pinsState_s { #define OBK_FLAG_LED_EMULATE_COOL_WITH_RGB 24 #define OBK_FLAG_POWER_ALLOW_NEGATIVE 25 #define OBK_FLAG_USE_SECONDARY_UART 26 +#define OBK_FLAG_AUTOMAIC_HASS_DISCOVERY 27 - -#define OBK_TOTAL_FLAGS 27 +#define OBK_TOTAL_FLAGS 28 #define CGF_MQTT_CLIENT_ID_SIZE 64 diff --git a/src/user_main.c b/src/user_main.c index 2711fe83b..132463454 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -326,9 +326,10 @@ void Main_LogPowerSave(){ } */ } -#endif - +#endif +/// @brief Schedule HomeAssistant discovery. The caller should check OBK_FLAG_AUTOMAIC_HASS_DISCOVERY if necessary. +/// @param seconds void Main_ScheduleHomeAssistantDiscovery(int seconds) { g_doHomeAssistantDiscoveryIn = seconds; } @@ -384,7 +385,10 @@ void Main_OnEverySecond() } EventHandlers_FireEvent(CMD_EVENT_IPCHANGE, 0); - Main_ScheduleHomeAssistantDiscovery(1); //Invoke Hass discovery in the next pass + //Invoke Hass discovery if ipaddr changed + if(CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } } } @@ -1019,6 +1023,10 @@ void Main_Init_After_Delay() HTTPServer_Start(); ADDLOGF_DEBUG("Started http tcp server\r\n"); + //Always invoke discovery on startup. This accounts for change in ipaddr before startup and firmware update. + if(CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } // only initialise certain things if we are not in AP mode if (!bSafeMode) From 255ac9cee8f2615427ddc894ac7c64246219e984 Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Tue, 3 Jan 2023 19:25:21 -0600 Subject: [PATCH 4/5] Adjusted comment --- src/httpserver/http_fns.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index b38caa3ef..030abb255 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2557,8 +2557,8 @@ int http_fn_cfg_pins(http_request_t* request) { //CFG_Save_SetupTimer(); CFG_Save_IfThereArePendingChanges(); - //Invoke Hass discovery if configuration has changed - if(!bSafeMode && CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + // Invoke Hass discovery if configuration has changed and not in safe mode. + if (!bSafeMode && CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { Main_ScheduleHomeAssistantDiscovery(1); } From f3a8c7947fbe80ef99a9723d9eef02abc222b0fd Mon Sep 17 00:00:00 2001 From: Indu Prakash Date: Tue, 3 Jan 2023 19:40:17 -0600 Subject: [PATCH 5/5] Adjusted one more location for safe mode --- src/user_main.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/user_main.c b/src/user_main.c index 132463454..613932862 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -386,7 +386,7 @@ void Main_OnEverySecond() EventHandlers_FireEvent(CMD_EVENT_IPCHANGE, 0); //Invoke Hass discovery if ipaddr changed - if(CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + if (CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { Main_ScheduleHomeAssistantDiscovery(1); } } @@ -1023,14 +1023,14 @@ void Main_Init_After_Delay() HTTPServer_Start(); ADDLOGF_DEBUG("Started http tcp server\r\n"); - //Always invoke discovery on startup. This accounts for change in ipaddr before startup and firmware update. - if(CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { - Main_ScheduleHomeAssistantDiscovery(1); - } - // only initialise certain things if we are not in AP mode if (!bSafeMode) - { + { + //Always invoke discovery on startup. This accounts for change in ipaddr before startup and firmware update. + if (CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } + Main_Init_AfterDelay_Unsafe(true); }