This commit is contained in:
Indu Prakash
2022-10-07 19:57:16 -05:00
parent 87e69d19b2
commit 2af56fb56f
5 changed files with 73 additions and 37 deletions

View File

@ -3,11 +3,13 @@
#include "../new_cfg.h"
#include "../logging/logging.h"
#include "../hal/hal_wifi.h"
#include "../driver/drv_public.h"
/*
Abbreviated node names - https://www.home-assistant.io/docs/mqtt/discovery/
Light - https://www.home-assistant.io/integrations/light.mqtt/
Switch - https://www.home-assistant.io/integrations/switch.mqtt/
Sensor - https://www.home-assistant.io/integrations/sensor.mqtt/
*/
//Buffer used to populate values in cJSON_Add* calls. The values are based on
@ -41,7 +43,8 @@ void hass_populate_unique_id(ENTITY_TYPE type, int index, char *uniq_id){
break;
case ENTITY_SENSOR:
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ENTITY_SENSOR not yet supported");
sprintf(uniq_id,"%s_%s_%d", longDeviceName, "sensor", index);
break;
}
}
@ -61,7 +64,6 @@ void hass_print_unique_id(http_request_t *request, const char *fmt, ENTITY_TYPE
/// @param uniq_id Entity unique id
/// @param info Device info
void hass_populate_device_config_channel(ENTITY_TYPE type, char *uniq_id, HassDeviceInfo *info){
//device_type is `switch` or `light`
switch(type){
case ENTITY_LIGHT_PWM:
case ENTITY_LIGHT_RGB:
@ -74,7 +76,8 @@ void hass_populate_device_config_channel(ENTITY_TYPE type, char *uniq_id, HassDe
break;
case ENTITY_SENSOR:
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ENTITY_SENSOR not yet supported");
sprintf(info->channel, "sensor/%s/config", uniq_id);
break;
}
}
@ -100,7 +103,7 @@ cJSON *hass_build_device_node(cJSON *ids) {
/// @brief Initializes HomeAssistant device discovery storage with common values.
/// @param type
/// @param index Ignored for RGB
/// @param index Ignored for RGB, for sensor this corresponds to sensor_mqttNames.
/// @param payload_on
/// @param payload_off
/// @return
@ -130,7 +133,10 @@ HassDeviceInfo *hass_init_device_info(ENTITY_TYPE type, int index, char *payload
sprintf(g_hassBuffer,"%s",CFG_GetShortDeviceName());
break;
case ENTITY_SENSOR:
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ENTITY_SENSOR not yet supported");
#ifndef OBK_DISABLE_ALL_DRIVERS
sprintf(g_hassBuffer,"%s %s",CFG_GetShortDeviceName(), sensor_mqttNames[index]);
#endif
break;
}
cJSON_AddStringToObject(info->root, "name", g_hassBuffer);
@ -139,6 +145,7 @@ HassDeviceInfo *hass_init_device_info(ENTITY_TYPE type, int index, char *payload
cJSON_AddStringToObject(info->root, "pl_on", payload_on); //payload_on
cJSON_AddStringToObject(info->root, "pl_off", payload_off); //payload_off
cJSON_AddStringToObject(info->root, "uniq_id", info->unique_id); //unique_id
addLogAdv(LOG_DEBUG, LOG_FEATURE_HASS, "root=%p", info->root);
@ -164,7 +171,7 @@ HassDeviceInfo *hass_init_relay_device_info(int index){
/// @brief Initializes HomeAssistant light device discovery storage.
/// @param type
/// @param index Ignored for RGB
/// @param index Ignored for RGB, for sensor this corresponds to sensor_mqttNames.
/// @return
HassDeviceInfo *hass_init_light_device_info(ENTITY_TYPE type, int index){
const char *clientId = CFG_GetMQTTClientId();
@ -223,8 +230,19 @@ HassDeviceInfo *hass_init_light_device_info(ENTITY_TYPE type, int index){
break;
default:
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "Unsupported light type %s", type);
case ENTITY_SENSOR:
#ifndef OBK_DISABLE_ALL_DRIVERS
info = hass_init_device_info(type, index, "1", "0");
//https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
//device_class automatically assigns unit,icon
cJSON_AddStringToObject(info->root, "dev_cla", sensor_mqttNames[index]); //device_class=voltage,current,power
sprintf(g_hassBuffer,"%s/%s/get",clientId,sensor_mqttNames[index]);
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, g_hassBuffer);
#endif
break;
}
return info;

View File

@ -1229,6 +1229,15 @@ void get_Relay_PWM_Count(int *relayCount, int *pwmCount){
}
}
bool isLedDriverChipRunning()
{
#ifndef OBK_DISABLE_ALL_DRIVERS
return DRV_IsRunning("SM2135") || DRV_IsRunning("BP5758D");
#else
return false;
#endif
}
/// @brief Sends HomeAssistant discovery MQTT messages.
/// @param request
/// @return
@ -1237,17 +1246,10 @@ int http_fn_ha_discovery(http_request_t *request) {
char topic[32];
int relayCount = 0;
int pwmCount = 0;
char bLedDriverChipRunning;
http_setup(request, httpMimeTypeText);
get_Relay_PWM_Count(&relayCount, &pwmCount);
#ifndef OBK_DISABLE_ALL_DRIVERS
bLedDriverChipRunning = DRV_IsRunning("SM2135") || DRV_IsRunning("BP5758D");
#else
bLedDriverChipRunning = 0;
#endif
if ((relayCount == 0) && (pwmCount == 0)) {
poststr(request, NULL);
return 0;
@ -1272,7 +1274,7 @@ int http_fn_ha_discovery(http_request_t *request) {
}
}
if (pwmCount == 5 || bLedDriverChipRunning) {
if (pwmCount == 5 || isLedDriverChipRunning()) {
// Enable + RGB control + CW control
HassDeviceInfo *dev_info = hass_init_light_device_info(ENTITY_LIGHT_RGBCW, -1);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
@ -1294,6 +1296,17 @@ int http_fn_ha_discovery(http_request_t *request) {
}
}
#ifndef OBK_DISABLE_ALL_DRIVERS
if (DRV_IsMeasuringPower()){
for(i = 0;i < OBK_NUM_MEASUREMENTS;i ++)
{
HassDeviceInfo *dev_info = hass_init_light_device_info(ENTITY_SENSOR, i);
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
hass_free_device_info(dev_info);
}
}
#endif
poststr(request, NULL);
return 0;
}
@ -1321,7 +1334,6 @@ int http_fn_ha_cfg(http_request_t *request) {
char mqttAdded = 0;
char switchAdded = 0;
char lightAdded = 0;
int bLedDriverChipRunning;
shortDeviceName = CFG_GetShortDeviceName();
clientId = CFG_GetMQTTClientId();
@ -1338,12 +1350,6 @@ int http_fn_ha_cfg(http_request_t *request) {
get_Relay_PWM_Count(&relayCount, &pwmCount);
#ifndef OBK_DISABLE_ALL_DRIVERS
bLedDriverChipRunning = DRV_IsRunning("SM2135") || DRV_IsRunning("BP5758D");
#else
bLedDriverChipRunning = 0;
#endif
if(relayCount > 0) {
for(i = 0; i < CHANNEL_MAX; i++) {
@ -1370,7 +1376,7 @@ int http_fn_ha_cfg(http_request_t *request) {
}
}
}
if(pwmCount == 5 || bLedDriverChipRunning) {
if(pwmCount == 5 || isLedDriverChipRunning()) {
// Enable + RGB control + CW control
if (mqttAdded == 0){
poststr(request,HASS_MQTT_NODE);