diff --git a/src/defaults.h b/src/defaults.h index 346a214..025403a 100644 --- a/src/defaults.h +++ b/src/defaults.h @@ -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 diff --git a/src/globals.h b/src/globals.h index eb06e3f..f5119ec 100644 --- a/src/globals.h +++ b/src/globals.h @@ -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"))); diff --git a/src/main.cpp b/src/main.cpp index a213c2c..38f80be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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"); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 5eeb9ff..7f80618 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -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, ""); }