mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-10 11:25:41 +00:00
Merge pull request #300 from iprak/resolve-issues-with-2-pwm
HomeAssistant Discovery: Handle 2 PWM and other fixes
This commit is contained in:
@ -33,6 +33,7 @@ void hass_populate_unique_id(ENTITY_TYPE type, int index, char* uniq_id) {
|
||||
sprintf(uniq_id, "%s_%s_%d", longDeviceName, "light", index);
|
||||
break;
|
||||
|
||||
case ENTITY_LIGHT_PWMCW:
|
||||
case ENTITY_LIGHT_RGB:
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
sprintf(uniq_id, "%s_%s", longDeviceName, "light");
|
||||
@ -66,6 +67,7 @@ void hass_print_unique_id(http_request_t* request, const char* fmt, ENTITY_TYPE
|
||||
void hass_populate_device_config_channel(ENTITY_TYPE type, char* uniq_id, HassDeviceInfo* info) {
|
||||
switch (type) {
|
||||
case ENTITY_LIGHT_PWM:
|
||||
case ENTITY_LIGHT_PWMCW:
|
||||
case ENTITY_LIGHT_RGB:
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
sprintf(info->channel, "light/%s/config", uniq_id);
|
||||
@ -103,9 +105,9 @@ cJSON* hass_build_device_node(cJSON* ids) {
|
||||
|
||||
/// @brief Initializes HomeAssistant device discovery storage with common values.
|
||||
/// @param type
|
||||
/// @param index Ignored for RGB, for sensor this corresponds to sensor_mqttNames.
|
||||
/// @param payload_on
|
||||
/// @param payload_off
|
||||
/// @param index This is used to generate generate unique_id and name. It is ignored for RGB. For sensor this corresponds to sensor_mqttNames.
|
||||
/// @param payload_on The payload that represents enabled state. This is not added for ENTITY_SENSOR.
|
||||
/// @param payload_off The payload that represents disabled state. This is not added for ENTITY_SENSOR.
|
||||
/// @return
|
||||
HassDeviceInfo* hass_init_device_info(ENTITY_TYPE type, int index, char* payload_on, char* payload_off) {
|
||||
HassDeviceInfo* info = os_malloc(sizeof(HassDeviceInfo));
|
||||
@ -122,35 +124,39 @@ HassDeviceInfo* hass_init_device_info(ENTITY_TYPE type, int index, char* payload
|
||||
info->root = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(info->root, "dev", info->device); //device
|
||||
|
||||
//Build the `name`
|
||||
switch (type) {
|
||||
case ENTITY_LIGHT_PWM:
|
||||
case ENTITY_RELAY:
|
||||
sprintf(g_hassBuffer, "%s %i", CFG_GetShortDeviceName(), index);
|
||||
break;
|
||||
case ENTITY_LIGHT_PWMCW:
|
||||
case ENTITY_LIGHT_RGB:
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
//There can only be one RGB so we can skip including index in the name
|
||||
//There can only be one RGB so we can skip including index in the name. Do the same
|
||||
//for 2 PWM case.
|
||||
sprintf(g_hassBuffer, "%s", CFG_GetShortDeviceName());
|
||||
break;
|
||||
case ENTITY_SENSOR:
|
||||
#ifndef OBK_DISABLE_ALL_DRIVERS
|
||||
if ((index >= OBK_VOLTAGE) && (index <= OBK_POWER))
|
||||
sprintf(g_hassBuffer, "%s %s", CFG_GetShortDeviceName(), sensor_mqttNames[index]);
|
||||
if ((index >= OBK_CONSUMPTION_TOTAL) && (index <= OBK_CONSUMPTION_STATS))
|
||||
sprintf(g_hassBuffer, "%s %s", CFG_GetShortDeviceName(), counter_mqttNames[index - OBK_CONSUMPTION_TOTAL]);
|
||||
if ((index >= OBK_VOLTAGE) && (index <= OBK_POWER))
|
||||
sprintf(g_hassBuffer, "%s %s", CFG_GetShortDeviceName(), sensor_mqttNames[index]);
|
||||
if ((index >= OBK_CONSUMPTION_TOTAL) && (index <= OBK_CONSUMPTION_STATS))
|
||||
sprintf(g_hassBuffer, "%s %s", CFG_GetShortDeviceName(), counter_mqttNames[index - OBK_CONSUMPTION_TOTAL]);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
cJSON_AddStringToObject(info->root, "name", g_hassBuffer);
|
||||
|
||||
cJSON_AddStringToObject(info->root, "~", CFG_GetMQTTClientId()); //base topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "avty_t", "~/connected"); //availability_topic, `online` value is broadcasted
|
||||
|
||||
cJSON_AddStringToObject(info->root, "pl_on", payload_on); //payload_on
|
||||
cJSON_AddStringToObject(info->root, "pl_off", payload_off); //payload_off
|
||||
if (type != ENTITY_SENSOR) {
|
||||
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
|
||||
cJSON_AddNumberToObject(info->root, "qos", 1);
|
||||
|
||||
addLogAdv(LOG_DEBUG, LOG_FEATURE_HASS, "root=%p", info->root);
|
||||
return info;
|
||||
@ -161,7 +167,6 @@ HassDeviceInfo* hass_init_device_info(ENTITY_TYPE type, int index, char* payload
|
||||
/// @return
|
||||
HassDeviceInfo* hass_init_relay_device_info(int index) {
|
||||
HassDeviceInfo* info = hass_init_device_info(ENTITY_RELAY, index, "1", "0");
|
||||
cJSON_AddNumberToObject(info->root, "qos", 1);
|
||||
|
||||
sprintf(g_hassBuffer, "~/%i/get", index);
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, g_hassBuffer); //state_topic
|
||||
@ -173,92 +178,95 @@ HassDeviceInfo* hass_init_relay_device_info(int index) {
|
||||
|
||||
/// @brief Initializes HomeAssistant light device discovery storage.
|
||||
/// @param type
|
||||
/// @param index Ignored for RGB, for sensor this corresponds to sensor_mqttNames.
|
||||
/// @return
|
||||
HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type, int index) {
|
||||
HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type) {
|
||||
const char* clientId = CFG_GetMQTTClientId();
|
||||
HassDeviceInfo* info = NULL;
|
||||
double brightness_scale = 100;
|
||||
|
||||
//We can just use 1 to generate unique_id and name for single PWM.
|
||||
//The payload_on/payload_off have to match the state_topic/command_topic values.
|
||||
info = hass_init_device_info(type, 1, "1", "0");
|
||||
|
||||
switch (type) {
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
case ENTITY_LIGHT_RGB:
|
||||
info = hass_init_device_info(type, index, "1", "0");
|
||||
|
||||
cJSON_AddStringToObject(info->root, "rgb_cmd_tpl", "{{'#%02x%02x%02x0000'|format(red,green,blue)}}"); //rgb_command_template
|
||||
cJSON_AddStringToObject(info->root, "rgb_val_tpl", "{{value[1:3]|int(base=16)}},{{value[3:5]|int(base=16)}},{{value[5:7]|int(base=16)}}"); //rgb_value_template
|
||||
|
||||
cJSON_AddStringToObject(info->root, "rgb_stat_t", "~/led_basecolor_rgb/get"); //rgb_state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_basecolor_rgb", clientId);
|
||||
cJSON_AddStringToObject(info->root, "rgb_cmd_t", g_hassBuffer); //rgb_command_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, "~/led_enableAll/get"); //state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_enableAll", clientId);
|
||||
cJSON_AddStringToObject(info->root, COMMAND_TOPIC_KEY, g_hassBuffer); //command_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "bri_stat_t", "~/led_dimmer/get"); //brightness_state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_dimmer", clientId);
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", g_hassBuffer); //brightness_command_topic
|
||||
|
||||
cJSON_AddNumberToObject(info->root, "bri_scl", 100); //brightness_scale
|
||||
|
||||
if (type == ENTITY_LIGHT_RGBCW) {
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_temperature", clientId);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_cmd_t", g_hassBuffer); //color_temp_command_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_stat_t", "~/led_temperature/get"); //color_temp_state_topic
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case ENTITY_LIGHT_PWM:
|
||||
info = hass_init_device_info(type, index, "99", "0");
|
||||
cJSON_AddStringToObject(info->root, "on_cmd_type", "brightness"); //on_command_type
|
||||
cJSON_AddNumberToObject(info->root, "bri_scl", 99); //brightness_scale
|
||||
cJSON_AddBoolToObject(info->root, "opt", cJSON_True); //optimistic
|
||||
cJSON_AddNumberToObject(info->root, "qos", 1);
|
||||
|
||||
cJSON_AddStringToObject(info->root, "bri_stat_t", "~/led_dimmer/get"); //brightness_state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_dimmer", clientId);
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", g_hassBuffer); //brightness_command_topic
|
||||
|
||||
sprintf(g_hassBuffer, "~/%i/get", index);
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, g_hassBuffer); //state_topic
|
||||
sprintf(g_hassBuffer, "~/%i/set", index);
|
||||
cJSON_AddStringToObject(info->root, COMMAND_TOPIC_KEY, g_hassBuffer); //command_topic
|
||||
|
||||
break;
|
||||
|
||||
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
|
||||
if ((index >= OBK_VOLTAGE) && (index <= OBK_POWER))
|
||||
{
|
||||
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);
|
||||
}
|
||||
if ((index >= OBK_CONSUMPTION_TOTAL) && (index <= OBK_CONSUMPTION_STATS))
|
||||
{
|
||||
cJSON_AddStringToObject(info->root, "dev_cla", counter_devClasses[index - OBK_CONSUMPTION_TOTAL]); //device_class=consumption
|
||||
|
||||
sprintf(g_hassBuffer, "%s/%s/get", clientId, counter_mqttNames[index - OBK_CONSUMPTION_TOTAL]);
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, g_hassBuffer);
|
||||
}
|
||||
#endif
|
||||
case ENTITY_LIGHT_PWMCW:
|
||||
brightness_scale = 99;
|
||||
|
||||
//Using `last` (the default) will send any style (brightness, color, etc) topics first and then a payload_on to the command_topic.
|
||||
//Using `first` will send the payload_on and then any style topics.
|
||||
//Using `brightness` will only send brightness commands instead of the payload_on to turn the light on.
|
||||
cJSON_AddStringToObject(info->root, "on_cmd_type", "first"); //on_command_type
|
||||
break;
|
||||
|
||||
default:
|
||||
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "Unsupported light type %s", type);
|
||||
}
|
||||
|
||||
if ((type == ENTITY_LIGHT_PWMCW) || (type == ENTITY_LIGHT_RGBCW)) {
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_temperature", clientId);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_cmd_t", g_hassBuffer); //color_temp_command_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_stat_t", "~/led_temperature/get"); //color_temp_state_topic
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, "~/led_enableAll/get"); //state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_enableAll", clientId);
|
||||
cJSON_AddStringToObject(info->root, COMMAND_TOPIC_KEY, g_hassBuffer); //command_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "bri_stat_t", "~/led_dimmer/get"); //brightness_state_topic
|
||||
sprintf(g_hassBuffer, "cmnd/%s/led_dimmer", clientId);
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", g_hassBuffer); //brightness_command_topic
|
||||
|
||||
cJSON_AddNumberToObject(info->root, "bri_scl", brightness_scale); //brightness_scale
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
#ifndef OBK_DISABLE_ALL_DRIVERS
|
||||
|
||||
/// @brief Initializes HomeAssistant sensor device discovery storage.
|
||||
/// @param index Index corresponding to sensor_mqttNames.
|
||||
/// @return
|
||||
HassDeviceInfo* hass_init_sensor_device_info(int index) {
|
||||
const char* clientId = CFG_GetMQTTClientId();
|
||||
HassDeviceInfo* info = hass_init_device_info(ENTITY_SENSOR, index, NULL, NULL);
|
||||
|
||||
//https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
|
||||
//device_class automatically assigns unit,icon
|
||||
if ((index >= OBK_VOLTAGE) && (index <= OBK_POWER))
|
||||
{
|
||||
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);
|
||||
}
|
||||
if ((index >= OBK_CONSUMPTION_TOTAL) && (index <= OBK_CONSUMPTION_STATS))
|
||||
{
|
||||
cJSON_AddStringToObject(info->root, "dev_cla", counter_devClasses[index - OBK_CONSUMPTION_TOTAL]); //device_class=consumption
|
||||
|
||||
sprintf(g_hassBuffer, "%s/%s/get", clientId, counter_mqttNames[index - OBK_CONSUMPTION_TOTAL]);
|
||||
cJSON_AddStringToObject(info->root, STATE_TOPIC_KEY, g_hassBuffer);
|
||||
}
|
||||
|
||||
//state_class can be measurement, total or total_increasing. Something like daily power consumption could be total_increasing.
|
||||
cJSON_AddStringToObject(info->root, "stat_cla", "measurement");
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// @brief Returns the discovery JSON.
|
||||
/// @param info
|
||||
/// @return
|
||||
|
||||
@ -5,11 +5,23 @@
|
||||
#include "../mqtt/new_mqtt.h"
|
||||
|
||||
typedef enum {
|
||||
/// @brief Switch
|
||||
ENTITY_RELAY = 0,
|
||||
ENTITY_LIGHT_PWM = 1,
|
||||
ENTITY_LIGHT_RGB = 2,
|
||||
ENTITY_LIGHT_RGBCW = 3,
|
||||
ENTITY_SENSOR = 4,
|
||||
|
||||
/// @brief Single PWM
|
||||
ENTITY_LIGHT_PWM,
|
||||
|
||||
/// @brief 2 PWM setup (brightness and temperature)
|
||||
ENTITY_LIGHT_PWMCW,
|
||||
|
||||
/// @brief RGB
|
||||
ENTITY_LIGHT_RGB,
|
||||
|
||||
/// @brief RGB + temperature
|
||||
ENTITY_LIGHT_RGBCW,
|
||||
|
||||
/// @brief Sensor (voltage, current, power)
|
||||
ENTITY_SENSOR
|
||||
} ENTITY_TYPE;
|
||||
|
||||
//unique_id is defined in hass_populate_unique_id and is based on CFG_GetDeviceName() whose size is CGF_DEVICE_NAME_SIZE.
|
||||
@ -36,6 +48,7 @@ typedef struct HassDeviceInfo_s {
|
||||
|
||||
void hass_print_unique_id(http_request_t* request, const char* fmt, ENTITY_TYPE type, int index);
|
||||
HassDeviceInfo* hass_init_relay_device_info(int index);
|
||||
HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type, int index);
|
||||
HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type);
|
||||
HassDeviceInfo* hass_init_sensor_device_info(int index);
|
||||
char* hass_build_discovery_json(HassDeviceInfo* info);
|
||||
void hass_free_device_info(HassDeviceInfo* info);
|
||||
|
||||
@ -92,9 +92,9 @@ template_t g_templates[] = {
|
||||
{ Setup_Device_Enbrighten_WFD4103, "Enbrighten WFD4103 WiFi Switch BK7231T WB2S"} ,
|
||||
{ Setup_Device_Zemismart_Light_Switch_KS_811_3, "Zemismart Light Switch (Neutral Optional) KS_811_3"} ,
|
||||
{ Setup_Device_TeslaSmartPlus_TSL_SPL_1, "Tesla Smart Plug. Model: (TSL-SPL-1)"},
|
||||
{ Setup_Device_Calex_900011_1_WB2S, "Calex Smart Power Plug 900011.1"},
|
||||
{ Setup_Device_Immax_NEO_LITE_NAS_WR07W, "Immax NEO Lite. Model: (NAS-WR07W)"} ,
|
||||
{ Setup_Device_MOES_TouchSwitch_WS_EU1_RFW_N, "MOES Touch Switch 1gang Model:(WS-EU1-RFW-N)"}
|
||||
{ Setup_Device_Calex_900011_1_WB2S, "Calex Smart Power Plug 900011.1"},
|
||||
{ Setup_Device_Immax_NEO_LITE_NAS_WR07W, "Immax NEO Lite. Model: (NAS-WR07W)"} ,
|
||||
{ Setup_Device_MOES_TouchSwitch_WS_EU1_RFW_N, "MOES Touch Switch 1gang Model:(WS-EU1-RFW-N)"}
|
||||
};
|
||||
|
||||
int g_total_templates = sizeof(g_templates) / sizeof(g_templates[0]);
|
||||
@ -549,33 +549,34 @@ int http_fn_index(http_request_t* request) {
|
||||
hprintf128(request, "MQTT Stats:CONN: %d PUB: %d RECV: %d ERR: %d </h5>", MQTT_GetConnectEvents(),
|
||||
MQTT_GetPublishEventCounter(), MQTT_GetReceivedEventCounter(), MQTT_GetPublishErrorCounter());
|
||||
|
||||
/* Format current PINS input state for all unused pins */
|
||||
if(CFG_HasFlag(OBK_FLAG_HTTP_PINMONITOR))
|
||||
{
|
||||
for (i=0;i<29;i++)
|
||||
{
|
||||
if ((PIN_GetPinRoleForPinIndex(i) == IOR_None) && (i!=10) && (i!=11))
|
||||
{
|
||||
HAL_PIN_Setup_Input(i);
|
||||
}
|
||||
}
|
||||
|
||||
hprintf128(request,"<h5> PIN States<br>");
|
||||
for (i=0;i<29;i++)
|
||||
{
|
||||
if ((PIN_GetPinRoleForPinIndex(i) != IOR_None) || (i==10) || (i==11))
|
||||
{
|
||||
hprintf128(request,"P%02i: NA ", i);
|
||||
} else {
|
||||
hprintf128(request,"P%02i: %i ", i, (int)HAL_PIN_ReadDigitalInput(i));
|
||||
}
|
||||
if (i%10==9)
|
||||
{
|
||||
hprintf128(request,"<br>");
|
||||
}
|
||||
}
|
||||
hprintf128(request,"</h5>");
|
||||
}
|
||||
/* Format current PINS input state for all unused pins */
|
||||
if (CFG_HasFlag(OBK_FLAG_HTTP_PINMONITOR))
|
||||
{
|
||||
for (i = 0;i < 29;i++)
|
||||
{
|
||||
if ((PIN_GetPinRoleForPinIndex(i) == IOR_None) && (i != 10) && (i != 11))
|
||||
{
|
||||
HAL_PIN_Setup_Input(i);
|
||||
}
|
||||
}
|
||||
|
||||
hprintf128(request, "<h5> PIN States<br>");
|
||||
for (i = 0;i < 29;i++)
|
||||
{
|
||||
if ((PIN_GetPinRoleForPinIndex(i) != IOR_None) || (i == 10) || (i == 11))
|
||||
{
|
||||
hprintf128(request, "P%02i: NA ", i);
|
||||
}
|
||||
else {
|
||||
hprintf128(request, "P%02i: %i ", i, (int)HAL_PIN_ReadDigitalInput(i));
|
||||
}
|
||||
if (i % 10 == 9)
|
||||
{
|
||||
hprintf128(request, "<br>");
|
||||
}
|
||||
}
|
||||
hprintf128(request, "</h5>");
|
||||
}
|
||||
|
||||
// for normal page loads, show the rest of the HTML
|
||||
if (!http_getArg(request->url, "state", tmpA, sizeof(tmpA))) {
|
||||
@ -1223,8 +1224,8 @@ int http_fn_cfg_quick(http_request_t* request) {
|
||||
}
|
||||
|
||||
/// @brief Computes the Relay and PWM count.
|
||||
/// @param relayCount
|
||||
/// @param pwmCount
|
||||
/// @param relayCount Number of relay and LED channels.
|
||||
/// @param pwmCount Number of PWM channels.
|
||||
void get_Relay_PWM_Count(int* relayCount, int* pwmCount) {
|
||||
(*relayCount) = 0;
|
||||
(*pwmCount) = 0;
|
||||
@ -1243,7 +1244,7 @@ void get_Relay_PWM_Count(int* relayCount, int* pwmCount) {
|
||||
bool isLedDriverChipRunning()
|
||||
{
|
||||
#ifndef OBK_DISABLE_ALL_DRIVERS
|
||||
return DRV_IsRunning("SM2135") || DRV_IsRunning("BP5758D");
|
||||
return DRV_IsRunning("SM2135") || DRV_IsRunning("BP5758D") || DRV_IsRunning("TESTLED");
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
@ -1255,8 +1256,9 @@ bool isLedDriverChipRunning()
|
||||
int http_fn_ha_discovery(http_request_t* request) {
|
||||
int i;
|
||||
char topic[32];
|
||||
int relayCount = 0;
|
||||
int pwmCount = 0;
|
||||
int relayCount;
|
||||
int pwmCount;
|
||||
HassDeviceInfo* dev_info = NULL;
|
||||
bool measuringPower = false;
|
||||
|
||||
http_setup(request, httpMimeTypeText);
|
||||
@ -1291,7 +1293,7 @@ int http_fn_ha_discovery(http_request_t* request) {
|
||||
if (relayCount > 0) {
|
||||
for (i = 0; i < CHANNEL_MAX; i++) {
|
||||
if (h_isChannelRelay(i)) {
|
||||
HassDeviceInfo* dev_info = hass_init_relay_device_info(i);
|
||||
dev_info = hass_init_relay_device_info(i);
|
||||
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
@ -1300,23 +1302,29 @@ int http_fn_ha_discovery(http_request_t* request) {
|
||||
|
||||
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);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
else if (pwmCount == 3) {
|
||||
// Enable + RGB control
|
||||
HassDeviceInfo* dev_info = hass_init_light_device_info(ENTITY_LIGHT_RGB, -1);
|
||||
dev_info = hass_init_light_device_info(ENTITY_LIGHT_RGBCW);
|
||||
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
else if (pwmCount > 0) {
|
||||
for (i = 0; i < CHANNEL_MAX; i++) {
|
||||
if (h_isChannelPWM(i)) {
|
||||
HassDeviceInfo* dev_info = hass_init_light_device_info(ENTITY_LIGHT_PWM, i);
|
||||
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
if (pwmCount == 4) {
|
||||
addLogAdv(LOG_ERROR, LOG_FEATURE_HTTP, "4 PWM device not yet handled\r\n");
|
||||
}
|
||||
else if (pwmCount == 3) {
|
||||
// Enable + RGB control
|
||||
dev_info = hass_init_light_device_info(ENTITY_LIGHT_RGB);
|
||||
}
|
||||
else if (pwmCount == 2) {
|
||||
// PWM + Temperature (https://github.com/openshwprojects/OpenBK7231T_App/issues/279)
|
||||
dev_info = hass_init_light_device_info(ENTITY_LIGHT_PWMCW);
|
||||
}
|
||||
else {
|
||||
dev_info = hass_init_light_device_info(ENTITY_LIGHT_PWM);
|
||||
}
|
||||
|
||||
if (dev_info != NULL) {
|
||||
MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1324,7 +1332,7 @@ int http_fn_ha_discovery(http_request_t* request) {
|
||||
if (measuringPower == true) {
|
||||
for (i = 0;i < OBK_NUM_SENSOR_COUNT;i++)
|
||||
{
|
||||
HassDeviceInfo* dev_info = hass_init_light_device_info(ENTITY_SENSOR, 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);
|
||||
hass_free_device_info(dev_info);
|
||||
}
|
||||
@ -1351,8 +1359,8 @@ void http_generate_rgb_cfg(http_request_t* request, const char* clientId) {
|
||||
}
|
||||
|
||||
int http_fn_ha_cfg(http_request_t* request) {
|
||||
int relayCount = 0;
|
||||
int pwmCount = 0;
|
||||
int relayCount;
|
||||
int pwmCount;
|
||||
const char* shortDeviceName;
|
||||
const char* clientId;
|
||||
int i;
|
||||
@ -1543,23 +1551,23 @@ int http_tasmota_json_power(http_request_t* request) {
|
||||
*/
|
||||
int http_tasmota_json_status_SNS(http_request_t* request) {
|
||||
float power, factor, voltage, current;
|
||||
float energy, energy_hour;
|
||||
float energy, energy_hour;
|
||||
|
||||
#ifndef OBK_DISABLE_ALL_DRIVERS
|
||||
factor = 0; // TODO
|
||||
voltage = DRV_GetReading(OBK_VOLTAGE);
|
||||
current = DRV_GetReading(OBK_CURRENT);
|
||||
power = DRV_GetReading(OBK_POWER);
|
||||
energy = DRV_GetReading(OBK_CONSUMPTION_TOTAL);
|
||||
energy_hour = DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR);
|
||||
energy = DRV_GetReading(OBK_CONSUMPTION_TOTAL);
|
||||
energy_hour = DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR);
|
||||
|
||||
#else
|
||||
factor = 0;
|
||||
voltage = 0;
|
||||
current = 0;
|
||||
power = 0;
|
||||
energy = 0;
|
||||
energy_hour = 0;
|
||||
energy = 0;
|
||||
energy_hour = 0;
|
||||
#endif
|
||||
|
||||
hprintf128(request, "{\"StatusSNS\":{\"ENERGY\":{");
|
||||
@ -1567,8 +1575,8 @@ int http_tasmota_json_status_SNS(http_request_t* request) {
|
||||
hprintf128(request, "\"ApparentPower\": 0,\"ReactivePower\": 0,\"Factor\":%f,", factor);
|
||||
hprintf128(request, "\"Voltage\":%f,", voltage);
|
||||
hprintf128(request, "\"Current\":%f,", current);
|
||||
hprintf128(request, "\"ConsumptionTotal\":%f,", energy);
|
||||
hprintf128(request, "\"ConsumptionLastHour\":%f}}}", energy_hour);
|
||||
hprintf128(request, "\"ConsumptionTotal\":%f,", energy);
|
||||
hprintf128(request, "\"ConsumptionLastHour\":%f}}}", energy_hour);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1807,7 +1815,7 @@ const char* g_obk_flagNames[] = {
|
||||
"[MQTT] Broadcast self state on MQTT connect",
|
||||
"[PWM] BK7231 use 600hz instead of 1khz default",
|
||||
"[LED] remember LED driver state (RGBCW, enable, brightness, temperature) after reboot",
|
||||
"[HTTP] Show actual PIN logic level for unconfigured pins",
|
||||
"[HTTP] Show actual PIN logic level for unconfigured pins",
|
||||
"error",
|
||||
"error",
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user