Created
September 6, 2017 09:28
-
-
Save benley/030d9f7a897b13b48a15ba9b6b186b87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """Send desktop notifications when AC power status changes.""" | |
| from __future__ import print_function | |
| import dbus | |
| from dbus.mainloop import glib | |
| import gobject | |
| onMessage="Power plugged in!" | |
| offMessage="Power unplugged!" | |
| onImage="/run/current-system/sw/share/icons/breeze-dark/devices/64/battery.svg" | |
| offImage="/run/current-system/sw/share/icons/breeze-dark/devices/64/battery.svg" | |
| def mkCallback(bus): | |
| bus_obj = bus.get_object( | |
| 'org.freedesktop.Notifications', | |
| '/org/freedesktop/Notifications', | |
| ) | |
| notifications_iface = dbus.Interface( | |
| bus_obj, | |
| dbus_interface='org.freedesktop.Notifications', | |
| ) | |
| def notify(msg, | |
| app_name="", | |
| icon="", | |
| summary="", | |
| actions=(), | |
| hints=(), | |
| timeout=5000, # milliseconds | |
| ): | |
| # print(msg) | |
| notify.last_msg_id = notifications_iface.Notify( | |
| app_name, | |
| notify.last_msg_id, | |
| icon, | |
| summary, | |
| msg, | |
| actions, | |
| hints, | |
| timeout, | |
| ) | |
| notify.last_msg_id = 0 | |
| def callback(*args): | |
| # print("callback called: ", args) | |
| if len(args) == 3: | |
| (dev, data, sig) = args | |
| online = data.get('Online') | |
| if online is None: | |
| print("??????? I AM CONFUSION ???????") | |
| elif online: | |
| notify(msg=onMessage, icon=onImage) | |
| else: | |
| notify(msg=offMessage, icon=offImage) | |
| return callback | |
| def main(): | |
| dbus.set_default_main_loop(glib.DBusGMainLoop()) | |
| system_bus = dbus.SystemBus() | |
| session_bus = dbus.SessionBus() | |
| system_bus.add_signal_receiver( | |
| handler_function=mkCallback(session_bus), | |
| signal_name="PropertiesChanged", | |
| dbus_interface="org.freedesktop.DBus.Properties", | |
| bus_name=None, # Sender? | |
| path='/org/freedesktop/UPower/devices/line_power_AC0', | |
| ) | |
| loop = gobject.MainLoop() | |
| loop.run() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment