Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary sensor types #244

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .vehicle_profiles/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
}
}
Expand Down
28 changes: 23 additions & 5 deletions main/autopid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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++;
}
}
Expand Down
7 changes: 7 additions & 0 deletions main/autopid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions main/config_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 6 additions & 3 deletions vehicle_profiles/hyundai/ioniq-2017.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion vehicle_profiles/kia/niro-phev.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"name": "Charger_Connected",
"expression": "B14:7",
"unit": "",
"class": ""
"class": "plug",
"type": "binary_sensor"
}
]
}
Expand Down
6 changes: 4 additions & 2 deletions vehicle_profiles/kia/nirosoulkona-ev.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down