From b33dd58734ad811b3210003fdee396f2c58ce3e7 Mon Sep 17 00:00:00 2001 From: Marcel Koonstra Date: Mon, 21 Oct 2024 15:19:07 +0200 Subject: [PATCH 1/2] Add binary sensor types --- .vehicle_profiles/schema.json | 7 +++++- main/autopid.c | 28 +++++++++++++++++++---- main/autopid.h | 7 ++++++ main/config_server.c | 1 + vehicle_profiles/hyundai/ioniq-2017.json | 9 +++++--- vehicle_profiles/kia/niro-phev.json | 3 ++- vehicle_profiles/kia/nirosoulkona-ev.json | 6 +++-- 7 files changed, 49 insertions(+), 12 deletions(-) diff --git a/.vehicle_profiles/schema.json b/.vehicle_profiles/schema.json index 383be45..be0a3c2 100644 --- a/.vehicle_profiles/schema.json +++ b/.vehicle_profiles/schema.json @@ -44,8 +44,13 @@ "type": "string" }, "class": { - "description": "Class of sensor, See Home Assistant sensor classes", + "description": "Class of sensor, See Home Assistant sensor classes https://www.home-assistant.io/integrations/sensor/#device-class or https://www.home-assistant.io/integrations/binary_sensor/#device-class", "type": "string" + }, + "type": { + "description": "Type of sensor", + "type": "string", + "enum": ["sensor", "binary_sensor"] } } } diff --git a/main/autopid.c b/main/autopid.c index 515d511..00868bc 100644 --- a/main/autopid.c +++ b/main/autopid.c @@ -199,7 +199,8 @@ void autopid_pub_discovery(void) } // Format discovery topic - if (asprintf(&discovery_topic, "homeassistant/sensor/%s/%s/config", + if (asprintf(&discovery_topic, "homeassistant/%s/%s/%s/config", + car.pids[i].parameters[j].sensor_type == BINARY_SENSOR ? "binary_sensor" : "sensor", device_id, car.pids[i].parameters[j].name) == -1) { // Handle error @@ -236,9 +237,6 @@ void autopid_pub_discovery(void) } } - - - void parse_elm327_response(char *buffer, unsigned char *data, uint32_t *data_length) { int k = 0; @@ -627,7 +625,15 @@ static void autopid_task(void *pvParameters) result = round(result * 100.0) / 100.0; // Add the name and result to the JSON object - cJSON_AddNumberToObject(rsp_json, car.pids[i].parameters[j].name, result); + if (car.pids[i].parameters[j].sensor_type == SENSOR) + { + cJSON_AddNumberToObject(rsp_json, car.pids[i].parameters[j].name, result); + } + else if (car.pids[i].parameters[j].sensor_type == BINARY_SENSOR) + { + cJSON_AddStringToObject(rsp_json, car.pids[i].parameters[j].name, result > 0 ? "ON" : "OFF"); + } + ESP_LOGI(TAG, "Expression result, Name: %s: %lf", car.pids[i].parameters[j].name, result); specific_pid_response = 1; } @@ -1182,6 +1188,18 @@ static void autopid_load_car_specific(char* car_mod) { car.pids[i].parameters[j].class = strdup(""); // Assign an empty string if not available } + + // Parse sensor type + cJSON *sensor_type = cJSON_GetObjectItemCaseSensitive(parameter_item, "type"); + if (sensor_type != NULL && cJSON_IsString(sensor_type) && strcmp(sensor_type->valuestring, "binary_sensor") == 0) + { + car.pids[i].parameters[j].sensor_type = BINARY_SENSOR; + } + else + { + car.pids[i].parameters[j].sensor_type = SENSOR; + } + j++; } } diff --git a/main/autopid.h b/main/autopid.h index 334e481..555e19e 100644 --- a/main/autopid.h +++ b/main/autopid.h @@ -55,12 +55,19 @@ typedef struct { uint8_t type; // Log type, could be MQTT or file-based }__attribute__((aligned(1),packed)) pid_req_t ; +typedef enum +{ + SENSOR = 0, + BINARY_SENSOR = 1, +} sensor_type_t; + typedef struct { char *name; char *expression; char *unit; char *class; + sensor_type_t sensor_type; } parameter_data_t; typedef struct diff --git a/main/config_server.c b/main/config_server.c index 3faa879..4785cb4 100644 --- a/main/config_server.c +++ b/main/config_server.c @@ -635,6 +635,7 @@ static esp_err_t load_car_config_handler(httpd_req_t *req) cJSON_AddStringToObject(parameter_details, "class", car->pids[i].parameters[j].class); cJSON_AddStringToObject(parameter_details, "unit", car->pids[i].parameters[j].unit); + cJSON_AddStringToObject(parameter_details, "sensor_type", car->pids[i].parameters[j].sensor_type == BINARY_SENSOR ? "binary_sensor" : "sensor"); cJSON_AddItemToObject(parameters_object, car->pids[i].parameters[j].name, parameter_details); } } diff --git a/vehicle_profiles/hyundai/ioniq-2017.json b/vehicle_profiles/hyundai/ioniq-2017.json index 6442756..03003e8 100644 --- a/vehicle_profiles/hyundai/ioniq-2017.json +++ b/vehicle_profiles/hyundai/ioniq-2017.json @@ -32,19 +32,22 @@ "name": "Charger_Connected", "expression": "B14:5", "unit": "", - "class": "" + "class": "plug", + "type": "binary_sensor" }, { "name": "Charging", "expression": "B14:7", "unit": "", - "class": "" + "class": "battery_charging", + "type": "binary_sensor" }, { "name": "HV_Charger_Connected", "expression": "B14:6", "unit": "", - "class": "" + "class": "plug", + "type": "binary_sensor" } ] } diff --git a/vehicle_profiles/kia/niro-phev.json b/vehicle_profiles/kia/niro-phev.json index 7d7d355..f1b0887 100644 --- a/vehicle_profiles/kia/niro-phev.json +++ b/vehicle_profiles/kia/niro-phev.json @@ -32,7 +32,8 @@ "name": "Charger_Connected", "expression": "B14:7", "unit": "", - "class": "" + "class": "plug", + "type": "binary_sensor" } ] } diff --git a/vehicle_profiles/kia/nirosoulkona-ev.json b/vehicle_profiles/kia/nirosoulkona-ev.json index d0e1bd4..29ed98c 100644 --- a/vehicle_profiles/kia/nirosoulkona-ev.json +++ b/vehicle_profiles/kia/nirosoulkona-ev.json @@ -172,13 +172,15 @@ "name": "Car_Ready", "expression": "B26:3", "unit": "none", - "class": "none" + "class": "none", + "type": "binary_sensor" }, { "name": "Car_ParkBreak", "expression": "B26:5", "unit": "none", - "class": "none" + "class": "none", + "type": "binary_sensor" } ] } From 38f0f2f37bc4ac4752f16a7c97a4a07392ef2b22 Mon Sep 17 00:00:00 2001 From: Marcel Koonstra Date: Sun, 27 Oct 2024 05:56:56 +0100 Subject: [PATCH 2/2] Lowercase binary outputs --- main/autopid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/autopid.c b/main/autopid.c index 00868bc..ca64d36 100644 --- a/main/autopid.c +++ b/main/autopid.c @@ -631,7 +631,7 @@ static void autopid_task(void *pvParameters) } else if (car.pids[i].parameters[j].sensor_type == BINARY_SENSOR) { - cJSON_AddStringToObject(rsp_json, car.pids[i].parameters[j].name, result > 0 ? "ON" : "OFF"); + cJSON_AddStringToObject(rsp_json, car.pids[i].parameters[j].name, result > 0 ? "on" : "off"); } ESP_LOGI(TAG, "Expression result, Name: %s: %lf", car.pids[i].parameters[j].name, result);