diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 133305fdd..c0c88240b 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1256,6 +1256,41 @@ 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) { + int i; + char topic[32]; + int relayCount=0; + int pwmCount=0; + + http_setup(request, httpMimeTypeText); + get_Relay_PWM_Count(&relayCount, &pwmCount); + + if ((relayCount == 0) && (pwmCount == 0)) { + poststr(request, NULL); + return 0; +} + +/// @brief Computes the Relay and PWM count. +/// @param relayCount +/// @param pwmCount +void get_Relay_PWM_Count(int *relayCount, int *pwmCount){ + (*relayCount) = 0; + (*pwmCount) = 0; + + for(int i = 0; i < PLATFORM_GPIO_MAX; i++) { + int role = PIN_GetPinRoleForPinIndex(i); + if(role == IOR_Relay || role == IOR_Relay_n || role == IOR_LED || role == IOR_LED_n) { + (*relayCount)++; + } + else if(role == IOR_PWM || role == IOR_PWM_n) { + (*pwmCount)++; + } + } +} + /// @brief Sends HomeAssistant discovery MQTT messages. /// @param request /// @return @@ -1284,7 +1319,11 @@ int http_fn_ha_discovery(http_request_t *request) { for(i = 0; i < CHANNEL_MAX; i++) { if(h_isChannelRelay(i)) { HassDeviceInfo *dev_info = hass_init_device_info(ENTITY_RELAY, i, "1", "0"); - MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); + + cJSON_AddStringToObject(dev_info->root, "pl_avail", "1"); //payload_available + cJSON_AddStringToObject(dev_info->root, "pl_not_avail", "0"); //payload_not_available + + MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info)); hass_free_device_info(dev_info); } } @@ -1304,7 +1343,7 @@ int http_fn_ha_discovery(http_request_t *request) { sprintf(tmp,"%s/%i/set",baseName,i); cJSON_AddStringToObject(dev_info->root, "bri_cmd_t", tmp); //brightness_command_topic - MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); + MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info)); hass_free_device_info(dev_info); } } @@ -1403,9 +1442,13 @@ int http_fn_ha_cfg(http_request_t *request) { poststr(request,""); - poststr(request,htmlFooterReturnToCfgLink); + poststr(request, HomeAssistantDiscoveryScript); poststr(request,"
 

"); + poststr(request,htmlReturnToCfg); + HTTP_AddBuildFooter(request); + poststr(request,htmlEnd); + http_html_end(request); poststr(request, NULL); return 0; @@ -1545,7 +1588,8 @@ int http_fn_cm(http_request_t *request) { int http_fn_cfg(http_request_t *request) { http_setup(request, httpMimeTypeHTML); - http_html_start(request, "Config"); + poststr(request,htmlHeader); + HTTP_AddHeader(request); postFormAction(request, "cfg_pins", "Configure Module"); postFormAction(request, "cfg_generic", "Configure General"); diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index d6125cf27..4de085f3e 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -432,7 +432,7 @@ static OBK_Publish_Result MQTT_PublishTopicToClient(mqtt_client_t *client, const char *pub_topic = (char *)os_malloc(strlen(sTopic) + 1 + strlen(sChannel) + 5 + 1); sprintf(pub_topic, "%s/%s%s", sTopic, sChannel, (appendGet == true ? "/get" : "")); - addLogAdv(LOG_INFO,LOG_FEATURE_MQTT,"Publishing to %s retain=%i",pub_topic, retain); + addLogAdv(LOG_INFO,LOG_FEATURE_MQTT,"Publishing to %s",pub_topic); err = mqtt_publish(client, pub_topic, sVal, strlen(sVal), qos, retain, mqtt_pub_request_cb, 0); os_free(pub_topic);