mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-21 13:25:44 +00:00
Fixed most of the issues
This commit is contained in:
@ -25,10 +25,13 @@ Abbreviated node names - https://www.home-assistant.io/docs/mqtt/discovery/
|
||||
|
||||
*/
|
||||
|
||||
//Buffer for populates values with cJSON_Add* calls
|
||||
//We are values based on CFG_GetShortDeviceName and clientId so it needs to be bigger than them. +64 for light/switch/etc.
|
||||
static char g_hassBuffer[CGF_MQTT_CLIENT_ID_SIZE + 64];
|
||||
|
||||
/// @brief Populates HomeAssistant unique id for the entity.
|
||||
/// @param type Entity type
|
||||
/// @param index Entity index
|
||||
/// @param index Entity index (Ignored for RGB)
|
||||
/// @param uniq_id Array to populate (should be of size HASS_UNIQUE_ID_SIZE)
|
||||
void hass_populate_unique_id(ENTITY_TYPE type, int index, char *uniq_id){
|
||||
//https://developers.home-assistant.io/docs/entity_registry_index/#unique-id-requirements
|
||||
@ -103,56 +106,9 @@ cJSON *hass_build_device_node(cJSON *ids) {
|
||||
return dev;
|
||||
}
|
||||
|
||||
/// @brief Populates common values for HomeAssistant device discovery.
|
||||
/// @param root
|
||||
/// @param type Entity type
|
||||
/// @param index
|
||||
/// @param unique_id
|
||||
/// @param payload_on
|
||||
/// @param payload_off
|
||||
void hass_populate_common(cJSON *root, ENTITY_TYPE type, int index, char *unique_id, char *payload_on, char *payload_off){
|
||||
const char *clientId = CFG_GetMQTTClientId();
|
||||
|
||||
//We are stuffing CFG_GetShortDeviceName and clientId into tmp so it needs to be bigger than them. +16 for light/switch/etc.
|
||||
char tmp[CGF_MQTT_CLIENT_ID_SIZE + 16];
|
||||
|
||||
//A device can have both relay and PWM and they would need to have separate names.
|
||||
switch(type){
|
||||
case ENTITY_LIGHT_PWM:
|
||||
sprintf(tmp,"%s light %i",CFG_GetShortDeviceName(),index);
|
||||
break;
|
||||
case ENTITY_LIGHT_RGB:
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
//There can only be one RGB so we can skip including index in the name
|
||||
sprintf(tmp,"%s light",CFG_GetShortDeviceName());
|
||||
break;
|
||||
case ENTITY_RELAY:
|
||||
//state_topic and command_topic are only for relay
|
||||
sprintf(tmp,"%s/%i/get",clientId,index);
|
||||
cJSON_AddStringToObject(root, "stat_t", tmp); //state_topic
|
||||
|
||||
sprintf(tmp,"%s/%i/set",clientId,index);
|
||||
cJSON_AddStringToObject(root, "cmd_t", tmp); //command_topic
|
||||
|
||||
sprintf(tmp,"%s switch %i",CFG_GetShortDeviceName(),index);
|
||||
break;
|
||||
case ENTITY_SENSOR:
|
||||
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ENTITY_SENSOR not yet supported");
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(root, "name", tmp);
|
||||
|
||||
sprintf(tmp,"%s/connected",clientId);
|
||||
cJSON_AddStringToObject(root, "avty_t", tmp); //availability_topic
|
||||
|
||||
cJSON_AddStringToObject(root, "pl_on", payload_on); //payload_on
|
||||
cJSON_AddStringToObject(root, "pl_off", payload_off); //payload_off
|
||||
cJSON_AddStringToObject(root, "uniq_id", unique_id); //unique_id
|
||||
}
|
||||
|
||||
/// @brief Initializes HomeAssistant device discovery storage.
|
||||
/// @brief Initializes HomeAssistant device discovery storage with common values.
|
||||
/// @param type
|
||||
/// @param index
|
||||
/// @param index Ignored for RGB
|
||||
/// @param payload_on
|
||||
/// @param payload_off
|
||||
/// @return
|
||||
@ -169,11 +125,32 @@ HassDeviceInfo *hass_init_device_info(ENTITY_TYPE type, int index, char *payload
|
||||
cJSON_AddItemToArray(info->ids, cJSON_CreateString(CFG_GetDeviceName()));
|
||||
|
||||
info->device = hass_build_device_node(info->ids);
|
||||
info->root = cJSON_CreateObject();
|
||||
|
||||
info->root = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(info->root, "dev", info->device); //device
|
||||
|
||||
hass_populate_common(info->root, type, index, info->unique_id, payload_on, payload_off);
|
||||
switch(type){
|
||||
case ENTITY_LIGHT_PWM:
|
||||
case ENTITY_RELAY:
|
||||
sprintf(g_hassBuffer,"%s %i",CFG_GetShortDeviceName(),index);
|
||||
break;
|
||||
case ENTITY_LIGHT_RGB:
|
||||
case ENTITY_LIGHT_RGBCW:
|
||||
//There can only be one RGB so we can skip including index in the name
|
||||
sprintf(g_hassBuffer,"%s",CFG_GetShortDeviceName());
|
||||
break;
|
||||
case ENTITY_SENSOR:
|
||||
addLogAdv(LOG_ERROR, LOG_FEATURE_HASS, "ENTITY_SENSOR not yet supported");
|
||||
}
|
||||
cJSON_AddStringToObject(info->root, "name", g_hassBuffer);
|
||||
|
||||
sprintf(g_hassBuffer,"%s/connected",CFG_GetMQTTClientId());
|
||||
cJSON_AddStringToObject(info->root, "avty_t", g_hassBuffer); //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
|
||||
cJSON_AddStringToObject(info->root, "uniq_id", info->unique_id); //unique_id
|
||||
|
||||
addLogAdv(LOG_DEBUG, LOG_FEATURE_HASS, "root=%p", info->root);
|
||||
return info;
|
||||
}
|
||||
@ -182,12 +159,23 @@ HassDeviceInfo *hass_init_device_info(ENTITY_TYPE type, int index, char *payload
|
||||
/// @param index
|
||||
/// @return
|
||||
HassDeviceInfo *hass_init_relay_device_info(int index){
|
||||
return hass_init_device_info(ENTITY_RELAY, index, "1", "0");
|
||||
const char *clientId = CFG_GetMQTTClientId();
|
||||
|
||||
HassDeviceInfo *info = hass_init_device_info(ENTITY_RELAY, index, "1", "0");
|
||||
cJSON_AddNumberToObject(info->root, "qos", 1);
|
||||
|
||||
sprintf(g_hassBuffer,"%s/%i/get",clientId,index);
|
||||
cJSON_AddStringToObject(info->root, "stat_t", g_hassBuffer); //state_topic
|
||||
|
||||
sprintf(g_hassBuffer,"%s/%i/set",clientId,index);
|
||||
cJSON_AddStringToObject(info->root, "cmd_t", g_hassBuffer); //command_topic
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
/// @brief Initializes HomeAssistant light device discovery storage.
|
||||
/// @param type
|
||||
/// @param index
|
||||
/// @param index Ignored for RGB
|
||||
/// @return
|
||||
HassDeviceInfo *hass_init_light_device_info(ENTITY_TYPE type, int index){
|
||||
char tmp[CGF_MQTT_CLIENT_ID_SIZE + 64]; //Used to generate values based on CFG_GetMQTTClientId
|
||||
@ -199,30 +187,31 @@ HassDeviceInfo *hass_init_light_device_info(ENTITY_TYPE type, int index){
|
||||
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)}}");
|
||||
cJSON_AddStringToObject(info->root, "rgb_cmd_tpl","{{'#%02x%02x%02x0000'|format(red, green, blue)}}"); //rgb_command_template
|
||||
|
||||
sprintf(tmp,"cmnd/%s/led_basecolor_rgb",clientId);
|
||||
cJSON_AddStringToObject(info->root, "rgb_stat_t", tmp);
|
||||
cJSON_AddStringToObject(info->root, "rgb_stat_t", tmp); //rgb_state_topic
|
||||
|
||||
sprintf(tmp,"cmnd/%s/led_basecolor_rgb",clientId);
|
||||
cJSON_AddStringToObject(info->root, "rgb_cmd_t", tmp);
|
||||
cJSON_AddStringToObject(info->root, "rgb_cmd_t", tmp); //rgb_command_topic
|
||||
|
||||
sprintf(tmp,"cmnd/%s/led_enableAll",clientId);
|
||||
cJSON_AddStringToObject(info->root, "cmd_t", tmp);
|
||||
cJSON_AddStringToObject(info->root, "cmd_t", tmp); //command_topic
|
||||
|
||||
sprintf(tmp,"cmnd/%s/led_dimmer",clientId);
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", tmp);
|
||||
cJSON_AddNumberToObject(info->root, "bri_scl", 100);
|
||||
cJSON_AddStringToObject(info->root, "bri_val_tpl", "{{value_json.Dimmer}}");
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", tmp); //brightness_command_topic
|
||||
|
||||
cJSON_AddNumberToObject(info->root, "bri_scl", 100); //brightness_scale
|
||||
cJSON_AddStringToObject(info->root, "bri_val_tpl", "{{value_json.Dimmer}}"); //brightness_value_template
|
||||
|
||||
if (type == ENTITY_LIGHT_RGBCW){
|
||||
sprintf(tmp,"cmnd/%s/led_temperature",clientId);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_cmd_t", tmp);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_cmd_t", tmp); //color_temp_command_topic
|
||||
|
||||
sprintf(tmp,"cmnd/%s/ctr",clientId);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_stat_t", tmp);
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_stat_t", tmp); //color_temp_state_topic
|
||||
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_val_tpl", "{{value_json.CT}}");
|
||||
cJSON_AddStringToObject(info->root, "clr_temp_val_tpl", "{{value_json.CT}}"); //color_temp_value_template
|
||||
}
|
||||
|
||||
break;
|
||||
@ -231,11 +220,18 @@ HassDeviceInfo *hass_init_light_device_info(ENTITY_TYPE type, int index){
|
||||
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_AddNumberToObject(info->root, "bri_scl", 99); //brightness_scale
|
||||
cJSON_AddBoolToObject(info->root, "opt", cJSON_True); //optimistic
|
||||
cJSON_AddNumberToObject(info->root, "qos", 1);
|
||||
|
||||
sprintf(tmp,"%s/%i/set",clientId,index);
|
||||
cJSON_AddStringToObject(info->root, "bri_cmd_t", tmp); //brightness_command_topic
|
||||
|
||||
sprintf(g_hassBuffer,"%s/%i/get",clientId,index);
|
||||
cJSON_AddStringToObject(info->root, "stat_t", g_hassBuffer); //state_topic
|
||||
|
||||
sprintf(g_hassBuffer,"%s/%i/set",clientId,index);
|
||||
cJSON_AddStringToObject(info->root, "cmd_t", g_hassBuffer); //command_topic
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user