feat: battery power level over mqtt #33

This commit is contained in:
leukipp
2025-07-09 19:14:36 +02:00
parent e64f092f6c
commit adbc214f4d
3 changed files with 83 additions and 9 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ const { app } = require("electron");
global.ARGS = global.ARGS || {}; global.ARGS = global.ARGS || {};
if (!process.env.DISPLAY) { if (!process.env.DISPLAY) {
console.error(`$DISPLAY variable not set to run the GUI application, are you connected via SSH?`); console.error(`\n$DISPLAY variable not set to run the GUI application, are you connected via SSH?`);
console.error(`If you have installed the service use:`); console.error(`If you have installed the service use:`);
console.error(` systemctl --user start touchkio.service`); console.error(` systemctl --user start touchkio.service`);
console.error(`Alternatively export the variables first:`); console.error(`Alternatively export the variables first:`);
+51 -8
View File
@@ -9,6 +9,11 @@ global.HARDWARE = global.HARDWARE || {
status: null, status: null,
session: {}, session: {},
support: {}, support: {},
battery: {
level: {
path: null,
},
},
display: { display: {
status: { status: {
path: null, path: null,
@@ -41,6 +46,8 @@ const init = async () => {
// Init globals // Init globals
HARDWARE.session.user = sessionUser(); HARDWARE.session.user = sessionUser();
HARDWARE.session.type = sessionType(); HARDWARE.session.type = sessionType();
HARDWARE.battery.level.path = getBatteryLevelPath();
HARDWARE.support.batteryLevel = HARDWARE.battery.level.path !== null;
HARDWARE.display.status.path = getDisplayStatusPath(); HARDWARE.display.status.path = getDisplayStatusPath();
HARDWARE.support.displayStatus = HARDWARE.display.status.path !== null; HARDWARE.support.displayStatus = HARDWARE.display.status.path !== null;
HARDWARE.display.brightness.path = getDisplayBrightnessPath(); HARDWARE.display.brightness.path = getDisplayBrightnessPath();
@@ -70,8 +77,9 @@ const init = async () => {
console.log("Processor Usage:", getProcessorUsage()); console.log("Processor Usage:", getProcessorUsage());
console.log("Processor Temperature:", getProcessorTemperature()); console.log("Processor Temperature:", getProcessorTemperature());
// Show display infos // Show hardware infos
console.log(`\nDisplay Status [${HARDWARE.display.status.path}]:`, getDisplayStatus()); console.log(`\nBattery Level [${HARDWARE.battery.level.path}]:`, getBatteryLevel());
console.log(`Display Status [${HARDWARE.display.status.path}]:`, getDisplayStatus());
console.log(`Display Brightness [${HARDWARE.display.brightness.path}]:`, getDisplayBrightness(), "\n"); console.log(`Display Brightness [${HARDWARE.display.brightness.path}]:`, getDisplayBrightness(), "\n");
// Check for keyboard visibility // Check for keyboard visibility
@@ -309,6 +317,39 @@ const getProcessorTemperature = () => {
return null; return null;
}; };
/**
* Gets the current battery power level path using `/sys/class/power_supply`.
*
* @returns {string|null} The battery power level path or null if nothing was found.
*/
const getBatteryLevelPath = () => {
const power = "/sys/class/power_supply";
for (const supply of fs.readdirSync(power)) {
const capacityFile = path.join(power, supply, "capacity");
if (!fs.existsSync(capacityFile)) {
continue;
}
return path.join(power, supply);
}
return null;
};
/**
* Gets the current battery power level using `/sys/class/power_supply/.../capacity`.
*
* @returns {number|null} The battery power level in percentage or null if nothing was found.
*/
const getBatteryLevel = () => {
if (!HARDWARE.support.batteryLevel) {
return null;
}
const capacity = fs.readFileSync(`${HARDWARE.battery.level.path}/capacity`, "utf8").trim();
if (capacity) {
return parseFloat(capacity);
}
return null;
};
/** /**
* Gets the current display status path using `/sys/class/drm`. * Gets the current display status path using `/sys/class/drm`.
* *
@@ -406,8 +447,8 @@ const getDisplayBrightnessMax = () => {
if (!HARDWARE.support.displayBrightness) { if (!HARDWARE.support.displayBrightness) {
return null; return null;
} }
const max = execSyncCommand("cat", [`${HARDWARE.display.brightness.path}/max_brightness`]); const max = fs.readFileSync(`${HARDWARE.display.brightness.path}/max_brightness`, "utf8").trim();
if (max !== null) { if (max) {
return parseInt(max, 10); return parseInt(max, 10);
} }
return null; return null;
@@ -422,10 +463,10 @@ const getDisplayBrightness = () => {
if (!HARDWARE.support.displayBrightness) { if (!HARDWARE.support.displayBrightness) {
return null; return null;
} }
const brightness = execSyncCommand("cat", [`${HARDWARE.display.brightness.path}/brightness`]); const brightness = fs.readFileSync(`${HARDWARE.display.brightness.path}/brightness`, "utf8").trim();
if (brightness !== null) { if (brightness) {
const max = HARDWARE.display.brightness.max; const max = HARDWARE.display.brightness.max || 1;
return Math.round((parseInt(brightness, 10) / max) * 100); return Math.max(1, Math.min(Math.round((parseInt(brightness, 10) / max) * 100), 100));
} }
return null; return null;
}; };
@@ -720,6 +761,8 @@ module.exports = {
getMemoryUsage, getMemoryUsage,
getProcessorUsage, getProcessorUsage,
getProcessorTemperature, getProcessorTemperature,
getBatteryLevelPath,
getBatteryLevel,
getDisplayStatusPath, getDisplayStatusPath,
getDisplayStatus, getDisplayStatus,
setDisplayStatus, setDisplayStatus,
+31
View File
@@ -77,6 +77,7 @@ const init = async () => {
initMemoryUsage(); initMemoryUsage();
initProcessorUsage(); initProcessorUsage();
initProcessorTemperature(); initProcessorTemperature();
initBatteryLevel();
initPackageUpgrades(); initPackageUpgrades();
initHeartbeat(); initHeartbeat();
initLastActive(); initLastActive();
@@ -134,6 +135,7 @@ const update = async () => {
updateMemoryUsage(); updateMemoryUsage();
updateProcessorUsage(); updateProcessorUsage();
updateProcessorTemperature(); updateProcessorTemperature();
updateBatteryLevel();
}; };
/** /**
@@ -692,6 +694,35 @@ const updateProcessorTemperature = () => {
publishState("processor_temperature", processorTemperature); publishState("processor_temperature", processorTemperature);
}; };
/**
* Initializes the battery level sensor.
*/
const initBatteryLevel = () => {
if (!HARDWARE.support.batteryLevel) {
return;
}
const root = `${INTEGRATION.root}/battery_level`;
const config = {
name: "Battery Level",
unique_id: `${INTEGRATION.node}_battery_level`,
state_topic: `${root}/state`,
value_template: "{{ (value | float) | round(0) }}",
unit_of_measurement: "%",
icon: "mdi:battery-medium",
device: INTEGRATION.device,
};
publishConfig("sensor", config);
updateBatteryLevel();
};
/**
* Updates the battery level sensor via the mqtt connection.
*/
const updateBatteryLevel = () => {
const batteryLevel = hardware.getBatteryLevel();
publishState("battery_level", batteryLevel);
};
/** /**
* Initializes the package upgrades sensor. * Initializes the package upgrades sensor.
*/ */