Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-container and multi-host overwrites #424

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions src/services/HomeassistantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@ export default class HomeassistantService {
let topicName: string = '';
let deviceName = containerName;

if (!prefix) {
topicName = `${formatedImage}_${formatedTag}`;
} else {
topicName = `${prefix}_${formatedImage}_${formatedTag}`;
}

if (!prefix) {
deviceName = containerName;
topicName = containerName;
} else {
deviceName = `${prefix}_${containerName}`;
topicName = `${prefix}_${containerName}`;
}

const discoveryPrefix = config?.mqtt?.discoveryPrefix
Expand Down Expand Up @@ -127,7 +123,7 @@ export default class HomeassistantService {
topic = `${discoveryPrefix}/button/${topicName}/docker_manual_restart/config`;
payload = {
name: "Manual Restart",
unique_id: `${image}_${tag}_manual_restart`,
unique_id: `${deviceName}_manual_restart`,
command_topic: `${config.mqtt.topic}/restart`,
command_template: JSON.stringify({containerId: container.Id}),
availability: {
Expand All @@ -140,7 +136,7 @@ export default class HomeassistantService {
name: deviceName,
sw_version: packageJson.version,
sa: "Docker",
identifiers: [`${image}_${tag}`],
identifiers: [`${deviceName}`],
},
icon: "mdi:restart",
};
Expand Down Expand Up @@ -171,7 +167,7 @@ export default class HomeassistantService {
topic = `${discoveryPrefix}/button/${topicName}/docker_manual_update/config`;
payload = {
name: "Manual Update",
unique_id: `${image}_${tag}_manual_update`,
unique_id: `${deviceName}_manual_update`,
command_topic: `${config.mqtt.topic}/manualUpdate`,
command_template: JSON.stringify({containerId: container.Id}),
availability: {
Expand All @@ -184,7 +180,7 @@ export default class HomeassistantService {
name: deviceName,
sw_version: packageJson.version,
sa: "Docker",
identifiers: [`${image}_${tag}`],
identifiers: [`${deviceName}`],
},
icon: "mdi:arrow-up-bold-circle",
};
Expand Down Expand Up @@ -263,10 +259,10 @@ export default class HomeassistantService {
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");

return {
object_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
object_id: `${deviceName} ${name}`,
name: `${name}`,
unique_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
state_topic: `${config.mqtt.topic}/${formatedImage}`,
unique_id: `${deviceName} ${name}`,
state_topic: `${config.mqtt.topic}/${deviceName}`,
device_class: deviceClass,
value_template: `{{ value_json.${valueName} }}`,
availability:
Expand All @@ -282,7 +278,7 @@ export default class HomeassistantService {
name: deviceName,
sw_version: packageJson.version,
sa: "Docker",
identifiers: [`${image}_${tag}`],
identifiers: [`${deviceName}`],
},
icon: icon,
};
Expand All @@ -300,10 +296,10 @@ export default class HomeassistantService {
const formatedImage = image.replace(/[\/.:;,+*?@^$%#!&"'`|<>{}\[\]()-\s\u0000-\u001F\u007F]/g, "_");

return {
object_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
object_id: `${deviceName} ${name}`,
name: `${name}`,
unique_id: prefix ? `${prefix}/${image} ${name}` : `${image} ${name}`,
state_topic: `${config.mqtt.topic}/${formatedImage}/update`,
unique_id: deviceName,
state_topic: `${config.mqtt.topic}/${deviceName}/update`,
device_class: "firmware",
availability: [
{
Expand All @@ -318,7 +314,7 @@ export default class HomeassistantService {
name: deviceName,
sw_version: packageJson.version,
sa: "Docker",
identifiers: [`${image}_${tag}`],
identifiers: [`${deviceName}`],
},
icon: "mdi:arrow-up-bold-circle",
entity_picture: "https://github.com/MichelFR/MqDockerUp/raw/main/assets/logo_200x200.png",
Expand All @@ -339,11 +335,17 @@ export default class HomeassistantService {
container = DockerService.docker.getContainer(container).inspect();
}

const jsonString = JSON.stringify(container);
console.log(jsonString);

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

// Update entity payload
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
const updateTopic = `${config.mqtt.topic}/${deviceName}/update`;
let updatePayload: any;

updatePayload = {
Expand Down Expand Up @@ -371,9 +373,13 @@ export default class HomeassistantService {

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

const prefix = config?.main.prefix || "";
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;

// Update entity payload
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
const updateTopic = `${config.mqtt.topic}/${deviceName}/update`;
let updatePayload: any;

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

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

const prefix = config?.main.prefix || "";
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;

if (currentDigest) {
if (log) {
if (currentDigest && newDigest) {
Expand All @@ -422,7 +432,7 @@ export default class HomeassistantService {
}

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

const prefix = config?.main.prefix || "";
const deviceName = prefix == "" ? `${containerName}` : `${prefix}_${containerName}`;

let dockerPorts = "";
if (container.HostConfig.PortBindings) {
for (const [key, value] of Object.entries(container.HostConfig.PortBindings)) {
Expand All @@ -503,7 +516,7 @@ export default class HomeassistantService {

let registry = await DockerService.getImageRegistryName(image);

const topic = `${config.mqtt.topic}/${formatedImage}`;
const topic = `${config.mqtt.topic}/${deviceName}`;
const payload = {
dockerImage: image,
dockerTag: tag,
Expand Down