Skip to content

Commit

Permalink
Filter discovery messages against HASS classes list (#1127)
Browse files Browse the repository at this point in the history
* Filter the discovery message content against HASS list of classes
  • Loading branch information
1technophile authored Jan 12, 2022
1 parent e6003c8 commit 71d5622
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,28 @@ void createDiscovery(const char* sensor_type,
sensor["stat_t"] = state_topic;
}

// We check if the class belongs to HAAS class list
bool HASSClass = false;
int num_classes = sizeof(availableHASSClasses) / sizeof(availableHASSClasses[0]);
for (int i = 0; i < num_classes; i++) { // see class list and size into config_mqttDiscovery.h
if (strcmp(availableHASSClasses[i], device_class) == 0) {
HASSClass = true;
}
}

sensor["name"] = s_name; //name
sensor["uniq_id"] = unique_id; //unique_id
if (retainCmd)
sensor["retain"] = retainCmd; // Retain command
if (device_class[0])
if (device_class[0] && HASSClass)
sensor["dev_cla"] = device_class; //device_class
if (value_template[0])
sensor["val_tpl"] = value_template; //value_template
if (payload_on[0])
sensor["pl_on"] = payload_on; // payload_on
if (payload_off[0])
sensor["pl_off"] = payload_off; //payload_off
if (unit_of_meas[0])
if (unit_of_meas[0] && HASSClass)
sensor["unit_of_meas"] = unit_of_meas; //unit_of_measurement*/
if (off_delay != 0)
sensor["off_delay"] = off_delay; //off_delay
Expand Down
17 changes: 17 additions & 0 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,21 @@ void announceDeviceTrigger(bool use_gateway_info,
# define stateClassTotalIncreasing "total_increasing"
#endif

// From https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L225
// List of classes available in Home Assistant
const char* availableHASSClasses[] = {"battery",
"carbon_monoxide",
"carbon_dioxide",
"humidity",
"illuminance",
"signal_strength",
"temperature",
"timestamp",
"pressure",
"power",
"current",
"energy",
"power_factor",
"voltage"};

#endif

0 comments on commit 71d5622

Please sign in to comment.