diff --git a/docs/commands-extended.md b/docs/commands-extended.md
index e5e11554e..da6613ad1 100644
--- a/docs/commands-extended.md
+++ b/docs/commands-extended.md
@@ -30,6 +30,7 @@ Do not add anything here, as it will overwritten with next rebuild.
| simonirtest | | Simons Special Test | File: cmnds/cmd_main.c
Function: CMD_SimonTest |
| if | [Condition]['then'][CommandA]['else'][CommandB] | Executed a conditional. Condition should be single line. You must always use 'then' after condition. 'else' is optional. Use aliases or quotes for commands with spaces | File: cmnds/cmd_main.c
Function: CMD_If |
| ota_http | [HTTP_URL] | Starts the firmware update procedure, the argument should be a reachable HTTP server file. You can easily setup HTTP server with Xampp, or Visual Code, or Python, etc. Make sure you are using OTA file for a correct platform (getting N platform RBL on T will brick device, etc etc) | File: cmnds/cmd_main.c
Function: CMD_HTTPOTA |
+| scheduleHADiscovery | [Seconds] | This will schedule HA discovery, the discovery will happen with given number of seconds, but timer only counts when MQTT is connected. It will not work without MQTT online, so you must set MQTT credentials first. | File: cmnds/cmd_main.c
Function: CMD_ScheduleHADiscovery |
| led_dimmer | [Value] | set output dimmer 0..100 | File: cmnds/cmd_newLEDDriver.c
Function: dimmer |
| add_dimmer | [Value][bWrapAroundInsteadOfHold] | Adds a given value to current LED dimmer. Function can wrap or clamp if max/min is exceeded. | File: cmnds/cmd_newLEDDriver.c
Function: add_dimmer |
| led_enableAll | [1or0orToggle] | Power on/off LED but remember the RGB(CW) values | File: cmnds/cmd_newLEDDriver.c
Function: enableAll |
diff --git a/docs/commands.md b/docs/commands.md
index cd288d0c7..f7f2acde2 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -30,6 +30,7 @@ Do not add anything here, as it will overwritten with next rebuild.
| simonirtest | | Simons Special Test |
| if | [Condition]['then'][CommandA]['else'][CommandB] | Executed a conditional. Condition should be single line. You must always use 'then' after condition. 'else' is optional. Use aliases or quotes for commands with spaces |
| ota_http | [HTTP_URL] | Starts the firmware update procedure, the argument should be a reachable HTTP server file. You can easily setup HTTP server with Xampp, or Visual Code, or Python, etc. Make sure you are using OTA file for a correct platform (getting N platform RBL on T will brick device, etc etc) |
+| scheduleHADiscovery | [Seconds] | This will schedule HA discovery, the discovery will happen with given number of seconds, but timer only counts when MQTT is connected. It will not work without MQTT online, so you must set MQTT credentials first. |
| led_dimmer | [Value] | set output dimmer 0..100 |
| add_dimmer | [Value][bWrapAroundInsteadOfHold] | Adds a given value to current LED dimmer. Function can wrap or clamp if max/min is exceeded. |
| led_enableAll | [1or0orToggle] | Power on/off LED but remember the RGB(CW) values |
diff --git a/src/cmnds/cmd_main.c b/src/cmnds/cmd_main.c
index d159e0f0b..eab507f11 100644
--- a/src/cmnds/cmd_main.c
+++ b/src/cmnds/cmd_main.c
@@ -41,6 +41,20 @@ static commandResult_t CMD_PowerSave(const void *context, const char *cmd, const
}
+static commandResult_t CMD_ScheduleHADiscovery(const void *context, const char *cmd, const char *args, int cmdFlags) {
+ int delay;
+
+ if (args && *args) {
+ delay = atoi(args);
+ }
+ else {
+ delay = 5;
+ }
+
+ Main_ScheduleHomeAssistantDiscovery(delay);
+
+ return CMD_RES_OK;
+}
static commandResult_t CMD_HTTPOTA(const void *context, const char *cmd, const char *args, int cmdFlags) {
if (args && *args) {
@@ -156,6 +170,11 @@ void CMD_Init_Early() {
//cmddetail:"fn":"CMD_HTTPOTA","file":"cmnds/cmd_main.c","requires":"",
//cmddetail:"examples":""}
CMD_RegisterCommand("ota_http", "", CMD_HTTPOTA, NULL, NULL);
+ //cmddetail:{"name":"scheduleHADiscovery","args":"[Seconds]",
+ //cmddetail:"descr":"This will schedule HA discovery, the discovery will happen with given number of seconds, but timer only counts when MQTT is connected. It will not work without MQTT online, so you must set MQTT credentials first.",
+ //cmddetail:"fn":"CMD_ScheduleHADiscovery","file":"cmnds/cmd_main.c","requires":"",
+ //cmddetail:"examples":""}
+ CMD_RegisterCommand("scheduleHADiscovery", "", CMD_ScheduleHADiscovery, NULL, NULL);
#if (defined WINDOWS) || (defined PLATFORM_BEKEN)
CMD_InitScripting();
diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c
index 7eea17fe3..d361fd62b 100644
--- a/src/httpserver/http_fns.c
+++ b/src/httpserver/http_fns.c
@@ -1530,12 +1530,8 @@ void get_Relay_PWM_Count(int* relayCount, int* pwmCount) {
}
-/// @brief Sends HomeAssistant discovery MQTT messages.
-/// @param request
-/// @return
-int http_fn_ha_discovery(http_request_t* request) {
+void doHomeAssistantDiscovery(const char *topic, http_request_t *request) {
int i;
- char topic[32];
int relayCount;
int pwmCount;
bool ledDriverChipRunning;
@@ -1543,14 +1539,9 @@ int http_fn_ha_discovery(http_request_t* request) {
bool measuringPower = false;
struct cJSON_Hooks hooks;
- http_setup(request, httpMimeTypeText);
-
- if (MQTT_IsReady() == false) {
- poststr(request, "MQTT not running.");
- poststr(request, NULL);
- return 0;
+ if (topic == 0 || *topic == 0) {
+ topic = "homeassistant";
}
-
#ifndef OBK_DISABLE_ALL_DRIVERS
measuringPower = DRV_IsMeasuringPower();
#endif
@@ -1562,16 +1553,17 @@ int http_fn_ha_discovery(http_request_t* request) {
ledDriverChipRunning = LED_IsLedDriverChipRunning();
if ((relayCount == 0) && (pwmCount == 0) && !measuringPower && !ledDriverChipRunning) {
- poststr(request, "No relay, PWM or power driver running.");
- poststr(request, NULL);
- return 0;
+ const char *msg = "No relay, PWM or power driver running.";
+ if (request) {
+ poststr(request, msg);
+ poststr(request, NULL);
+ }
+ else {
+ addLogAdv(LOG_ERROR, LOG_FEATURE_HTTP, "HA discovery: %s\r\n", msg);
+ }
+ return;
}
- if (!http_getArg(request->url, "prefix", topic, sizeof(topic))) {
- sprintf(topic, "homeassistant"); //default discovery topic is `homeassistant`
- }
-
-
hooks.malloc_fn = os_malloc;
hooks.free_fn = os_free;
cJSON_InitHooks(&hooks);
@@ -1630,7 +1622,7 @@ int http_fn_ha_discovery(http_request_t* request) {
#ifndef OBK_DISABLE_ALL_DRIVERS
if (measuringPower == true) {
- for (i = 0;i < OBK_NUM_SENSOR_COUNT;i++)
+ for (i = 0; i < OBK_NUM_SENSOR_COUNT; i++)
{
dev_info = hass_init_sensor_device_info(i);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
@@ -1638,6 +1630,26 @@ int http_fn_ha_discovery(http_request_t* request) {
}
}
#endif
+}
+
+/// @brief Sends HomeAssistant discovery MQTT messages.
+/// @param request
+/// @return
+int http_fn_ha_discovery(http_request_t* request) {
+ char topic[32];
+
+ http_setup(request, httpMimeTypeText);
+
+ if (MQTT_IsReady() == false) {
+ poststr(request, "MQTT not running.");
+ poststr(request, NULL);
+ return 0;
+ }
+
+ // even if it returns the empty HA topic,
+ // the function call below will set default
+ http_getArg(request->url, "prefix", topic, sizeof(topic));
+ doHomeAssistantDiscovery(topic, request);
poststr(request, "MQTT discovery queued.");
poststr(request, NULL);
diff --git a/src/httpserver/http_fns.h b/src/httpserver/http_fns.h
index c8c7a1f66..e14d03ba3 100644
--- a/src/httpserver/http_fns.h
+++ b/src/httpserver/http_fns.h
@@ -1,6 +1,8 @@
#include "new_http.h"
+// TODO: move it out
+void doHomeAssistantDiscovery(const char *topic, http_request_t *request);
int http_fn_about(http_request_t* request);
int http_fn_cfg_mqtt(http_request_t* request);
diff --git a/src/new_common.h b/src/new_common.h
index 93818370d..902f491c4 100644
--- a/src/new_common.h
+++ b/src/new_common.h
@@ -378,6 +378,7 @@ int Time_getUpTimeSeconds();
char Tiny_CRC8(const char *data,int length);
void RESET_ScheduleModuleReset(int delSeconds);
void MAIN_ScheduleUnsafeInit(int delSeconds);
+void Main_ScheduleHomeAssistantDiscovery(int seconds);
int Main_IsConnectedToWiFi();
int Main_IsOpenAccessPointMode();
void Main_Init();
diff --git a/src/user_main.c b/src/user_main.c
index c8452952e..21db82513 100644
--- a/src/user_main.c
+++ b/src/user_main.c
@@ -20,6 +20,7 @@
#include "obk_config.h"
#include "httpserver/new_http.h"
+#include "httpserver/http_fns.h"
#include "new_pins.h"
#include "quicktick.h"
#include "new_cfg.h"
@@ -260,6 +261,7 @@ void Main_OnPingCheckerReply(int ms)
g_timeSinceLastPingReply = 0;
}
+int g_doHomeAssistantDiscoveryIn = 0;
int g_bBootMarkedOK = 0;
static int bMQTTconnected = 0;
@@ -323,6 +325,9 @@ void Main_LogPowerSave(){
#endif
+void Main_ScheduleHomeAssistantDiscovery(int seconds) {
+ g_doHomeAssistantDiscoveryIn = seconds;
+}
void Main_OnEverySecond()
{
@@ -464,6 +469,21 @@ void Main_OnEverySecond()
}
}
+ if (g_doHomeAssistantDiscoveryIn) {
+ if (MQTT_IsReady()) {
+ g_doHomeAssistantDiscoveryIn--;
+ if (g_doHomeAssistantDiscoveryIn == 0) {
+ ADDLOGF_INFO("Will do request HA discovery now.\n");
+ doHomeAssistantDiscovery(0, 0);
+ }
+ else {
+ ADDLOGF_INFO("Will scheduled HA discovery in %i seconds\n", g_doHomeAssistantDiscoveryIn);
+ }
+ }
+ else {
+ ADDLOGF_INFO("HA discovery is scheduled, but MQTT connection is present yet\n");
+ }
+ }
if (g_openAP)
{
g_openAP--;