GithubHelp home page GithubHelp logo

battery-notify's Introduction

battery-notify | Tests

battery-notify is a small, Linux-only program that sends notifications on changes to system or Bluetooth battery state.

Features

  • Small, easy to understand codebase
  • Notifications on battery state change
  • Bluetooth battery support
  • Works with multiple system batteries
  • Warnings on low/critical battery percentages
  • Warnings when connected to an external monitor but not mains power (X11 only)
  • Ability to sleep the computer with a custom command on critical percentage

Installation

cargo install battery-notify

Default features:

  • mons: Support warn_on_mons_with_no_ac. Adds a dependency on the x11rb crate.
  • bluetooth: Support bluetooth_low_pct. Adds a dependency on the zbus crate. You will also need to run bluetoothd with the --experimental flag to expose battery information.

If you don't want to use some subset of these features, you can pass --no-default-features and select the ones you do want with --feature.

Usage

Run battery-notify. You'll also need a notification daemon capable of disabling Desktop Notifications, like dunst or similar.

Configuration

You can configure battery-notify at ~/.config/battery-notify/config.toml -- on first run, this will be populated with a basic config if it doesn't exist.

The default config is:

# How often to check battery status, in seconds.
interval_secs = 30

# At what percentage of battery capacity to notify about low battery.
low_pct = 40

# At what percentage of battery capacity to notify and run sleep_command.
sleep_pct = 15

# The command to run when sleeping. Bear in mind that if you run as an
# unprivileged user, you may need to consider elevation, either with NOPASSWD
# or things like polkit.
sleep_command = 'systemctl suspend'

# If this many monitors are connected (that is, plugged in -- they can be off)
# and we are discharging, show a warning. Intended to avoid cases where power
# is inadvertently disconnected at a desk.
#
# Set to 0 to disable.
warn_on_mons_with_no_ac = 2

# If a bluetooth device is below this percentage, notify about low battery.
# Note that you need to run bluetoothd with --experimental in order for it to
# expose battery information.
#
# Set to 0 to disable.
bluetooth_low_pct = 40

Output

If you don't like the output, you can disable logging with RUST_LOG=none.

battery-notify's People

Contributors

cdown avatar dependabot[bot] avatar linsallyzhao avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

imadnyc

battery-notify's Issues

Check drift behaviour

Seems fairly likely the anti drift isn't working right:

May 05 11:31:05 roujiamo sh[266462]: [2023-05-05T10:31:05Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39330000, full_uwh: 56970000 }]
May 05 11:31:05 roujiamo sh[266462]: [2023-05-05T10:31:05Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39330000, full_uwh: 56970000 }
May 05 11:31:05 roujiamo sh[266462]: [2023-05-05T10:31:05Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:33:05 roujiamo sh[266462]: [2023-05-05T10:33:05Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:33:05 roujiamo sh[266462]: [2023-05-05T10:33:05Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:33:06 roujiamo sh[266462]: [2023-05-05T10:33:05Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:35:06 roujiamo sh[266462]: [2023-05-05T10:35:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:35:06 roujiamo sh[266462]: [2023-05-05T10:35:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:35:06 roujiamo sh[266462]: [2023-05-05T10:35:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:37:06 roujiamo sh[266462]: [2023-05-05T10:37:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:37:06 roujiamo sh[266462]: [2023-05-05T10:37:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:37:06 roujiamo sh[266462]: [2023-05-05T10:37:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:39:06 roujiamo sh[266462]: [2023-05-05T10:39:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:39:06 roujiamo sh[266462]: [2023-05-05T10:39:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:39:06 roujiamo sh[266462]: [2023-05-05T10:39:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:41:06 roujiamo sh[266462]: [2023-05-05T10:41:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:41:06 roujiamo sh[266462]: [2023-05-05T10:41:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:41:06 roujiamo sh[266462]: [2023-05-05T10:41:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:43:06 roujiamo sh[266462]: [2023-05-05T10:43:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:43:06 roujiamo sh[266462]: [2023-05-05T10:43:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:43:06 roujiamo sh[266462]: [2023-05-05T10:43:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:45:06 roujiamo sh[266462]: [2023-05-05T10:45:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:45:06 roujiamo sh[266462]: [2023-05-05T10:45:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:45:06 roujiamo sh[266462]: [2023-05-05T10:45:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:47:06 roujiamo sh[266462]: [2023-05-05T10:47:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:47:06 roujiamo sh[266462]: [2023-05-05T10:47:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:47:06 roujiamo sh[266462]: [2023-05-05T10:47:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:49:06 roujiamo sh[266462]: [2023-05-05T10:49:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:49:06 roujiamo sh[266462]: [2023-05-05T10:49:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:49:06 roujiamo sh[266462]: [2023-05-05T10:49:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:51:06 roujiamo sh[266462]: [2023-05-05T10:51:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:51:06 roujiamo sh[266462]: [2023-05-05T10:51:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:51:06 roujiamo sh[266462]: [2023-05-05T10:51:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:53:06 roujiamo sh[266462]: [2023-05-05T10:53:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:53:06 roujiamo sh[266462]: [2023-05-05T10:53:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:53:06 roujiamo sh[266462]: [2023-05-05T10:53:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:55:06 roujiamo sh[266462]: [2023-05-05T10:55:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:55:06 roujiamo sh[266462]: [2023-05-05T10:55:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:55:06 roujiamo sh[266462]: [2023-05-05T10:55:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:57:06 roujiamo sh[266462]: [2023-05-05T10:57:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:57:06 roujiamo sh[266462]: [2023-05-05T10:57:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:57:06 roujiamo sh[266462]: [2023-05-05T10:57:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 11:59:06 roujiamo sh[266462]: [2023-05-05T10:59:06Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39320000, full_uwh: 56970000 }]
May 05 11:59:06 roujiamo sh[266462]: [2023-05-05T10:59:06Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39320000, full_uwh: 56970000 }
May 05 11:59:06 roujiamo sh[266462]: [2023-05-05T10:59:06Z INFO  battery_notify] Bluetooth battery status: []
May 05 12:01:07 roujiamo sh[266462]: [2023-05-05T11:01:07Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39300000, full_uwh: 56970000 }]
May 05 12:01:07 roujiamo sh[266462]: [2023-05-05T11:01:07Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39300000, full_uwh: 56970000 }
May 05 12:01:07 roujiamo sh[266462]: [2023-05-05T11:01:07Z INFO  battery_notify] Bluetooth battery status: []
May 05 12:03:07 roujiamo sh[266462]: [2023-05-05T11:03:07Z INFO  battery_notify] Battery status: [Battery { state: NotCharging, now_uwh: 39300000, full_uwh: 56970000 }]
May 05 12:03:07 roujiamo sh[266462]: [2023-05-05T11:03:07Z INFO  battery_notify] Global status: Battery { state: AtThreshold, now_uwh: 39300000, full_uwh: 56970000 }
May 05 12:03:07 roujiamo sh[266462]: [2023-05-05T11:03:07Z INFO  battery_notify] Bluetooth battery status: []

Go through error handling and validate it make sense

Some general thoughts:

  1. We should make sure connection errors would only error at the point of connection, and terminate the program if failed.
  2. We should make sure transient errors don't just get shoved into an .unwrap_or(), and actually show an error.

Do checked_sub on duration/intervals

This needs some more thinking, because we also need to handle what to do if Instant::now() is too low. Maybe always just bump it by sleep_backoff?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.