Browse Source

Merge pull request #1547 from abmantis/ha_discovery_devices

Add unique id and device for better HA UI integration
rules-rpn
Xose Pérez 5 years ago
committed by GitHub
parent
commit
dd6e446320
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 6 deletions
  1. +26
    -6
      code/espurna/homeassistant.ino

+ 26
- 6
code/espurna/homeassistant.ino View File

@ -38,10 +38,9 @@ void _haSendMagnitude(unsigned char i, JsonObject& config) {
config.set("platform", "mqtt"); config.set("platform", "mqtt");
config["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false); config["state_topic"] = mqttTopic(magnitudeTopicIndex(i).c_str(), false);
config["unit_of_measurement"] = magnitudeUnits(type); config["unit_of_measurement"] = magnitudeUnits(type);
} }
void _haSendMagnitudes() {
void _haSendMagnitudes(const JsonObject& deviceConfig) {
for (unsigned char i=0; i<magnitudeCount(); i++) { for (unsigned char i=0; i<magnitudeCount(); i++) {
@ -55,6 +54,9 @@ void _haSendMagnitudes() {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& config = jsonBuffer.createObject(); JsonObject& config = jsonBuffer.createObject();
_haSendMagnitude(i, config); _haSendMagnitude(i, config);
config["uniq_id"] = getIdentifier() + "_" + magnitudeTopic(magnitudeType(i)) + "_" + String(i);
config["device"] = deviceConfig;
config.printTo(output); config.printTo(output);
jsonBuffer.clear(); jsonBuffer.clear();
} }
@ -115,7 +117,7 @@ void _haSendSwitch(unsigned char i, JsonObject& config) {
} }
void _haSendSwitches() {
void _haSendSwitches(const JsonObject& deviceConfig) {
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER)) #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
String type = String("light"); String type = String("light");
@ -135,6 +137,9 @@ void _haSendSwitches() {
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
JsonObject& config = jsonBuffer.createObject(); JsonObject& config = jsonBuffer.createObject();
_haSendSwitch(i, config); _haSendSwitch(i, config);
config["uniq_id"] = getIdentifier() + "_" + type + "_" + String(i);
config["device"] = deviceConfig;
config.printTo(output); config.printTo(output);
jsonBuffer.clear(); jsonBuffer.clear();
} }
@ -241,7 +246,16 @@ void _haDumpConfig(std::function<void(String&)> printer, bool wrapJson = false)
} }
#endif #endif
}
void _haGetDeviceConfig(JsonObject& config) {
String identifier = getIdentifier();
config.createNestedArray("identifiers").add(identifier);
config["name"] = getSetting("desc", getSetting("hostname"));
config["manufacturer"] = String(MANUFACTURER);
config["model"] = String(DEVICE);
config["sw_version"] = String(APP_NAME) + " " + String(APP_VERSION) + " (" + getCoreVersion() + ")";
} }
void _haSend() { void _haSend() {
@ -254,12 +268,18 @@ void _haSend() {
DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n")); DEBUG_MSG_P(PSTR("[HA] Sending autodiscovery MQTT message\n"));
// Get common device config
DynamicJsonBuffer jsonBuffer;
JsonObject& deviceConfig = jsonBuffer.createObject();
_haGetDeviceConfig(deviceConfig);
// Send messages // Send messages
_haSendSwitches();
_haSendSwitches(deviceConfig);
#if SENSOR_SUPPORT #if SENSOR_SUPPORT
_haSendMagnitudes();
_haSendMagnitudes(deviceConfig);
#endif #endif
jsonBuffer.clear();
_haSendFlag = false; _haSendFlag = false;
} }


Loading…
Cancel
Save