Skip to content

Commit b33dd58

Browse files
author
Marcel Koonstra
committed
Add binary sensor types
1 parent b81bb75 commit b33dd58

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

.vehicle_profiles/schema.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@
4444
"type": "string"
4545
},
4646
"class": {
47-
"description": "Class of sensor, See Home Assistant sensor classes",
47+
"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",
4848
"type": "string"
49+
},
50+
"type": {
51+
"description": "Type of sensor",
52+
"type": "string",
53+
"enum": ["sensor", "binary_sensor"]
4954
}
5055
}
5156
}

main/autopid.c

+23-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ void autopid_pub_discovery(void)
199199
}
200200

201201
// Format discovery topic
202-
if (asprintf(&discovery_topic, "homeassistant/sensor/%s/%s/config",
202+
if (asprintf(&discovery_topic, "homeassistant/%s/%s/%s/config",
203+
car.pids[i].parameters[j].sensor_type == BINARY_SENSOR ? "binary_sensor" : "sensor",
203204
device_id, car.pids[i].parameters[j].name) == -1)
204205
{
205206
// Handle error
@@ -236,9 +237,6 @@ void autopid_pub_discovery(void)
236237
}
237238
}
238239

239-
240-
241-
242240
void parse_elm327_response(char *buffer, unsigned char *data, uint32_t *data_length)
243241
{
244242
int k = 0;
@@ -627,7 +625,15 @@ static void autopid_task(void *pvParameters)
627625

628626
result = round(result * 100.0) / 100.0;
629627
// Add the name and result to the JSON object
630-
cJSON_AddNumberToObject(rsp_json, car.pids[i].parameters[j].name, result);
628+
if (car.pids[i].parameters[j].sensor_type == SENSOR)
629+
{
630+
cJSON_AddNumberToObject(rsp_json, car.pids[i].parameters[j].name, result);
631+
}
632+
else if (car.pids[i].parameters[j].sensor_type == BINARY_SENSOR)
633+
{
634+
cJSON_AddStringToObject(rsp_json, car.pids[i].parameters[j].name, result > 0 ? "ON" : "OFF");
635+
}
636+
631637
ESP_LOGI(TAG, "Expression result, Name: %s: %lf", car.pids[i].parameters[j].name, result);
632638
specific_pid_response = 1;
633639
}
@@ -1182,6 +1188,18 @@ static void autopid_load_car_specific(char* car_mod)
11821188
{
11831189
car.pids[i].parameters[j].class = strdup(""); // Assign an empty string if not available
11841190
}
1191+
1192+
// Parse sensor type
1193+
cJSON *sensor_type = cJSON_GetObjectItemCaseSensitive(parameter_item, "type");
1194+
if (sensor_type != NULL && cJSON_IsString(sensor_type) && strcmp(sensor_type->valuestring, "binary_sensor") == 0)
1195+
{
1196+
car.pids[i].parameters[j].sensor_type = BINARY_SENSOR;
1197+
}
1198+
else
1199+
{
1200+
car.pids[i].parameters[j].sensor_type = SENSOR;
1201+
}
1202+
11851203
j++;
11861204
}
11871205
}

main/autopid.h

+7
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ typedef struct {
5555
uint8_t type; // Log type, could be MQTT or file-based
5656
}__attribute__((aligned(1),packed)) pid_req_t ;
5757

58+
typedef enum
59+
{
60+
SENSOR = 0,
61+
BINARY_SENSOR = 1,
62+
} sensor_type_t;
63+
5864
typedef struct
5965
{
6066
char *name;
6167
char *expression;
6268
char *unit;
6369
char *class;
70+
sensor_type_t sensor_type;
6471
} parameter_data_t;
6572

6673
typedef struct

main/config_server.c

+1
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ static esp_err_t load_car_config_handler(httpd_req_t *req)
635635

636636
cJSON_AddStringToObject(parameter_details, "class", car->pids[i].parameters[j].class);
637637
cJSON_AddStringToObject(parameter_details, "unit", car->pids[i].parameters[j].unit);
638+
cJSON_AddStringToObject(parameter_details, "sensor_type", car->pids[i].parameters[j].sensor_type == BINARY_SENSOR ? "binary_sensor" : "sensor");
638639
cJSON_AddItemToObject(parameters_object, car->pids[i].parameters[j].name, parameter_details);
639640
}
640641
}

vehicle_profiles/hyundai/ioniq-2017.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,22 @@
3232
"name": "Charger_Connected",
3333
"expression": "B14:5",
3434
"unit": "",
35-
"class": ""
35+
"class": "plug",
36+
"type": "binary_sensor"
3637
},
3738
{
3839
"name": "Charging",
3940
"expression": "B14:7",
4041
"unit": "",
41-
"class": ""
42+
"class": "battery_charging",
43+
"type": "binary_sensor"
4244
},
4345
{
4446
"name": "HV_Charger_Connected",
4547
"expression": "B14:6",
4648
"unit": "",
47-
"class": ""
49+
"class": "plug",
50+
"type": "binary_sensor"
4851
}
4952
]
5053
}

vehicle_profiles/kia/niro-phev.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"name": "Charger_Connected",
3333
"expression": "B14:7",
3434
"unit": "",
35-
"class": ""
35+
"class": "plug",
36+
"type": "binary_sensor"
3637
}
3738
]
3839
}

vehicle_profiles/kia/nirosoulkona-ev.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,15 @@
172172
"name": "Car_Ready",
173173
"expression": "B26:3",
174174
"unit": "none",
175-
"class": "none"
175+
"class": "none",
176+
"type": "binary_sensor"
176177
},
177178
{
178179
"name": "Car_ParkBreak",
179180
"expression": "B26:5",
180181
"unit": "none",
181-
"class": "none"
182+
"class": "none",
183+
"type": "binary_sensor"
182184
}
183185
]
184186
}

0 commit comments

Comments
 (0)