reno/modules/powermanX.lua

76 lines
3.5 KiB
Lua
Raw Normal View History

2022-09-16 18:42:50 +00:00
-- This file is part of Reno desktop.
--
-- Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
-- Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>.
-- Powerman X - second generation of the power management module
2023-03-21 18:12:35 +00:00
local awful = require("awful")
2022-09-16 18:42:50 +00:00
local sysctl = require("syscontrol")
local naughty = require("naughty")
local gears = require("gears")
local batteries = sysctl.power_supply.enumerate()
local state_tracking = {}
-- Configuration variables
local cfg = config.powerman or {}
local quality_min = cfg.battery_quality_min or 33
local capacity_min = cfg.battery_capacity_min or 15
2023-03-21 18:12:35 +00:00
local on_low_battery = cfg.on_low_battery or ""
local on_charged_battery = cfg.on_charged_battery
local on_critical_condition = cfg.on_critical_condition
2022-09-16 18:42:50 +00:00
-- Main loop
2023-01-19 13:42:20 +00:00
gears.timer({
2022-09-16 18:42:50 +00:00
timeout = 2,
autostart = true,
callback = function()
2023-01-19 13:42:20 +00:00
for _,v in pairs(batteries) do
local data,_ = sysctl.power_supply.read_attribs(v)
2022-09-16 18:42:50 +00:00
state_tracking[v] = state_tracking[v] or {}
if data.type == "Battery" then
2023-03-21 18:12:35 +00:00
if (tonumber(data.quality) < quality_min) and
2022-09-16 18:42:50 +00:00
(not state_tracking[v].quality_notification) then
naughty.notify({
title = "Critical battery condition",
text = "Battery "..data.name.." has reached critically low condition, seek a suitable replacement"
})
state_tracking[v].quality_notification = true
2023-03-21 18:12:35 +00:00
if on_critical_condition then
awful.spawn(on_critical_condition)
end
2022-09-16 18:42:50 +00:00
end
2023-03-21 18:12:35 +00:00
if (tonumber(data.capacity) <= capacity_min) and
2022-09-16 18:42:50 +00:00
(not data.charging) and
(not state_tracking[v].capacity_notification) then
naughty.notify({
title = "Battery capacity low",
text = "Battery "..data.name.." capacity is at "..tostring(data.capacity).."%"
})
state_tracking[v].capacity_notification = true
2023-03-21 18:12:35 +00:00
if on_low_battery then
awful.spawn(on_low_battery)
end
2022-09-16 18:42:50 +00:00
end
if (tonumber(data.capacity) > capacity_min) then
state_tracking[v].capacity_notification = false
end
2023-03-21 18:12:35 +00:00
if (data.capacity == "100") and
2022-09-16 18:42:50 +00:00
(data.charging) and
(not state_tracking[v].charged_notification) then
naughty.notify({
title = "Battery is completely charged",
text = "Disconnect the charger from the power grid to avoid passive electricity usage."
})
2023-03-21 18:12:35 +00:00
if on_charged_battery then
awful.spawn(on_charged_battery)
end
2022-09-16 18:42:50 +00:00
end
if (not data.charging) then
state_tracking[v].charged_notification = false
end
end
end
end
})