~kennylevinsen/poweralertd-devel

feat: Add option '-o' to ignore notifications over a certain battery-level v2 PROPOSED

Sebastian Sellmeier: 1
 feat: Add option '-o' to ignore notifications over a certain battery-level

 1 files changed, 28 insertions(+), 19 deletions(-)
It seems to me that poweralertd could do some "hysterisis", and by that
I mean that poweralertd could simply tolerate discharge signals for a
certain duration of time without triggering a notification.

The proposed patch is an interesting solution, but it requires the user
to actually do something. In my case, I *think* I would use `-o 99`
because that's where I get those annoying warnings all the time. But
really, poweralertd shouldn't need to be configured to do this properly,
IMHO.
Okay let's see then, i have:

Apr 22 12:19:16 angela poweralertd[3253]: Ignored because -o: 99 | percentage: 100.000000
Apr 22 12:19:16 angela poweralertd[3253]: Ignored because -o: 99 | percentage: 100.000000

and with that same timestamp, i have:

‣ Type=signal  Endian=l  Flags=1  Version=1 Cookie=7186  Timestamp="Mon 2024-04-22 16:19:16.519300 UTC"
  Sender=:1.30  Path=/org/freedesktop/UPower/devices/battery_BAT1  Interface=org.freedesktop.DBus.Properties  Member=PropertiesChanged
  UniqueName=:1.30
  MESSAGE "sa{sv}as" {
          STRING "org.freedesktop.UPower.Device";
          ARRAY "{sv}" {
                  DICT_ENTRY "sv" {
                          STRING "UpdateTime";
                          VARIANT "t" {
                                  UINT64 1713802756;
                          };
                  };
          };
          ARRAY "s" {
          };
  };

everything else in busctl seems unrelated
(org.freedesktop.NetworkManager.AccessPoint, org.bluez.Device1, etc)
Understood, I have downgraded back to the package without the patch and
will keep monitoring for changes.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~kennylevinsen/poweralertd-devel/patches/43895/mbox | git am -3
Learn more about email & git

[PATCH v2] feat: Add option '-o' to ignore notifications over a certain battery-level Export this patch

First proposal of a feature-request discuessed some time ago in irc as my devices flipps state at around 98/99%.
It does not fix the issue fully as I also would like to supress online/offline notifications of the charger when over certain battery-level,
but at the time I don't have access to the current battery-level as there could be mutliple. For device notifications it only supresses the one
for devices with state over -o value.
Open for feedback/suggestions, as it's my first C-involving contribution :v:

Signed-off-by: Sebastian Sellmeier <mail@sebastian-sellmeier.de>
---
 main.c | 47 ++++++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 19 deletions(-)

diff --git a/main.c b/main.c
index 3dadda1..4ded249 100644
--- a/main.c
+++ b/main.c
@@ -142,19 +142,21 @@ static int send_warning_update(sd_bus *bus, struct upower_device *device) {
}

static const char usage[] = "usage: %s [options]\n"
"  -h				show this help message\n"
"  -s				ignore the events at startup\n"
"  -i <device_type>		ignore this device type, can be use several times\n";
"  -h					show this help message\n"
"  -i <device_type>		ignore this device type, can be use several times\n"
"  -s					ignore the events at startup\n"
"  -o <percentage>		ignores events over specified battery percentage\n";


int main(int argc, char *argv[]) {
	int opt = 0;
	int device_type = 0;
	int ignore_types_mask = 0;
	int ignore_over = 0;
	bool ignore_initial = false;
	bool initialized = false;

	while ((opt = getopt(argc, argv, "hsi:")) != -1) {
	while ((opt = getopt(argc, argv, "hi:so:")) != -1) {
		switch (opt) {
		case 'i':
			device_type = upower_device_type_int(optarg);
@@ -168,6 +170,9 @@ int main(int argc, char *argv[]) {
		case 's':
			ignore_initial = true;
			break;
		case 'o':
			ignore_over = atoi(optarg);
			break;
		case 'h':
		default:
			fprintf(stderr, usage, argv[0]);
@@ -212,23 +217,27 @@ int main(int argc, char *argv[]) {
				goto next_device;
			}

			if (upower_device_has_battery(device)) {
				ret = send_state_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send state update notification: %s\n", strerror(-ret));
					goto finish;
				}
				ret = send_warning_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send warning update notification: %s\n", strerror(-ret));
					goto finish;
			if (ignore_over == 0 || device->current.percentage < ignore_over) {
				if (upower_device_has_battery(device)) {
					ret = send_state_update(user_bus, device);
					if (ret < 0) {
						fprintf(stderr, "could not send state update notification: %s\n", strerror(-ret));
						goto finish;
					}
					ret = send_warning_update(user_bus, device);
					if (ret < 0) {
						fprintf(stderr, "could not send warning update notification: %s\n", strerror(-ret));
						goto finish;
					}
				} else {
					ret = send_online_update(user_bus, device);
					if (ret < 0) {
						fprintf(stderr, "could not send online update notification: %s\n", strerror(-ret));
						goto finish;
					}
				}
			} else {
				ret = send_online_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send online update notification: %s\n", strerror(-ret));
					goto finish;
				}
				fprintf(stderr, "Ignored because -o: %i | percentage: %f\n", ignore_over, device->current.percentage);
			}
next_device:
			device->last = device->current;
-- 
2.41.0