feat: add network address mqtt sensor #66
This commit is contained in:
@@ -67,6 +67,7 @@ const init = async () => {
|
||||
console.log("\nModel:", getModel());
|
||||
console.log("Vendor:", getVendor());
|
||||
console.log("Serial Number:", getSerialNumber());
|
||||
console.log("Network Addresses:", getNetworkAddresses());
|
||||
console.log("Host Name:", getHostName());
|
||||
|
||||
// Show system infos
|
||||
@@ -280,6 +281,32 @@ const getMachineId = () => {
|
||||
return "123456";
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the network interfaces addresses using `os.networkInterfaces()`.
|
||||
*
|
||||
* @returns {Object} The network addresses of all interfaces.
|
||||
*/
|
||||
const getNetworkAddresses = () => {
|
||||
const addresses = {};
|
||||
for (const [key, interfaces] of Object.entries(os.networkInterfaces())) {
|
||||
for (const interface of interfaces) {
|
||||
if (interface.internal || !interface.family || !interface.address) {
|
||||
continue;
|
||||
}
|
||||
const name = key.charAt(0).toUpperCase() + key.slice(1);
|
||||
if (!addresses[name]) {
|
||||
addresses[name] = {};
|
||||
}
|
||||
const family = interface.family;
|
||||
if (!addresses[name][family]) {
|
||||
addresses[name][family] = [];
|
||||
}
|
||||
addresses[name][family].push(interface.address);
|
||||
}
|
||||
}
|
||||
return addresses;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the host name of the current system using `os.hostname()`.
|
||||
*
|
||||
@@ -845,6 +872,7 @@ module.exports = {
|
||||
getVendor,
|
||||
getSerialNumber,
|
||||
getMachineId,
|
||||
getNetworkAddresses,
|
||||
getHostName,
|
||||
getUpTime,
|
||||
getMemorySize,
|
||||
|
||||
+34
-1
@@ -47,6 +47,7 @@ const init = async () => {
|
||||
serial_number: serialNumber,
|
||||
identifiers: [INTEGRATION.node],
|
||||
sw_version: `${app.getName()}-v${app.getVersion()}`,
|
||||
configuration_url: "https://github.com/leukipp/touchkio",
|
||||
};
|
||||
|
||||
// Connection settings
|
||||
@@ -75,6 +76,7 @@ const init = async () => {
|
||||
initModel();
|
||||
initSerialNumber();
|
||||
initHostName();
|
||||
initNetworkAddress();
|
||||
initUpTime();
|
||||
initMemorySize();
|
||||
initMemoryUsage();
|
||||
@@ -128,6 +130,7 @@ const update = async () => {
|
||||
updatePageNumber();
|
||||
updatePageZoom();
|
||||
updatePageUrl();
|
||||
updateNetworkAddress();
|
||||
updateUpTime();
|
||||
updateLastActive();
|
||||
updateMemoryUsage();
|
||||
@@ -593,6 +596,36 @@ const updateSerialNumber = () => {
|
||||
publishState("serial_number", serialNumber);
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes the network address sensor.
|
||||
*/
|
||||
const initNetworkAddress = () => {
|
||||
const root = `${INTEGRATION.root}/network_address`;
|
||||
const config = {
|
||||
name: "Network Address",
|
||||
unique_id: `${INTEGRATION.node}_network_address`,
|
||||
state_topic: `${root}/state`,
|
||||
json_attributes_topic: `${root}/attributes`,
|
||||
value_template: "{{ value }}",
|
||||
icon: "mdi:ip-network",
|
||||
device: INTEGRATION.device,
|
||||
};
|
||||
publishConfig("sensor", config);
|
||||
updateNetworkAddress();
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the network address sensor via the mqtt connection.
|
||||
*/
|
||||
const updateNetworkAddress = () => {
|
||||
const networkAddresses = hardware.getNetworkAddresses();
|
||||
const [name] = Object.keys(networkAddresses);
|
||||
const [family] = name ? Object.keys(networkAddresses[name]) : [];
|
||||
const networkAddress = networkAddresses[name]?.[family]?.[0] || null;
|
||||
publishState("network_address", networkAddress);
|
||||
publishAttributes("network_address", networkAddresses);
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes the host name sensor.
|
||||
*/
|
||||
@@ -804,8 +837,8 @@ const updatePackageUpgrades = () => {
|
||||
total: packages.length,
|
||||
packages: packages.map((pkg) => pkg.replace(/\s*\[.*?\]\s*/g, "").trim()),
|
||||
};
|
||||
publishAttributes("package_upgrades", attributes);
|
||||
publishState("package_upgrades", attributes.total);
|
||||
publishAttributes("package_upgrades", attributes);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user