ref: move and rename mqtt topics

This commit is contained in:
leukipp
2025-07-08 19:40:43 +02:00
parent 0d66d104fb
commit cab2052477
+234 -311
View File
@@ -36,6 +36,7 @@ const init = async () => {
// Init globals
INTEGRATION.node = `rpi_${deviceId}`;
INTEGRATION.root = `${app.getName()}/${INTEGRATION.node}`;
INTEGRATION.discovery = discovery;
INTEGRATION.device = {
name: `TouchKio ${deviceName}`,
@@ -58,27 +59,27 @@ const init = async () => {
console.log(`MQTT Connected: ${url.toString()}\n`);
// Init client controls
initShutdown(INTEGRATION.client);
initReboot(INTEGRATION.client);
initRefresh(INTEGRATION.client);
initKiosk(INTEGRATION.client);
initDisplay(INTEGRATION.client);
initKeyboard(INTEGRATION.client);
initPageNumber(INTEGRATION.client);
initPageUrl(INTEGRATION.client);
initShutdown();
initReboot();
initRefresh();
initKiosk();
initDisplay();
initKeyboard();
initPageNumber();
initPageUrl();
// Init client sensors
initModel(INTEGRATION.client);
initSerialNumber(INTEGRATION.client);
initHostName(INTEGRATION.client);
initUpTime(INTEGRATION.client);
initMemorySize(INTEGRATION.client);
initMemoryUsage(INTEGRATION.client);
initProcessorUsage(INTEGRATION.client);
initProcessorTemperature(INTEGRATION.client);
initPackageUpgrades(INTEGRATION.client);
initHeartbeat(INTEGRATION.client);
initLastActive(INTEGRATION.client);
initModel();
initSerialNumber();
initHostName();
initUpTime();
initMemorySize();
initMemoryUsage();
initProcessorUsage();
initProcessorTemperature();
initPackageUpgrades();
initHeartbeat();
initLastActive();
// Integration initialized
INTEGRATION.initialized = true;
@@ -91,16 +92,16 @@ const init = async () => {
// Update sensor states from notifiers
HARDWARE.display.notifiers.push(() => {
updateDisplay(INTEGRATION.client);
updateDisplay();
});
HARDWARE.keyboard.notifiers.push(() => {
updateKeyboard(INTEGRATION.client);
updateKeyboard();
});
// Update time sensors periodically (30s)
setInterval(() => {
updateHeartbeat(INTEGRATION.client);
updateLastActive(INTEGRATION.client);
updateHeartbeat();
updateLastActive();
}, 30 * 1000);
// Update system sensors periodically (1min)
@@ -110,7 +111,7 @@ const init = async () => {
// Update package sensors periodically (60min)
setInterval(() => {
updatePackageUpgrades(INTEGRATION.client);
updatePackageUpgrades();
}, 3600 * 1000);
return true;
@@ -125,23 +126,67 @@ const update = async () => {
}
// Update client sensors
updateKiosk(INTEGRATION.client);
updatePageNumber(INTEGRATION.client);
updatePageUrl(INTEGRATION.client);
updateUpTime(INTEGRATION.client);
updateLastActive(INTEGRATION.client);
updateMemoryUsage(INTEGRATION.client);
updateProcessorUsage(INTEGRATION.client);
updateProcessorTemperature(INTEGRATION.client);
updateKiosk();
updatePageNumber();
updatePageUrl();
updateUpTime();
updateLastActive();
updateMemoryUsage();
updateProcessorUsage();
updateProcessorTemperature();
};
/**
* Publishes the auto-discovery config via the mqtt connection.
*
* @param {string} type - The entity type name.
* @param {Object} config - The configuration object.
* @returns {Object} Instance of the mqtt client.
*/
const publishConfig = (type, config) => {
if (type === null || config === null) {
return INTEGRATION.client;
}
const path = config.unique_id.replace(`${INTEGRATION.node}_`, "");
const root = `${INTEGRATION.discovery}/${type}/${INTEGRATION.node}/${path}/config`;
return INTEGRATION.client.publish(root, JSON.stringify(config), { qos: 1, retain: true });
};
/**
* Publishes the sensor attributes via the mqtt connection.
*
* @param {string} path - The entity path name.
* @param {Object} attributes - The attributes object.
* @returns {Object} Instance of the mqtt client.
*/
const publishAttributes = (path, attributes) => {
if (path === null || attributes === null) {
return INTEGRATION.client;
}
const root = `${INTEGRATION.root}/${path}/attributes`;
return INTEGRATION.client.publish(root, JSON.stringify(attributes), { qos: 1, retain: true });
};
/**
* Publishes the sensor state via the mqtt connection.
*
* @param {string} path - The entity path name.
* @param {string|number} state - The state value.
* @returns {Object} Instance of the mqtt client.
*/
const publishState = (path, state) => {
if (path === null || state === null) {
return INTEGRATION.client;
}
const root = `${INTEGRATION.root}/${path}/state`;
return INTEGRATION.client.publish(root, `${state}`, { qos: 1, retain: true });
};
/**
* Initializes the shutdown button and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initShutdown = (client) => {
const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/shutdown`;
const initShutdown = () => {
const root = `${INTEGRATION.root}/shutdown`;
const config = {
name: "Shutdown",
unique_id: `${INTEGRATION.node}_shutdown`,
@@ -149,25 +194,22 @@ const initShutdown = (client) => {
icon: "mdi:power",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("button", config)
.on("message", (topic, message) => {
if (topic === `${root}/execute`) {
if (topic === config.command_topic) {
console.log("Shutdown system...");
hardware.setDisplayStatus("ON");
hardware.shutdownSystem();
}
})
.subscribe(`${root}/execute`);
.subscribe(config.command_topic);
};
/**
* Initializes the reboot button and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initReboot = (client) => {
const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/reboot`;
const initReboot = () => {
const root = `${INTEGRATION.root}/reboot`;
const config = {
name: "Reboot",
unique_id: `${INTEGRATION.node}_reboot`,
@@ -175,25 +217,22 @@ const initReboot = (client) => {
icon: "mdi:restart",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("button", config)
.on("message", (topic, message) => {
if (topic === `${root}/execute`) {
if (topic === config.command_topic) {
console.log("Rebooting system...");
hardware.setDisplayStatus("ON");
hardware.rebootSystem();
}
})
.subscribe(`${root}/execute`);
.subscribe(config.command_topic);
};
/**
* Initializes the refresh button and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initRefresh = (client) => {
const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/refresh`;
const initRefresh = () => {
const root = `${INTEGRATION.root}/refresh`;
const config = {
name: "Refresh",
unique_id: `${INTEGRATION.node}_refresh`,
@@ -201,39 +240,35 @@ const initRefresh = (client) => {
icon: "mdi:web-refresh",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("button", config)
.on("message", (topic, message) => {
if (topic === `${root}/execute`) {
if (topic === config.command_topic) {
console.log("Refreshing webview...");
hardware.setDisplayStatus("ON");
WEBVIEW.views[WEBVIEW.viewActive].webContents.reloadIgnoringCache();
}
})
.subscribe(`${root}/execute`);
.subscribe(config.command_topic);
};
/**
* Initializes the kiosk select status and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initKiosk = (client) => {
const root = `${INTEGRATION.discovery}/select/${INTEGRATION.node}/kiosk`;
const initKiosk = () => {
const root = `${INTEGRATION.root}/kiosk`;
const config = {
name: "Kiosk",
unique_id: `${INTEGRATION.node}_kiosk`,
command_topic: `${root}/set`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
options: ["Framed", "Fullscreen", "Maximized", "Minimized", "Terminated"],
icon: "mdi:overscan",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("select", config)
.on("message", (topic, message) => {
if (topic === `${root}/set`) {
if (topic === config.command_topic) {
const status = message.toString();
console.log("Set Kiosk Status:", status);
hardware.setDisplayStatus("ON");
@@ -263,106 +298,88 @@ const initKiosk = (client) => {
}
}
})
.subscribe(`${root}/set`);
updateKiosk(client);
.subscribe(config.command_topic);
updateKiosk();
};
/**
* Updates the kiosk status via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateKiosk = (client) => {
const updateKiosk = () => {
const kiosk = WEBVIEW.status;
const root = `${INTEGRATION.discovery}/select/${INTEGRATION.node}/kiosk`;
if (kiosk !== null) {
client.publish(`${root}/status`, `${kiosk}`, { qos: 1, retain: true });
}
publishState("kiosk", kiosk);
};
/**
* Initializes the display status, brightness and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initDisplay = (client) => {
const initDisplay = () => {
if (!HARDWARE.support.displayStatus) {
return;
}
const root = `${INTEGRATION.discovery}/light/${INTEGRATION.node}/display`;
const root = `${INTEGRATION.root}/display`;
const config = {
name: "Display",
unique_id: `${INTEGRATION.node}_display`,
command_topic: `${root}/set`,
state_topic: `${root}/status`,
command_topic: `${root}/power/set`,
state_topic: `${root}/power/state`,
icon: "mdi:monitor-shimmer",
platform: "light",
device: INTEGRATION.device,
...(HARDWARE.support.displayBrightness && {
brightness_command_topic: `${root}/brightness/set`,
brightness_state_topic: `${root}/brightness/state`,
brightness_scale: 100,
}),
};
if (HARDWARE.support.displayBrightness) {
config.brightness_scale = 100;
config.brightness_command_topic = `${root}/brightness/set`;
config.brightness_state_topic = `${root}/brightness/status`;
}
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("light", config)
.on("message", (topic, message) => {
if (topic === `${root}/set`) {
if (topic === config.command_topic) {
const status = message.toString();
console.log("Set Display Status:", status);
hardware.setDisplayStatus(status);
hardware.update();
} else if (topic === `${root}/brightness/set`) {
} else if (topic === config.brightness_command_topic) {
const brightness = parseInt(message, 10);
console.log("Set Display Brightness:", brightness);
hardware.setDisplayBrightness(brightness);
hardware.update();
}
})
.subscribe(`${root}/set`)
.subscribe(`${root}/brightness/set`);
updateDisplay(client);
.subscribe(config.command_topic)
.subscribe(config.brightness_command_topic);
updateDisplay();
};
/**
* Updates the display status, brightness via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateDisplay = (client) => {
const updateDisplay = () => {
const status = hardware.getDisplayStatus();
const brightness = hardware.getDisplayBrightness();
const root = `${INTEGRATION.discovery}/light/${INTEGRATION.node}/display`;
if (status !== null) {
client.publish(`${root}/status`, `${status}`, { qos: 1, retain: true });
}
if (brightness !== null) {
client.publish(`${root}/brightness/status`, `${brightness}`, { qos: 1, retain: true });
}
publishState("display/power", status);
publishState("display/brightness", brightness);
};
/**
* Initializes the keyboard visibility and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initKeyboard = (client) => {
const initKeyboard = () => {
if (!HARDWARE.support.keyboardVisibility) {
return;
}
const root = `${INTEGRATION.discovery}/switch/${INTEGRATION.node}/keyboard`;
const root = `${INTEGRATION.root}/keyboard`;
const config = {
name: "Keyboard",
unique_id: `${INTEGRATION.node}_keyboard`,
command_topic: `${root}/set`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
icon: "mdi:keyboard-close-outline",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("switch", config)
.on("message", (topic, message) => {
if (topic === `${root}/set`) {
if (topic === config.command_topic) {
const status = message.toString();
console.log("Set Keyboard Visibility:", status);
hardware.setKeyboardVisibility(status);
@@ -380,36 +397,29 @@ const initKeyboard = (client) => {
}
}
})
.subscribe(`${root}/set`);
updateKeyboard(client);
.subscribe(config.command_topic);
updateKeyboard();
};
/**
* Updates the keyboard visibility via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateKeyboard = (client) => {
const updateKeyboard = () => {
const visibility = hardware.getKeyboardVisibility();
const root = `${INTEGRATION.discovery}/switch/${INTEGRATION.node}/keyboard`;
if (visibility !== null) {
client.publish(`${root}/status`, `${visibility}`, { qos: 1, retain: true });
}
publishState("keyboard", visibility);
};
/**
* Initializes the page number and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initPageNumber = (client) => {
const root = `${INTEGRATION.discovery}/number/${INTEGRATION.node}/page_number`;
const initPageNumber = () => {
const root = `${INTEGRATION.root}/page_number`;
const config = {
name: "View",
name: "Page Number",
unique_id: `${INTEGRATION.node}_page_number`,
command_topic: `${root}/set`,
state_topic: `${root}/status`,
value_template: "{{ value }}",
state_topic: `${root}/state`,
value_template: "{{ value | int }}",
mode: "box",
min: 1,
max: WEBVIEW.viewUrls.length - 1,
@@ -417,439 +427,352 @@ const initPageNumber = (client) => {
icon: "mdi:page-next",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("number", config)
.on("message", (topic, message) => {
if (topic === `${root}/set`) {
if (topic === config.command_topic) {
const number = parseInt(message, 10);
console.log("Set Page Number:", number);
WEBVIEW.viewActive = number;
}
})
.subscribe(`${root}/set`);
updatePageNumber(client);
.subscribe(config.command_topic);
updatePageNumber();
};
/**
* Updates the page number via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updatePageNumber = (client) => {
const updatePageNumber = () => {
const pageNumber = WEBVIEW.viewActive;
const root = `${INTEGRATION.discovery}/number/${INTEGRATION.node}/page_number`;
if (pageNumber !== null) {
client.publish(`${root}/status`, `${pageNumber}`, { qos: 1, retain: true });
}
publishState("page_number", pageNumber);
};
/**
* Initializes the page url and handles the execute logic.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initPageUrl = (client) => {
const root = `${INTEGRATION.discovery}/text/${INTEGRATION.node}/page_url`;
const initPageUrl = () => {
const root = `${INTEGRATION.root}/page_url`;
const config = {
name: "Url",
name: "Page Url",
unique_id: `${INTEGRATION.node}_page_url`,
command_topic: `${root}/set`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
pattern: "https?://.*",
icon: "mdi:web",
device: INTEGRATION.device,
};
client
.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true })
publishConfig("text", config)
.on("message", (topic, message) => {
if (topic === `${root}/set`) {
if (topic === config.command_topic) {
const url = message.toString();
console.log("Set Page Url:", url);
WEBVIEW.views[WEBVIEW.viewActive].webContents.loadURL(url);
}
})
.subscribe(`${root}/set`);
updatePageUrl(client);
.subscribe(config.command_topic);
updatePageUrl();
};
/**
* Updates the page url via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updatePageUrl = (client) => {
const updatePageUrl = () => {
const defaultUrl = WEBVIEW.viewUrls[WEBVIEW.viewActive];
const currentUrl = WEBVIEW.views[WEBVIEW.viewActive].webContents.getURL();
const pageUrl = !currentUrl || currentUrl.startsWith("data:") ? defaultUrl : currentUrl;
const root = `${INTEGRATION.discovery}/text/${INTEGRATION.node}/page_url`;
if (pageUrl !== null) {
client.publish(`${root}/status`, `${pageUrl}`, { qos: 1, retain: true });
}
publishState("page_url", pageUrl);
};
/**
* Initializes the model sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initModel = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/model`;
const initModel = () => {
const root = `${INTEGRATION.root}/model`;
const config = {
name: "Model",
unique_id: `${INTEGRATION.node}_model`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
icon: "mdi:raspberry-pi",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateModel(client);
publishConfig("sensor", config);
updateModel();
};
/**
* Updates the model sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateModel = (client) => {
const updateModel = () => {
const model = hardware.getModel();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/model`;
if (model !== null) {
client.publish(`${root}/status`, `${model}`, { qos: 1, retain: true });
}
publishState("model", model);
};
/**
* Initializes the serial number sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initSerialNumber = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/serial_number`;
const initSerialNumber = () => {
const root = `${INTEGRATION.root}/serial_number`;
const config = {
name: "Serial Number",
unique_id: `${INTEGRATION.node}_serial_number`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
icon: "mdi:hexadecimal",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateSerialNumber(client);
publishConfig("sensor", config);
updateSerialNumber();
};
/**
* Updates the serial number sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateSerialNumber = (client) => {
const updateSerialNumber = () => {
const serialNumber = hardware.getSerialNumber();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/serial_number`;
if (serialNumber !== null) {
client.publish(`${root}/status`, `${serialNumber}`, { qos: 1, retain: true });
}
publishState("serial_number", serialNumber);
};
/**
* Initializes the host name sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initHostName = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/host_name`;
const initHostName = () => {
const root = `${INTEGRATION.root}/host_name`;
const config = {
name: "Host Name",
unique_id: `${INTEGRATION.node}_host_name`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
icon: "mdi:console-network",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateHostName(client);
publishConfig("sensor", config);
updateHostName();
};
/**
* Updates the host name sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateHostName = (client) => {
const updateHostName = () => {
const hostName = hardware.getHostName();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/host_name`;
if (hostName !== null) {
client.publish(`${root}/status`, `${hostName}`, { qos: 1, retain: true });
}
publishState("host_name", hostName);
};
/**
* Initializes the up time sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initUpTime = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/up_time`;
const initUpTime = () => {
const root = `${INTEGRATION.root}/up_time`;
const config = {
name: "Up Time",
unique_id: `${INTEGRATION.node}_up_time`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "min",
icon: "mdi:timeline-clock",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateUpTime(client);
publishConfig("sensor", config);
updateUpTime();
};
/**
* Updates the up time sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateUpTime = (client) => {
const updateUpTime = () => {
const upTime = hardware.getUpTime();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/up_time`;
if (upTime !== null) {
client.publish(`${root}/status`, `${upTime}`, { qos: 1, retain: true });
}
publishState("up_time", upTime);
};
/**
* Initializes the memory size sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initMemorySize = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_size`;
const initMemorySize = () => {
const root = `${INTEGRATION.root}/memory_size`;
const config = {
name: "Memory Size",
unique_id: `${INTEGRATION.node}_memory_size`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(2) }}",
unit_of_measurement: "GiB",
icon: "mdi:memory",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateMemorySize(client);
publishConfig("sensor", config);
updateMemorySize();
};
/**
* Updates the memory size sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateMemorySize = (client) => {
const updateMemorySize = () => {
const memorySize = hardware.getMemorySize();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_size`;
if (memorySize !== null) {
client.publish(`${root}/status`, `${memorySize}`, { qos: 1, retain: true });
}
publishState("memory_size", memorySize);
};
/**
* Initializes the memory usage sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initMemoryUsage = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_usage`;
const initMemoryUsage = () => {
const root = `${INTEGRATION.root}/memory_usage`;
const config = {
name: "Memory Usage",
unique_id: `${INTEGRATION.node}_memory_usage`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "%",
icon: "mdi:memory-arrow-down",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateMemoryUsage(client);
publishConfig("sensor", config);
updateMemoryUsage();
};
/**
* Updates the memory usage sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateMemoryUsage = (client) => {
const updateMemoryUsage = () => {
const memoryUsage = hardware.getMemoryUsage();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_usage`;
if (memoryUsage !== null) {
client.publish(`${root}/status`, `${memoryUsage}`, { qos: 1, retain: true });
}
publishState("memory_usage", memoryUsage);
};
/**
* Initializes the processor usage sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initProcessorUsage = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_usage`;
const initProcessorUsage = () => {
const root = `${INTEGRATION.root}/processor_usage`;
const config = {
name: "Processor Usage",
unique_id: `${INTEGRATION.node}_processor_usage`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "%",
icon: "mdi:cpu-64-bit",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateProcessorUsage(client);
publishConfig("sensor", config);
updateProcessorUsage();
};
/**
* Updates the processor usage sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateProcessorUsage = (client) => {
const updateProcessorUsage = () => {
const processorUsage = hardware.getProcessorUsage();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_usage`;
if (processorUsage !== null) {
client.publish(`${root}/status`, `${processorUsage}`, { qos: 1, retain: true });
}
publishState("processor_usage", processorUsage);
};
/**
* Initializes the processor temperature sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initProcessorTemperature = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_temperature`;
const initProcessorTemperature = () => {
const root = `${INTEGRATION.root}/processor_temperature`;
const config = {
name: "Processor Temperature",
unique_id: `${INTEGRATION.node}_processor_temperature`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "°C",
icon: "mdi:radiator",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateProcessorTemperature(client);
publishConfig("sensor", config);
updateProcessorTemperature();
};
/**
* Updates the processor temperature sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateProcessorTemperature = (client) => {
const updateProcessorTemperature = () => {
const processorTemperature = hardware.getProcessorTemperature();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_temperature`;
if (processorTemperature !== null) {
client.publish(`${root}/status`, `${processorTemperature}`, { qos: 1, retain: true });
}
publishState("processor_temperature", processorTemperature);
};
/**
* Initializes the package upgrades sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initPackageUpgrades = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/package_upgrades`;
const initPackageUpgrades = () => {
const root = `${INTEGRATION.root}/package_upgrades`;
const config = {
name: "Package Upgrades",
unique_id: `${INTEGRATION.node}_package_upgrades`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
json_attributes_topic: `${root}/attributes`,
value_template: "{{ value }}",
value_template: "{{ value | int }}",
icon: "mdi:package-down",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updatePackageUpgrades(client);
publishConfig("sensor", config);
updatePackageUpgrades();
};
/**
* Updates the package upgrades sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updatePackageUpgrades = (client) => {
const updatePackageUpgrades = () => {
const packages = hardware.checkPackageUpgrades();
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/package_upgrades`;
if (packages !== null) {
const attributes = {
total: packages.length,
packages: packages.map((pkg) => pkg.replace(/\s*\[.*?\]\s*/g, "").trim()),
};
client.publish(`${root}/status`, `${attributes.total}`, { qos: 1, retain: true });
client.publish(`${root}/attributes`, JSON.stringify(attributes), { qos: 1, retain: true });
}
const attributes = {
total: packages.length,
packages: packages.map((pkg) => pkg.replace(/\s*\[.*?\]\s*/g, "").trim()),
};
publishAttributes("package_upgrades", attributes);
publishState("package_upgrades", attributes.total);
};
/**
* Initializes the heartbeat sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initHeartbeat = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/heartbeat`;
const initHeartbeat = () => {
const root = `${INTEGRATION.root}/heartbeat`;
const config = {
name: "Heartbeat",
unique_id: `${INTEGRATION.node}_heartbeat`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ value }}",
icon: "mdi:heart-flash",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateHeartbeat(client);
publishConfig("sensor", config);
updateHeartbeat();
};
/**
* Updates the heartbeat sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateHeartbeat = (client) => {
const updateHeartbeat = () => {
const now = new Date();
const local = new Date(now.getTime() - now.getTimezoneOffset() * 60 * 1000);
const heartbeat = local.toISOString().replace(/\.\d{3}Z$/, "");
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/heartbeat`;
client.publish(`${root}/status`, `${heartbeat}`, { qos: 1, retain: true });
publishState("heartbeat", heartbeat);
};
/**
* Initializes the last active sensor.
*
* @param {Object} client - Instance of the mqtt client.
*/
const initLastActive = (client) => {
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/last_active`;
const initLastActive = () => {
const root = `${INTEGRATION.root}/last_active`;
const config = {
name: "Last Active",
unique_id: `${INTEGRATION.node}_last_active`,
state_topic: `${root}/status`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "min",
icon: "mdi:gesture-tap-hold",
device: INTEGRATION.device,
};
client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true });
updateLastActive(client);
publishConfig("sensor", config);
updateLastActive();
};
/**
* Updates the last active sensor via the mqtt connection.
*
* @param {Object} client - Instance of the mqtt client.
*/
const updateLastActive = (client) => {
const updateLastActive = () => {
const now = new Date();
const then = WEBVIEW.pointer.time;
const lastActive = Math.abs(now - then) / (1000 * 60);
const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/last_active`;
client.publish(`${root}/status`, `${lastActive}`, { qos: 1, retain: true });
publishState("last_active", lastActive);
};
module.exports = {