fix: single-owner battery debounce with hardened input handling

This commit is contained in:
loki committed 2026-09-05 15:56:46 +02:00
1 parent 97b1c4e077
commit a3455dd586
3 files changed
+16 -8

No files matched your search

+14 -6
View File
@@ -14,10 +14,13 @@ local CAP_KEY = {
local config = { device = "", mock = false, wake_sink = "" }
local zeroStreak = 0
function M.init(cfg)
config.device = cfg.device or ""
config.mock = cfg.mock or false
config.wake_sink = cfg.wake_sink or ""
zeroStreak = 0
end
function M.isMock()
@@ -224,18 +227,23 @@ function M.fetchConnected(cb)
end)
end
local zeroStreak = 0
-- Debounces transient 0% readings: a lone 0 between good readings is
-- normalized to unavailable (no false widget 0% or warning); two 0s in a
-- row pass through as a genuinely empty battery. Each entry keeps its own
-- streak since every entry gets a separate module instance.
-- row pass through as a genuinely empty battery. Single owner is the
-- service entry (service.luau); the panel publishes its one-shot wake
-- read raw. Non-table input or a missing/non-numeric level resets the
-- streak and normalizes to unavailable, as does a nil reading.
function M.stableBattery(battery)
if battery == nil then
if type(battery) ~= "table" then
zeroStreak = 0
return { status = "BATTERY_UNAVAILABLE", level = -1 }
end
if battery.level == 0 then
local level = tonumber(battery.level)
if level == nil then
zeroStreak = 0
return { status = "BATTERY_UNAVAILABLE", level = -1 }
end
if level == 0 then
zeroStreak = zeroStreak + 1
if zeroStreak >= 2 then
return battery
+1 -1
View File
@@ -110,7 +110,7 @@ function onWake()
end
hsc.fetchStatus(function(status)
if status ~= nil then
noctalia.state.set("battery", hsc.stableBattery(status.battery))
noctalia.state.set("battery", status.battery or { status = "BATTERY_UNAVAILABLE", level = -1 })
noctalia.state.set("chatmix", status.chatmix or 0)
noctalia.state.set("lastUpdate", noctalia.nowMs())
end
+1 -1
View File
@@ -36,7 +36,7 @@ local function handleStatus(status)
return
end
noctalia.state.set("error", "unavailable")
noctalia.state.set("battery", { status = "BATTERY_UNAVAILABLE", level = -1 })
noctalia.state.set("battery", hsc.stableBattery(nil))
noctalia.state.set("chatmix", 0)
return
end