diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 900bc3735..59d3208ef 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2564,6 +2564,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 and not in safe mode. + if (!bSafeMode && CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } + hprintf255(request, "Pins update - %i reqs, %i changed!

", iChangedRequested, iChanged); } // strcat(outbuf,""); @@ -2666,6 +2672,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 fb45bbd0a..07c2e588e 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -323,9 +323,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; } @@ -380,6 +381,11 @@ void Main_OnEverySecond() MQTT_DoItemPublish(PUBLISHITEM_SELF_IP); } EventHandlers_FireEvent(CMD_EVENT_IPCHANGE, 0); + + //Invoke Hass discovery if ipaddr changed + if (CFG_HasFlag(OBK_FLAG_AUTOMAIC_HASS_DISCOVERY)) { + Main_ScheduleHomeAssistantDiscovery(1); + } } } @@ -495,7 +501,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) @@ -1011,10 +1017,14 @@ void Main_Init_After_Delay() HTTPServer_Start(); ADDLOGF_DEBUG("Started http tcp server\r\n"); - // 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); }