Make the Home Assistant MQTT discovery prefix configurable (#793)

This commit is contained in:
Justin Graham 2023-02-03 20:01:22 -06:00 committed by GitHub
parent f40717cd74
commit c11a6be689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 10 deletions

View File

@ -38,6 +38,8 @@
// Replace with your MQTT Broker password
#define DEFAULT_MQTT_PASSWORD ""
#define DEFAULT_HA_DISCOVERY_PREFIX "homeassistant"
// Maximum distance (in meters) to report. Devices that are calculated to be further than this distance in meters will not be reported
#define DEFAULT_MAX_DISTANCE 16

View File

@ -30,6 +30,7 @@ Setup variable declaration macros.
_DECL char buffer[2048];
_DECL String room, id, statusTopic, teleTopic, roomsTopic, setTopic, configTopic;
_DECL AsyncMqttClient mqttClient;
_DECL String homeAssistantDiscoveryPrefix;
_DECL DynamicJsonDocument doc _INIT_N(((2048)));
_DECL String localIp;
_DECL AsyncWebSocket ws _INIT_N((("/ws")));

View File

@ -134,6 +134,7 @@ void setupNetwork() {
mqttUser = AsyncWiFiSettings.pstring("mqtt_user", DEFAULT_MQTT_USER, "Username");
mqttPass = AsyncWiFiSettings.pstring("mqtt_pass", DEFAULT_MQTT_PASSWORD, "Password");
discovery = AsyncWiFiSettings.checkbox("discovery", true, "Send to discovery topic");
homeAssistantDiscoveryPrefix = AsyncWiFiSettings.string("discovery_prefix", DEFAULT_HA_DISCOVERY_PREFIX, "Home Assistant discovery topic prefix");
publishTele = AsyncWiFiSettings.checkbox("pub_tele", true, "Send to telemetry topic");
publishRooms = AsyncWiFiSettings.checkbox("pub_rooms", true, "Send to rooms topic");
publishDevices = AsyncWiFiSettings.checkbox("pub_devices", true, "Send to devices topic");

View File

@ -48,7 +48,7 @@ bool sendConnectivityDiscovery()
doc["pl_off"] = "offline";
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/binary_sensor/espresense_%06lx/connectivity/config", CHIPID);
String discoveryTopic = Sprintf("%s/binary_sensor/espresense_%06lx/connectivity/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID);
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -67,7 +67,7 @@ bool sendTeleBinarySensorDiscovery(const String &name, const String &entityCateg
if (!devClass.isEmpty()) doc["dev_cla"] = devClass;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/binary_sensor/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/binary_sensor/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -87,7 +87,7 @@ bool sendTeleSensorDiscovery(const String &name, const String &entityCategory, c
if (!devClass.isEmpty()) doc["dev_cla"] = devClass;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/sensor/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/sensor/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(),CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -107,7 +107,7 @@ bool sendSensorDiscovery(const String &name, const String &entityCategory, const
doc["frc_upd"] = frcUpdate;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/sensor/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/sensor/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -125,7 +125,7 @@ bool sendBinarySensorDiscovery(const String &name, const String &entityCategory,
if (!devClass.isEmpty()) doc["dev_cla"] = devClass;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/binary_sensor/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/binary_sensor/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -143,7 +143,7 @@ bool sendButtonDiscovery(const String &name, const String &entityCategory)
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/button/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/button/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -161,7 +161,7 @@ bool sendSwitchDiscovery(const String &name, const String &entityCategory)
doc["entity_category"] = entityCategory;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/switch/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/switch/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer, 0);
}
@ -180,7 +180,7 @@ bool sendNumberDiscovery(const String &name, const String &entityCategory)
if (!entityCategory.isEmpty()) doc["entity_category"] = entityCategory;
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/number/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/number/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
@ -201,14 +201,14 @@ bool sendLightDiscovery(const String &name, const String &entityCategory, bool r
serializeJson(doc, buffer);
serializeJson(doc, buffer);
String discoveryTopic = Sprintf("homeassistant/light/espresense_%06lx/%s/config", CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/light/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, true, buffer);
}
bool sendDeleteDiscovery(const String &domain, const String &name)
{
auto slug = slugify(name);
String discoveryTopic = Sprintf("homeassistant/%s/espresense_%06lx/%s/config", domain, CHIPID, slug.c_str());
String discoveryTopic = Sprintf("%s/%s/espresense_%06lx/%s/config", homeAssistantDiscoveryPrefix.c_str(), domain, CHIPID, slug.c_str());
return pub(discoveryTopic.c_str(), 0, false, "");
}