diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index 06235681c..6b2e871d3 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -40,12 +40,7 @@ int changeSendThresholds[OBK_NUM_MEASUREMENTS] = { 0.002f, // current - OBK_CURRENT 0.25f, // power - OBK_POWER }; -// how are they called in MQTT -const char *mqttNames[OBK_NUM_MEASUREMENTS] = { - "voltage", - "current", - "power" -}; + int changeSendAlwaysFrames = 60; int changeDoNotSendMinFrames = 5; @@ -124,7 +119,7 @@ void BL_ProcessUpdate(float voltage, float current, float power) EventHandlers_ProcessVariableChange_Integer(CMD_EVENT_CHANGE_VOLTAGE+i, lastSentValues[i], lastReadings[i]); } lastSentValues[i] = lastReadings[i]; - MQTT_PublishMain_StringFloat(mqttNames[i],lastReadings[i]); + MQTT_PublishMain_StringFloat(sensor_mqttNames[i],lastReadings[i]); stat_updatesSent++; } else { // no change frame diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 2a044d8f2..ad3052386 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -10,7 +10,13 @@ #include "../i2c/drv_i2c_public.h" #include "drv_ntp.h" #include "../httpserver/new_http.h" +#include "drv_public.h" +const char *sensor_mqttNames[OBK_NUM_MEASUREMENTS] = { + "voltage", + "current", + "power" +}; typedef struct driver_s { const char *name; @@ -28,9 +34,12 @@ static driver_t g_drivers[] = { { "TuyaMCU", TuyaMCU_Init, TuyaMCU_RunFrame, NULL, NULL, NULL, NULL, false }, { "NTP", NTP_Init, NTP_OnEverySecond, NULL, NULL, NULL, NULL, false }, { "I2C", DRV_I2C_Init, DRV_I2C_EverySecond, NULL, NULL, NULL, NULL, false }, + + //These 3 measure power { "BL0942", BL0942_Init, BL0942_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, { "BL0937", BL0937_Init, BL0937_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, { "CSE7766", CSE7766_Init, CSE7766_RunFrame, BL09XX_AppendInformationToHTTPIndexPage, NULL, NULL, NULL, false }, + #if PLATFORM_BEKEN { "DGR", DRV_DGR_Init, NULL, NULL, DRV_DGR_RunFrame, DRV_DGR_Shutdown, DRV_DGR_OnChannelChanged, false }, { "DDP", DRV_DDP_Init, NULL, NULL, DRV_DDP_RunFrame, DRV_DDP_Shutdown, NULL, false }, @@ -38,7 +47,7 @@ static driver_t g_drivers[] = { { "SM2135", SM2135_Init, SM2135_RunFrame, NULL, NULL, NULL, SM2135_OnChannelChanged, false }, { "BP5758D", BP5758D_Init, BP5758D_RunFrame, NULL, NULL, NULL, BP5758D_OnChannelChanged, false }, { "BP1658CJ", BP1658CJ_Init, BP1658CJ_RunFrame, NULL, NULL, NULL, BP1658CJ_OnChannelChanged, false }, - { "tmSensor", TuyaMCU_Sensor_Init, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false }, + { "tmSensor", TuyaMCU_Sensor_Init, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false } }; static int g_numDrivers = sizeof(g_drivers)/sizeof(g_drivers[0]); @@ -230,3 +239,11 @@ void DRV_AppendInformationToHTTPIndexPage(http_request_t *request) { } hprintf128(request,", total %i",g_numDrivers); } + +bool DRV_IsMeasuringPower(){ +#ifndef OBK_DISABLE_ALL_DRIVERS + return DRV_IsRunning("BL0937") || DRV_IsRunning("BL0942") || DRV_IsRunning("CSE7766"); +#else + return false; +#endif +} diff --git a/src/driver/drv_public.h b/src/driver/drv_public.h index 6e250d124..490ec5591 100644 --- a/src/driver/drv_public.h +++ b/src/driver/drv_public.h @@ -9,6 +9,9 @@ enum { OBK_NUM_MEASUREMENTS, }; +// MQTT names of sensors (voltage, current, power) +extern const char *sensor_mqttNames[]; + void DRV_Generic_Init(); void DRV_AppendInformationToHTTPIndexPage(http_request_t *request); void DRV_OnEverySecond(); @@ -21,10 +24,7 @@ void SM2135_Write(byte *rgbcw); void BP5758D_Write(byte *rgbcw); void DRV_DGR_OnLedDimmerChange(int iVal); void DRV_DGR_OnLedEnableAllChange(int iVal); + // OBK_POWER etc float DRV_GetReading(int type); - - - - - +bool DRV_IsMeasuringPower(); diff --git a/src/httpserver/hass.c b/src/httpserver/hass.c index ccfe1eb64..98d42bbb4 100644 --- a/src/httpserver/hass.c +++ b/src/httpserver/hass.c @@ -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; diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 5c0c413f7..b98e5971d 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -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);