Skip to content

Commit 411d7d9

Browse files
committed
Fix multi-container and multi-host overwrites
1 parent 49f5038 commit 411d7d9

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/services/HomeassistantService.ts

+35-22
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,12 @@ export default class HomeassistantService {
5555
let topicName: string = '';
5656
let deviceName = containerName;
5757

58-
if (!prefix) {
59-
topicName = `${formatedImage}_${formatedTag}`;
60-
} else {
61-
topicName = `${prefix}_${formatedImage}_${formatedTag}`;
62-
}
63-
6458
if (!prefix) {
6559
deviceName = containerName;
60+
topicName = containerName;
6661
} else {
6762
deviceName = `${prefix}_${containerName}`;
63+
topicName = `${prefix}_${containerName}`;
6864
}
6965

7066
const discoveryPrefix = config?.mqtt?.discoveryPrefix
@@ -127,7 +123,7 @@ export default class HomeassistantService {
127123
topic = `${discoveryPrefix}/button/${topicName}/docker_manual_restart/config`;
128124
payload = {
129125
name: "Manual Restart",
130-
unique_id: `${image}_${tag}_manual_restart`,
126+
unique_id: `${deviceName}_manual_restart`,
131127
command_topic: `${config.mqtt.topic}/restart`,
132128
command_template: JSON.stringify({containerId: container.Id}),
133129
availability: {
@@ -140,7 +136,7 @@ export default class HomeassistantService {
140136
name: deviceName,
141137
sw_version: packageJson.version,
142138
sa: "Docker",
143-
identifiers: [`${image}_${tag}`],
139+
identifiers: [`${deviceName}`],
144140
},
145141
icon: "mdi:restart",
146142
};
@@ -171,7 +167,7 @@ export default class HomeassistantService {
171167
topic = `${discoveryPrefix}/button/${topicName}/docker_manual_update/config`;
172168
payload = {
173169
name: "Manual Update",
174-
unique_id: `${image}_${tag}_manual_update`,
170+
unique_id: `${deviceName}_manual_update`,
175171
command_topic: `${config.mqtt.topic}/manualUpdate`,
176172
command_template: JSON.stringify({containerId: container.Id}),
177173
availability: {
@@ -184,7 +180,7 @@ export default class HomeassistantService {
184180
name: deviceName,
185181
sw_version: packageJson.version,
186182
sa: "Docker",
187-
identifiers: [`${image}_${tag}`],
183+
identifiers: [`${deviceName}`],
188184
},
189185
icon: "mdi:arrow-up-bold-circle",
190186
};
@@ -263,10 +259,10 @@ export default class HomeassistantService {
263259
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");
264260

265261
return {
266-
object_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
262+
object_id: `${deviceName} ${name}`,
267263
name: `${name}`,
268-
unique_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
269-
state_topic: `${config.mqtt.topic}/${formatedImage}`,
264+
unique_id: `${deviceName} ${name}`,
265+
state_topic: `${config.mqtt.topic}/${deviceName}`,
270266
device_class: deviceClass,
271267
value_template: `{{ value_json.${valueName} }}`,
272268
availability:
@@ -282,7 +278,7 @@ export default class HomeassistantService {
282278
name: deviceName,
283279
sw_version: packageJson.version,
284280
sa: "Docker",
285-
identifiers: [`${image}_${tag}`],
281+
identifiers: [`${deviceName}`],
286282
},
287283
icon: icon,
288284
};
@@ -300,10 +296,10 @@ export default class HomeassistantService {
300296
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");
301297

302298
return {
303-
object_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
299+
object_id: `${deviceName} ${name}`,
304300
name: `${name}`,
305-
unique_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
306-
state_topic: `${config.mqtt.topic}/${formatedImage}/update`,
301+
unique_id: deviceName,
302+
state_topic: `${config.mqtt.topic}/${deviceName}/update`,
307303
device_class: "firmware",
308304
availability: [
309305
{
@@ -318,7 +314,7 @@ export default class HomeassistantService {
318314
name: deviceName,
319315
sw_version: packageJson.version,
320316
sa: "Docker",
321-
identifiers: [`${image}_${tag}`],
317+
identifiers: [`${deviceName}`],
322318
},
323319
icon: "mdi:arrow-up-bold-circle",
324320
entity_picture: "https://github.com/MichelFR/MqDockerUp/raw/main/assets/logo_200x200.png",
@@ -339,11 +335,17 @@ export default class HomeassistantService {
339335
container = DockerService.docker.getContainer(container).inspect();
340336
}
341337

338+
const jsonString = JSON.stringify(container);
339+
console.log(jsonString);
340+
342341
const image = container.Config.Image.split(":")[0];
343342
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");
343+
const containerName = container.Name.substring(1);
344+
const prefix = config?.main.prefix || "";
345+
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;
344346

345347
// Update entity payload
346-
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
348+
const updateTopic = `${config.mqtt.topic}/${deviceName}/update`;
347349
let updatePayload: any;
348350

349351
updatePayload = {
@@ -371,9 +373,13 @@ export default class HomeassistantService {
371373

372374
const image = container?.Config?.Image?.split(":")[0];
373375
const formatedImage = image?.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");
376+
const containerName = container.Name.substring(1);
377+
378+
const prefix = config?.main.prefix || "";
379+
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;
374380

375381
// Update entity payload
376-
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
382+
const updateTopic = `${config.mqtt.topic}/${deviceName}/update`;
377383
let updatePayload: any;
378384

379385
updatePayload = {
@@ -398,11 +404,15 @@ export default class HomeassistantService {
398404
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");
399405
const tag = container.Config.Image.split(":")[1] || "latest";
400406
const imageInfo = await DockerService.getImageInfo(image + ":" + tag);
407+
const containerName = container.Name.substring(1);
401408
const currentDigest = imageInfo?.RepoDigests[0]?.split(":")[1];
402409
let newDigest = null;
403410

404411
newDigest = await DockerService.getImageNewDigest(image, tag, currentDigest);
405412

413+
const prefix = config?.main.prefix || "";
414+
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;
415+
406416
if (currentDigest) {
407417
if (log) {
408418
if (currentDigest && newDigest) {
@@ -422,7 +432,7 @@ export default class HomeassistantService {
422432
}
423433

424434
// Update entity payload
425-
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
435+
const updateTopic = `${config.mqtt.topic}/${deviceName}/update`;
426436
let updatePayload: any;
427437
if (haLegacy) {
428438
updatePayload = {
@@ -487,6 +497,9 @@ export default class HomeassistantService {
487497
const tag = container.Config.Image.split(":")[1] || "latest";
488498
const containerName = container.Name.substring(1);
489499

500+
const prefix = config?.main.prefix || "";
501+
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;
502+
490503
let dockerPorts = "";
491504
if (container.HostConfig.PortBindings) {
492505
for (const [key, value] of Object.entries(container.HostConfig.PortBindings)) {
@@ -503,7 +516,7 @@ export default class HomeassistantService {
503516

504517
let registry = await DockerService.getImageRegistryName(image);
505518

506-
const topic = `${config.mqtt.topic}/${formatedImage}`;
519+
const topic = `${config.mqtt.topic}/${deviceName}`;
507520
const payload = {
508521
dockerImage: image,
509522
dockerTag: tag,

0 commit comments

Comments
 (0)