channel type - Percent

This commit is contained in:
Tester23
2025-05-31 01:18:37 +02:00
parent d6f7678dc6
commit b1b41f4a66
5 changed files with 42 additions and 4 deletions

View File

@ -128,6 +128,9 @@ void hass_populate_unique_id(ENTITY_TYPE type, int index, char* uniq_id, int ase
case HASS_IP:
sprintf(uniq_id, "%s_ip", longDeviceName);
break;
case HASS_PERCENT:
sprintf(uniq_id, "%s_%s_%d", longDeviceName, "number", index);
break;
default:
// TODO: USE type here as well?
// If type is not set, and we use "sensor" naming, we can easily make collision
@ -181,6 +184,9 @@ void hass_populate_device_config_channel(ENTITY_TYPE type, char* uniq_id, HassDe
case HASS_FAN:
sprintf(info->channel, "fan/%s/config", uniq_id);
break;
case HASS_PERCENT:
sprintf(info->channel, "number/%s/config", uniq_id);
break;
case SMOKE_SENSOR:
case CO2_SENSOR:
case TVOC_SENSOR:
@ -806,6 +812,29 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int
//https://developers.home-assistant.io/docs/core/entity/sensor/#available-device-classes
switch (type) {
case HASS_PERCENT:
// backlog setChannelType 5 Percent; scheduleHADiscovery
cJSON_AddStringToObject(info->root, "unit_of_meas", "%");
cJSON_AddStringToObject(info->root, "stat_cla", "measurement");
// State topic for reading the percentage value
sprintf(g_hassBuffer, "~/%d/get", channel);
cJSON_AddStringToObject(info->root, "stat_t", g_hassBuffer);
// Command topic for writing the percentage value
sprintf(g_hassBuffer, "~/%d/set", channel);
cJSON_AddStringToObject(info->root, "cmd_t", g_hassBuffer);
// Value template to ensure the value is between 0 and 100
//cJSON_AddStringToObject(info->root, "val_tpl", "{{ value | float | round(0) | max(0) | min(100) }}");
// Add number-specific properties for the slider
cJSON_AddStringToObject(info->root, "mode", "slider"); // Use slider mode in HA
cJSON_AddNumberToObject(info->root, "min", 0); // Minimum value
cJSON_AddNumberToObject(info->root, "max", 100); // Maximum value
cJSON_AddNumberToObject(info->root, "step", 1); // Step value for slider
break;
case TEMPERATURE_SENSOR:
cJSON_AddStringToObject(info->root, "dev_cla", "temperature");
cJSON_AddStringToObject(info->root, "unit_of_meas", "°C");
@ -972,7 +1001,7 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int
}
if (decPlaces != -1 && decOffset != -1 && divider != -1) {
if (decPlaces != -1 && decOffset != -1 && divider != -1 && type != HASS_PERCENT) {
//https://www.home-assistant.io/integrations/sensor.mqtt/ refers to value_template (val_tpl)
cJSON_AddStringToObject(info->root, "val_tpl", hass_generate_multiplyAndRound_template(decPlaces, decOffset, divider));
}

View File

@ -97,7 +97,8 @@ typedef enum {
BATTERY_CHANNEL_SENSOR,
HASS_HVAC,
HASS_FAN,
HASS_SELECT
HASS_SELECT,
HASS_PERCENT,
} ENTITY_TYPE;
//unique_id is defined in hass_populate_unique_id and is based on CFG_GetDeviceName() whose size is CGF_DEVICE_NAME_SIZE.

View File

@ -579,7 +579,9 @@ int http_fn_index(http_request_t* request) {
else if (h_isChannelRelay(i) || channelType == ChType_Toggle || channelType == ChType_Toggle_Inv) {
// HANDLED ABOVE in previous loop
}
else if ((bRawPWMs && h_isChannelPWM(i)) || (channelType == ChType_Dimmer) || (channelType == ChType_Dimmer256) || (channelType == ChType_Dimmer1000)) {
else if ((bRawPWMs && h_isChannelPWM(i)) ||
(channelType == ChType_Dimmer) || (channelType == ChType_Dimmer256) || (channelType == ChType_Dimmer1000)
|| channelType == ChType_Percent) {
int maxValue;
// PWM and dimmer both use a slider control
inputName = h_isChannelPWM(i) ? "pwm" : "dim";
@ -2143,6 +2145,11 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) {
dev_info = hass_init_sensor_device_info(FREQUENCY_SENSOR, i, 3, 2, 1);
}
break;
case ChType_Percent:
{
dev_info = hass_init_sensor_device_info(HASS_PERCENT, i, 3, 2, 1);
}
break;
case ChType_Frequency_div1000:
{
dev_info = hass_init_sensor_device_info(FREQUENCY_SENSOR, i, 4, 3, 1);