Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't determine if battery is charging #2

Open
Hashino opened this issue Jul 7, 2022 · 2 comments
Open

Can't determine if battery is charging #2

Hashino opened this issue Jul 7, 2022 · 2 comments
Assignees

Comments

@Hashino
Copy link

Hashino commented Jul 7, 2022

-- 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/

@Aire-One Aire-One self-assigned this Jul 8, 2022
@Aire-One
Copy link
Owner

Aire-One commented Jul 8, 2022

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 🎉

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 ?

@Hashino
Copy link
Author

Hashino commented Jul 8, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants