From 51f2281e365a31a531f3ba0930b928e51e9b9b15 Mon Sep 17 00:00:00 2001 From: Tester23 Date: Wed, 27 Sep 2023 09:03:31 +0200 Subject: [PATCH] Temperature_div2 added --- src/httpserver/hass.c | 13 ++-- src/httpserver/hass.h | 2 +- src/httpserver/http_fns.c | 67 +++++++++++++-------- src/new_pins.c | 6 ++ src/new_pins.h | 7 +++ src/selftest/selftest_hass_discovery_base.c | 30 +++++---- 6 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/httpserver/hass.c b/src/httpserver/hass.c index efb41774d..4778d3dfd 100644 --- a/src/httpserver/hass.c +++ b/src/httpserver/hass.c @@ -431,7 +431,7 @@ HassDeviceInfo* hass_init_power_sensor_device_info(int index) { // generate string like "{{ float(value)*0.1|round(2) }}" // {{ float(value)*0.1 }} for value=12 give 1.2000000000000002, using round() to limit the decimal places -char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int decimalPointOffset) { +char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int decimalPointOffset, int divider) { char tmp[8]; int i; @@ -442,7 +442,10 @@ char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int strcat(g_hassBuffer, "0"); } } - strcat(g_hassBuffer, "1|round("); + // usually it's 1 + sprintf(tmp, "%i", divider); + strcat(g_hassBuffer, tmp); + strcat(g_hassBuffer, "|round("); sprintf(tmp, "%i", decimalPlacesForRounding); strcat(g_hassBuffer, tmp); strcat(g_hassBuffer, ") }}"); @@ -475,7 +478,7 @@ HassDeviceInfo* hass_init_light_singleColor_onChannels(int toggle, int dimmer, i /// @param type /// @param channel /// @return -HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int decPlaces, int decOffset) { +HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int decPlaces, int decOffset, int divider) { int i; //Assuming that there is only one DHT setup per device which keeps uniqueid/names simpler @@ -589,9 +592,9 @@ HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int } - if (decPlaces != -1 && decOffset != -1) { + if (decPlaces != -1 && decOffset != -1 && divider != -1) { //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)); + cJSON_AddStringToObject(info->root, "val_tpl", hass_generate_multiplyAndRound_template(decPlaces, decOffset, divider)); } return info; diff --git a/src/httpserver/hass.h b/src/httpserver/hass.h index d019005ed..e3b9a5514 100644 --- a/src/httpserver/hass.h +++ b/src/httpserver/hass.h @@ -96,7 +96,7 @@ HassDeviceInfo* hass_init_light_device_info(ENTITY_TYPE type); HassDeviceInfo* hass_init_power_sensor_device_info(int index); HassDeviceInfo* hass_init_light_singleColor_onChannels(int toggle, int dimmer, int brightness_scale); HassDeviceInfo* hass_init_binary_sensor_device_info(int index, bool bInverse); -HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int decPlaces, int decOffset); +HassDeviceInfo* hass_init_sensor_device_info(ENTITY_TYPE type, int channel, int decPlaces, int decOffset, int divider); const char* hass_build_discovery_json(HassDeviceInfo* info); void hass_free_device_info(HassDeviceInfo* info); char *hass_generate_multiplyAndRound_template(int decimalPlacesForRounding, int decimalPointOffset); diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index b15545c5b..adf9adabd 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -372,6 +372,16 @@ int http_fn_index(http_request_t* request) { hprintf255(request, "Temperature Channel %s value %i C
", CHANNEL_GetLabel(i), iValue); poststr(request, ""); + } + else if (channelType == ChType_Temperature_div2) { + + iValue = CHANNEL_Get(i); + fValue = iValue * 0.5f; + + poststr(request, ""); + hprintf255(request, "Temperature Channel %s value %.2f C
", CHANNEL_GetLabel(i), fValue); + poststr(request, ""); + } else if (channelType == ChType_Temperature_div10) { @@ -1800,11 +1810,11 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { #endif if (measuringBattery == true) { - dev_info = hass_init_sensor_device_info(BATTERY_SENSOR, 0, -1, -1); + dev_info = hass_init_sensor_device_info(BATTERY_SENSOR, 0, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); - dev_info = hass_init_sensor_device_info(BATTERY_VOLTAGE_SENSOR, 0, -1, -1); + dev_info = hass_init_sensor_device_info(BATTERY_VOLTAGE_SENSOR, 0, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1816,14 +1826,14 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { ch = PIN_GetPinChannelForPinIndex(i); // TODO: flags are 32 bit and there are 64 max channels BIT_SET(flagsChannelPublished, ch); - dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, ch, 2, 1); + dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, ch, 2, 1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); ch = PIN_GetPinChannel2ForPinIndex(i); // TODO: flags are 32 bit and there are 64 max channels BIT_SET(flagsChannelPublished, ch); - dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, ch, -1, -1); + dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, ch, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1833,14 +1843,14 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { ch = PIN_GetPinChannelForPinIndex(i); // TODO: flags are 32 bit and there are 64 max channels BIT_SET(flagsChannelPublished, ch); - dev_info = hass_init_sensor_device_info(CO2_SENSOR, ch, -1, -1); + dev_info = hass_init_sensor_device_info(CO2_SENSOR, ch, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); ch = PIN_GetPinChannel2ForPinIndex(i); // TODO: flags are 32 bit and there are 64 max channels BIT_SET(flagsChannelPublished, ch); - dev_info = hass_init_sensor_device_info(TVOC_SENSOR, ch, -1, -1); + dev_info = hass_init_sensor_device_info(TVOC_SENSOR, ch, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1876,7 +1886,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Voltage_div10: { - dev_info = hass_init_sensor_device_info(VOLTAGE_SENSOR, i, 2, 1); + dev_info = hass_init_sensor_device_info(VOLTAGE_SENSOR, i, 2, 1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1885,7 +1895,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Voltage_div100: { - dev_info = hass_init_sensor_device_info(VOLTAGE_SENSOR, i, 2, 2); + dev_info = hass_init_sensor_device_info(VOLTAGE_SENSOR, i, 2, 2, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1894,7 +1904,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_ReadOnlyLowMidHigh: { - dev_info = hass_init_sensor_device_info(READONLYLOWMIDHIGH_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(READONLYLOWMIDHIGH_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1903,7 +1913,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_SmokePercent: { - dev_info = hass_init_sensor_device_info(SMOKE_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(SMOKE_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1912,7 +1922,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Illuminance: { - dev_info = hass_init_sensor_device_info(ILLUMINANCE_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(ILLUMINANCE_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1921,7 +1931,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_ReadOnly: { - dev_info = hass_init_sensor_device_info(CUSTOM_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(CUSTOM_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1930,7 +1940,16 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Temperature: { - dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, i, -1, -1, 1); + MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); + hass_free_device_info(dev_info); + + discoveryQueued = true; + } + break; + case ChType_Temperature_div2: + { + dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, i, 2, 1, 5); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1939,7 +1958,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Temperature_div10: { - dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, i, 2, 1); + dev_info = hass_init_sensor_device_info(TEMPERATURE_SENSOR, i, 2, 1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1948,7 +1967,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Humidity: { - dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1957,7 +1976,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Humidity_div10: { - dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, i, 2, 1); + dev_info = hass_init_sensor_device_info(HUMIDITY_SENSOR, i, 2, 1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1966,7 +1985,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Current_div100: { - dev_info = hass_init_sensor_device_info(CURRENT_SENSOR, i, 3, 2); + dev_info = hass_init_sensor_device_info(CURRENT_SENSOR, i, 3, 2, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1975,7 +1994,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Current_div1000: { - dev_info = hass_init_sensor_device_info(CURRENT_SENSOR, i, 3, 3); + dev_info = hass_init_sensor_device_info(CURRENT_SENSOR, i, 3, 3, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1984,7 +2003,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Power: { - dev_info = hass_init_sensor_device_info(POWER_SENSOR, i, -1, -1); + dev_info = hass_init_sensor_device_info(POWER_SENSOR, i, -1, -1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -1993,7 +2012,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Power_div10: { - dev_info = hass_init_sensor_device_info(POWER_SENSOR, i, 2, 1); + dev_info = hass_init_sensor_device_info(POWER_SENSOR, i, 2, 1, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -2002,7 +2021,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_PowerFactor_div1000: { - dev_info = hass_init_sensor_device_info(POWERFACTOR_SENSOR, i, 4, 3); + dev_info = hass_init_sensor_device_info(POWERFACTOR_SENSOR, i, 4, 3, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -2011,7 +2030,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_Frequency_div100: { - dev_info = hass_init_sensor_device_info(FREQUENCY_SENSOR, i, 3, 2); + dev_info = hass_init_sensor_device_info(FREQUENCY_SENSOR, i, 3, 2, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -2020,7 +2039,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_EnergyTotal_kWh_div100: { - dev_info = hass_init_sensor_device_info(ENERGY_SENSOR, i, 3, 2); + dev_info = hass_init_sensor_device_info(ENERGY_SENSOR, i, 3, 2, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); @@ -2029,7 +2048,7 @@ void doHomeAssistantDiscovery(const char* topic, http_request_t* request) { break; case ChType_EnergyTotal_kWh_div1000: { - dev_info = hass_init_sensor_device_info(ENERGY_SENSOR, i, 3, 3); + dev_info = hass_init_sensor_device_info(ENERGY_SENSOR, i, 3, 3, 1); MQTT_QueuePublish(topic, dev_info->channel, hass_build_discovery_json(dev_info), OBK_PUBLISH_FLAG_RETAIN); hass_free_device_info(dev_info); diff --git a/src/new_pins.c b/src/new_pins.c index da6fc05ec..f6b77f045 100644 --- a/src/new_pins.c +++ b/src/new_pins.c @@ -1872,6 +1872,12 @@ const char* g_channelTypeNames[] = { "Toggle_Inv", "OffOnRemember", "Voltage_div100", + "Temperature_div2", + "error", + "error", + "error", + "error", + "error", "error", "error", "error", diff --git a/src/new_pins.h b/src/new_pins.h index 98cfdf62e..f58459d7d 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -859,6 +859,13 @@ typedef enum channelType_e { //chandetail:"file":"new_pins.h", //chandetail:"driver":""} ChType_Voltage_div100, + //chandetail:{"name":"Temperature_div2", + //chandetail:"title":"TODO", + //chandetail:"descr":"Just like ChType_Temperature_div10, but for multiplied by 0.5.", + //chandetail:"enum":"ChType_Temperature_div2", + //chandetail:"file":"new_pins.h", + //chandetail:"driver":""} + ChType_Temperature_div2, //chandetail:{"name":"Max", //chandetail:"title":"TODO", //chandetail:"descr":"This is the current total number of available channel types.", diff --git a/src/selftest/selftest_hass_discovery_base.c b/src/selftest/selftest_hass_discovery_base.c index 454fef308..c978c2b8c 100644 --- a/src/selftest/selftest_hass_discovery_base.c +++ b/src/selftest/selftest_hass_discovery_base.c @@ -6,19 +6,23 @@ void Test_HassDiscovery_Base() { char tmp[32]; - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1), "{{ float(value)*0.1|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 2), "{{ float(value)*0.01|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 3), "{{ float(value)*0.001|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 4), "{{ float(value)*0.0001|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 5), "{{ float(value)*0.00001|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(1, 0), "{{ float(value)*1|round(1) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 0), "{{ float(value)*1|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(3, 0), "{{ float(value)*1|round(3) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(4, 0), "{{ float(value)*1|round(4) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(1, 5), "{{ float(value)*0.00001|round(1) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 5), "{{ float(value)*0.00001|round(2) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(3, 5), "{{ float(value)*0.00001|round(3) }}"); - SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(4, 5), "{{ float(value)*0.00001|round(4) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1, 1), "{{ float(value)*0.1|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1, 2), "{{ float(value)*0.2|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1, 3), "{{ float(value)*0.3|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1, 4), "{{ float(value)*0.4|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 1, 5), "{{ float(value)*0.5|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 2, 1), "{{ float(value)*0.01|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 3, 1), "{{ float(value)*0.001|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 4, 1), "{{ float(value)*0.0001|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 5, 1), "{{ float(value)*0.00001|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(1, 0, 1), "{{ float(value)*1|round(1) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 0, 1), "{{ float(value)*1|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(3, 0, 1), "{{ float(value)*1|round(3) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(4, 0, 1), "{{ float(value)*1|round(4) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(1, 5, 1), "{{ float(value)*0.00001|round(1) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(2, 5, 1), "{{ float(value)*0.00001|round(2) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(3, 5, 1), "{{ float(value)*0.00001|round(3) }}"); + SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(4, 5, 1), "{{ float(value)*0.00001|round(4) }}"); // causes an error //SELFTEST_ASSERT_STRING(hass_generate_multiplyAndRound_template(3, 5), "{{ float(value)*0.00001|round(4) }}");