@@ -55,16 +55,12 @@ export default class HomeassistantService {
55
55
let topicName : string = '' ;
56
56
let deviceName = containerName ;
57
57
58
- if ( ! prefix ) {
59
- topicName = `${ formatedImage } _${ formatedTag } ` ;
60
- } else {
61
- topicName = `${ prefix } _${ formatedImage } _${ formatedTag } ` ;
62
- }
63
-
64
58
if ( ! prefix ) {
65
59
deviceName = containerName ;
60
+ topicName = containerName ;
66
61
} else {
67
62
deviceName = `${ prefix } _${ containerName } ` ;
63
+ topicName = `${ prefix } _${ containerName } ` ;
68
64
}
69
65
70
66
const discoveryPrefix = config ?. mqtt ?. discoveryPrefix
@@ -127,7 +123,7 @@ export default class HomeassistantService {
127
123
topic = `${ discoveryPrefix } /button/${ topicName } /docker_manual_restart/config` ;
128
124
payload = {
129
125
name : "Manual Restart" ,
130
- unique_id : `${ image } _ ${ tag } _manual_restart` ,
126
+ unique_id : `${ deviceName } _manual_restart` ,
131
127
command_topic : `${ config . mqtt . topic } /restart` ,
132
128
command_template : JSON . stringify ( { containerId : container . Id } ) ,
133
129
availability : {
@@ -140,7 +136,7 @@ export default class HomeassistantService {
140
136
name : deviceName ,
141
137
sw_version : packageJson . version ,
142
138
sa : "Docker" ,
143
- identifiers : [ `${ image } _ ${ tag } ` ] ,
139
+ identifiers : [ `${ deviceName } ` ] ,
144
140
} ,
145
141
icon : "mdi:restart" ,
146
142
} ;
@@ -171,7 +167,7 @@ export default class HomeassistantService {
171
167
topic = `${ discoveryPrefix } /button/${ topicName } /docker_manual_update/config` ;
172
168
payload = {
173
169
name : "Manual Update" ,
174
- unique_id : `${ image } _ ${ tag } _manual_update` ,
170
+ unique_id : `${ deviceName } _manual_update` ,
175
171
command_topic : `${ config . mqtt . topic } /manualUpdate` ,
176
172
command_template : JSON . stringify ( { containerId : container . Id } ) ,
177
173
availability : {
@@ -184,7 +180,7 @@ export default class HomeassistantService {
184
180
name : deviceName ,
185
181
sw_version : packageJson . version ,
186
182
sa : "Docker" ,
187
- identifiers : [ `${ image } _ ${ tag } ` ] ,
183
+ identifiers : [ `${ deviceName } ` ] ,
188
184
} ,
189
185
icon : "mdi:arrow-up-bold-circle" ,
190
186
} ;
@@ -263,10 +259,10 @@ export default class HomeassistantService {
263
259
const formatedImage = image . replace ( / [ \/ . : ; , + * ? @ ^ $ % # ! & " ' ` | < > { } \[ \] ( ) - \s \u0000 - \u001F \u007F ] / g, "_" ) ;
264
260
265
261
return {
266
- object_id : prefix ? `${ prefix } / ${ image } ${ name } ` : ` ${ image } ${ name } `,
262
+ object_id : `${ deviceName } ${ name } ` ,
267
263
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 } ` ,
270
266
device_class : deviceClass ,
271
267
value_template : `{{ value_json.${ valueName } }}` ,
272
268
availability :
@@ -282,7 +278,7 @@ export default class HomeassistantService {
282
278
name : deviceName ,
283
279
sw_version : packageJson . version ,
284
280
sa : "Docker" ,
285
- identifiers : [ `${ image } _ ${ tag } ` ] ,
281
+ identifiers : [ `${ deviceName } ` ] ,
286
282
} ,
287
283
icon : icon ,
288
284
} ;
@@ -300,10 +296,10 @@ export default class HomeassistantService {
300
296
const formatedImage = image . replace ( / [ \/ . : ; , + * ? @ ^ $ % # ! & " ' ` | < > { } \[ \] ( ) - \s \u0000 - \u001F \u007F ] / g, "_" ) ;
301
297
302
298
return {
303
- object_id : prefix ? `${ prefix } / ${ image } ${ name } ` : ` ${ image } ${ name } `,
299
+ object_id : `${ deviceName } ${ name } ` ,
304
300
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` ,
307
303
device_class : "firmware" ,
308
304
availability : [
309
305
{
@@ -318,7 +314,7 @@ export default class HomeassistantService {
318
314
name : deviceName ,
319
315
sw_version : packageJson . version ,
320
316
sa : "Docker" ,
321
- identifiers : [ `${ image } _ ${ tag } ` ] ,
317
+ identifiers : [ `${ deviceName } ` ] ,
322
318
} ,
323
319
icon : "mdi:arrow-up-bold-circle" ,
324
320
entity_picture : "https://github.com/MichelFR/MqDockerUp/raw/main/assets/logo_200x200.png" ,
@@ -339,11 +335,17 @@ export default class HomeassistantService {
339
335
container = DockerService . docker . getContainer ( container ) . inspect ( ) ;
340
336
}
341
337
338
+ const jsonString = JSON . stringify ( container ) ;
339
+ console . log ( jsonString ) ;
340
+
342
341
const image = container . Config . Image . split ( ":" ) [ 0 ] ;
343
342
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 } ` ;
344
346
345
347
// Update entity payload
346
- const updateTopic = `${ config . mqtt . topic } /${ formatedImage } /update` ;
348
+ const updateTopic = `${ config . mqtt . topic } /${ deviceName } /update` ;
347
349
let updatePayload : any ;
348
350
349
351
updatePayload = {
@@ -371,9 +373,13 @@ export default class HomeassistantService {
371
373
372
374
const image = container ?. Config ?. Image ?. split ( ":" ) [ 0 ] ;
373
375
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 } ` ;
374
380
375
381
// Update entity payload
376
- const updateTopic = `${ config . mqtt . topic } /${ formatedImage } /update` ;
382
+ const updateTopic = `${ config . mqtt . topic } /${ deviceName } /update` ;
377
383
let updatePayload : any ;
378
384
379
385
updatePayload = {
@@ -398,11 +404,15 @@ export default class HomeassistantService {
398
404
const formatedImage = image . replace ( / [ \/ . : ; , + * ? @ ^ $ % # ! & " ' ` | < > { } \[ \] ( ) - \s \u0000 - \u001F \u007F ] / g, "_" ) ;
399
405
const tag = container . Config . Image . split ( ":" ) [ 1 ] || "latest" ;
400
406
const imageInfo = await DockerService . getImageInfo ( image + ":" + tag ) ;
407
+ const containerName = container . Name . substring ( 1 ) ;
401
408
const currentDigest = imageInfo ?. RepoDigests [ 0 ] ?. split ( ":" ) [ 1 ] ;
402
409
let newDigest = null ;
403
410
404
411
newDigest = await DockerService . getImageNewDigest ( image , tag , currentDigest ) ;
405
412
413
+ const prefix = config ?. main . prefix || "" ;
414
+ const deviceName = prefix == "" ? `${ containerName } ` : `${ prefix } _${ containerName } ` ;
415
+
406
416
if ( currentDigest ) {
407
417
if ( log ) {
408
418
if ( currentDigest && newDigest ) {
@@ -422,7 +432,7 @@ export default class HomeassistantService {
422
432
}
423
433
424
434
// Update entity payload
425
- const updateTopic = `${ config . mqtt . topic } /${ formatedImage } /update` ;
435
+ const updateTopic = `${ config . mqtt . topic } /${ deviceName } /update` ;
426
436
let updatePayload : any ;
427
437
if ( haLegacy ) {
428
438
updatePayload = {
@@ -487,6 +497,9 @@ export default class HomeassistantService {
487
497
const tag = container . Config . Image . split ( ":" ) [ 1 ] || "latest" ;
488
498
const containerName = container . Name . substring ( 1 ) ;
489
499
500
+ const prefix = config ?. main . prefix || "" ;
501
+ const deviceName = prefix == "" ? `${ containerName } ` : `${ prefix } _${ containerName } ` ;
502
+
490
503
let dockerPorts = "" ;
491
504
if ( container . HostConfig . PortBindings ) {
492
505
for ( const [ key , value ] of Object . entries ( container . HostConfig . PortBindings ) ) {
@@ -503,7 +516,7 @@ export default class HomeassistantService {
503
516
504
517
let registry = await DockerService . getImageRegistryName ( image ) ;
505
518
506
- const topic = `${ config . mqtt . topic } /${ formatedImage } ` ;
519
+ const topic = `${ config . mqtt . topic } /${ deviceName } ` ;
507
520
const payload = {
508
521
dockerImage : image ,
509
522
dockerTag : tag ,
0 commit comments