Skip to content

Commit

Permalink
Temperature_div2 added
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 27, 2023
1 parent 0e006b4 commit 51f2281
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 43 deletions.
13 changes: 8 additions & 5 deletions src/httpserver/hass.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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, ") }}");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/httpserver/hass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 43 additions & 24 deletions src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,16 @@ int http_fn_index(http_request_t* request) {
hprintf255(request, "Temperature Channel %s value %i C<br>", CHANNEL_GetLabel(i), iValue);
poststr(request, "</td></tr>");

}
else if (channelType == ChType_Temperature_div2) {

iValue = CHANNEL_Get(i);
fValue = iValue * 0.5f;

poststr(request, "<tr><td>");
hprintf255(request, "Temperature Channel %s value %.2f C<br>", CHANNEL_GetLabel(i), fValue);
poststr(request, "</td></tr>");

}
else if (channelType == ChType_Temperature_div10) {

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/new_pins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,12 @@ const char* g_channelTypeNames[] = {
"Toggle_Inv",
"OffOnRemember",
"Voltage_div100",
"Temperature_div2",
"error",
"error",
"error",
"error",
"error",
"error",
"error",
"error",
Expand Down
7 changes: 7 additions & 0 deletions src/new_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
30 changes: 17 additions & 13 deletions src/selftest/selftest_hass_discovery_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}");
Expand Down

0 comments on commit 51f2281

Please sign in to comment.