awesome-battery_widget icon indicating copy to clipboard operation
awesome-battery_widget copied to clipboard

Can't determine if battery is charging

Open Hashino opened this issue 3 years ago • 2 comments

-- Create the battery widget:
local my_battery_widget = battery_widget{
    screen = s,
    device_path = '/org/freedesktop/UPower/devices/battery_BAT0',
    widget_template = wibox.widget.textbox,
    instant_update = true
}
-- When UPower updates the battery status, the widget is notified
-- and calls a signal you need to connect to:
my_battery_widget:connect_signal('upower::update', function (widget, device)
	
	widget.text = string.format('%3d', device.percentage) .. '%'

	naughty.notify({ title = "Battery Status", text = device.state, timeout = 0 })
	
end)

Doesn't yield anything. Tried all variations: state, State, status, Status; none of them returned anything. My battery widget works and it shows the percentage as it should, so at least i know that it's properly connected to UPower. original thread: https://old.reddit.com/r/awesomewm/comments/esrhnk/i_wrote_a_upowerglib_based_battery_widget_for_the/if3n4xv/

Hashino avatar Jul 07 '22 22:07 Hashino

Hello @Hashino,

I just took some time to try to debug all of that.

It looks very weird to my that trying to access a property didn't produce anything. I would have expected at least an error message on the case the property doesn't exist.

Regarding at what's available, device.state should exist. Any function or property mentioned in the documentation at https://lazka.github.io/pgi-docs/UPowerGlib-1.0/classes/Device.html should be available.

As a quick example, I used the following signal callback :

   widget:connect_signal("upower::update", function(w, device)
        print("percentage", device.percentage)
        print("icon_name", device.icon_name)
        print("state", device.state)
    end)

It gave me the output we can expect for both devices type, the display_device and BAT0 (from my utils function get_BAT0_device_path) :

percentage      99.0
icon_name       battery-full-symbolic
state   2

Note that the State from device.state is actually an enum https://lazka.github.io/pgi-docs/UPowerGlib-1.0/enums.html#UPowerGlib.DeviceState. We get here an integer that we can compare to the enum directly:

local upower = require("lgi").require "UPowerGlib"
assert(device.state == upower.DeviceState.DISCHARGING)

In my case, it was trusty since my laptop is currently on battery :tada:

Alternatively, we can convert it to a string with the static function state_to_string:

print("state_to_string", device.state_to_string(device.state))

-- Outputs:
-- state_to_string discharging

Everything seems to work just fine on my side. I'm sorry I can't come up with an easy solution... Can you provide the awesome version you use, and the error logs you have ?

Aire-One avatar Jul 08 '22 17:07 Aire-One

awesome v4.3 (Too long)
 • Compiled against Lua 5.3.6 (running with Lua 5.3)
 • D-Bus support: ✔
 • execinfo support: ✔
 • xcb-randr version: 1.6
 • LGI version: 0.9.2

i changed my code to this:

-- When UPower updates the battery status, the widget is notified
-- and calls a signal you need to connect to:
my_battery_widget:connect_signal("upower::update", function(widget, device)
	naughty.notify({ title = "Battery Status", text = "update received", timeout = 3 }) --for debugging 
		
	if device.state == 1 then
		if not widget.text == string.format('%3d', device.percentage) .. '%+' then
			naughty.notify({ title = "Battery Status", text = "charging", timeout = 3 })
		end
		widget.text = string.format('%3d', device.percentage) .. '%+'
	else
		if not widget.text == string.format('%3d', device.percentage) .. '%' then
			naughty.notify({ title = "Battery Status", text = "not charging", timeout = 3 })
		end
		widget.text = string.format('%3d', device.percentage) .. '%'
	end
end)

and it works when Awesome starts/restarts but it doesn't seem to be receiving updates when battery drops or i disconnect the charger. When i run upower --monitor it shows the updates in the terminal but the notification never appears (after the first one when i start/reset awesome)

Hashino avatar Jul 08 '22 19:07 Hashino