Skip to content

Commit c6f2fca

Browse files
authored
[mqtt.homeassistant] fix unbounded growth of config for device_trigger (openhab#17894)
Because of how it shares a channel, whenever openHAB was rebooted and it would first restore the device trigger components from the channel configuration, and then from the MQTT message, it didn't identify it as the same component as before, and so would merge into another instance of itself. My Things.json is normally 13MB, and had grown to 545MB, and my openHAB was constantly having memory issues! So now just make sure we only keep unique information, which will automatically clean up anyone in a bad state. Signed-off-by: Cody Cutrer <[email protected]>
1 parent 334c909 commit c6f2fca

File tree

1 file changed

+12
-6
lines changed
  • bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component

1 file changed

+12
-6
lines changed

bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTrigger.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,22 @@ public boolean merge(DeviceTrigger other) {
111111
newConfiguration.put("nodeid", currentConfiguration.get("nodeid"));
112112
Object objectIdObject = currentConfiguration.get("objectid");
113113
if (objectIdObject instanceof String objectIdString) {
114-
newConfiguration.put("objectid", List.of(objectIdString, other.getHaID().objectID));
114+
if (!objectIdString.equals(other.getHaID().objectID)) {
115+
newConfiguration.put("objectid", List.of(objectIdString, other.getHaID().objectID));
116+
}
115117
} else if (objectIdObject instanceof List<?> objectIdList) {
116-
newConfiguration.put("objectid",
117-
Stream.concat(objectIdList.stream(), Stream.of(other.getHaID().objectID)).toList());
118+
newConfiguration.put("objectid", Stream.concat(objectIdList.stream(), Stream.of(other.getHaID().objectID))
119+
.sorted().distinct().toList());
118120
}
119121
Object configObject = currentConfiguration.get("config");
120122
if (configObject instanceof String configString) {
121-
newConfiguration.put("config", List.of(configString, other.getChannelConfigurationJson()));
123+
if (!configString.equals(other.getChannelConfigurationJson())) {
124+
newConfiguration.put("config", List.of(configString, other.getChannelConfigurationJson()));
125+
}
122126
} else if (configObject instanceof List<?> configList) {
123127
newConfiguration.put("config",
124-
Stream.concat(configList.stream(), Stream.of(other.getChannelConfigurationJson())).toList());
128+
Stream.concat(configList.stream(), Stream.of(other.getChannelConfigurationJson())).sorted()
129+
.distinct().toList());
125130
}
126131

127132
// Append payload to allowed values
@@ -130,7 +135,8 @@ public boolean merge(DeviceTrigger other) {
130135
// Need to accept anything
131136
value = new TextValue();
132137
} else {
133-
String[] newValues = Stream.concat(payloads.stream(), Stream.of(otherPayload)).toArray(String[]::new);
138+
String[] newValues = Stream.concat(payloads.stream(), Stream.of(otherPayload)).distinct()
139+
.toArray(String[]::new);
134140
value = new TextValue(newValues);
135141
}
136142

0 commit comments

Comments
 (0)