Created
February 10, 2021 10:52
-
-
Save betaboon/a112b822bb9676a7f8adebc1704dcd29 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
import dbus | |
import json | |
# https://upower.freedesktop.org/docs/Device.html | |
PROPERTIES_MAP = { | |
"Type": { | |
0: "Unknown", | |
1: "Line Power", | |
2: "Battery", | |
3: "Ups", | |
4: "Monitor", | |
5: "Mouse", | |
6: "Keyboard", | |
7: "Pda", | |
8: "Phone", | |
}, | |
"State": { | |
0: "Unknown", | |
1: "Charging", | |
2: "Discharging", | |
3: "Empty", | |
4: "Fully charged", | |
5: "Pending charge", | |
6: "Pending discharge", | |
}, | |
"Technology": { | |
0: "Unknown", | |
1: "Lithium ion", | |
2: "Lithium polymer", | |
3: "Lithium iron phosphate", | |
4: "Lead acid", | |
5: "Nickel cadmium", | |
6: "Nickel metal hydride", | |
}, | |
"WarningLevel": { | |
0: "Unknown", | |
1: "None", | |
2: "Discharging", | |
3: "Low", | |
4: "Critical", | |
5: "Action", | |
}, | |
"BatteryLevel": { | |
0: "Unknown", | |
1: "None", | |
3: "Low", | |
4: "Critical", | |
6: "Normal", | |
7: "High", | |
8: "Full", | |
}, | |
} | |
# https://dbus.freedesktop.org/doc/dbus-python/tutorial.html#basic-types | |
NORMALIZE_TYPE_MAP = { | |
dbus.Boolean: bool, | |
dbus.Int16: int, | |
dbus.UInt16: int, | |
dbus.Int32: int, | |
dbus.UInt32: int, | |
dbus.Int64: int, | |
dbus.UInt64: int, | |
dbus.Double: float, | |
dbus.String: str, | |
} | |
def get_devices(bus): | |
obj = bus.get_object(bus_name="org.freedesktop.UPower", object_path="/org/freedesktop/UPower") | |
devices = obj.EnumerateDevices(dbus_interface="org.freedesktop.UPower") | |
return devices | |
def get_device_properties(bus, device_object_path): | |
obj = bus.get_object(bus_name="org.freedesktop.UPower", object_path=device_object_path) | |
interface = dbus.Interface(object=obj, dbus_interface="org.freedesktop.DBus.Properties") | |
properties = interface.GetAll("org.freedesktop.UPower.Device") | |
return properties | |
def get_all(): | |
result = dbus.Array() | |
bus = dbus.SystemBus() | |
devices = get_devices(bus) | |
for device in devices: | |
device_properties = get_device_properties(bus, device) | |
result.append(device_properties) | |
return result | |
def normalize(obj): | |
if type(obj) in NORMALIZE_TYPE_MAP: | |
return NORMALIZE_TYPE_MAP[type(obj)](obj) | |
if isinstance(obj, dbus.Array): | |
return [normalize(v) for v in obj] | |
if isinstance(obj, dbus.Dictionary): | |
result = {} | |
for k, v in obj.items(): | |
if k in PROPERTIES_MAP: | |
result[normalize(k)] = PROPERTIES_MAP[k].get(v, "Unknown") | |
else: | |
result[normalize(k)] = normalize(v) | |
return result | |
return obj | |
def main(): | |
devices = get_all() | |
print(json.dumps(normalize(devices))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment