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