-
Notifications
You must be signed in to change notification settings - Fork 52
/
upower.py
250 lines (211 loc) · 8.22 KB
/
upower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""upowerd mock template
This creates the expected methods and properties of the main
org.freedesktop.UPower object, but no devices. You can specify any property
such as 'OnLowBattery' or the return value of 'SuspendAllowed',
'HibernateAllowed', and 'GetCriticalAction' in "parameters".
This provides the 1.0 D-Bus API of upower.
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012, 2013 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt <[email protected]>
"""
import dbus
import dbusmock
from dbusmock import MOCK_IFACE, mockobject
BUS_NAME = "org.freedesktop.UPower"
MAIN_OBJ = "/org/freedesktop/UPower"
MAIN_IFACE = "org.freedesktop.UPower"
SYSTEM_BUS = True
DEVICE_IFACE = "org.freedesktop.UPower.Device"
def load(mock, parameters):
mock.AddMethods(
MAIN_IFACE,
[
(
"EnumerateDevices",
"",
"ao",
'ret = [k for k in objects.keys() if "/devices" in k and not k.endswith("/DisplayDevice")]',
),
],
)
props = dbus.Dictionary(
{
"DaemonVersion": parameters.get("DaemonVersion", "0.99"),
"OnBattery": parameters.get("OnBattery", False),
"LidIsPresent": parameters.get("LidIsPresent", True),
"LidIsClosed": parameters.get("LidIsClosed", False),
"LidForceSleep": parameters.get("LidForceSleep", True),
"IsDocked": parameters.get("IsDocked", False),
},
signature="sv",
)
mock.AddMethods(
MAIN_IFACE,
[
("GetCriticalAction", "", "s", f'ret = "{parameters.get("GetCriticalAction", "HybridSleep")}"'),
("GetDisplayDevice", "", "o", 'ret = "/org/freedesktop/UPower/devices/DisplayDevice"'),
],
)
mock.p_display_dev = "/org/freedesktop/UPower/devices/DisplayDevice"
# add Display device; for defined properties, see
# http://cgit.freedesktop.org/upower/tree/src/org.freedesktop.UPower.xml
mock.AddObject(
mock.p_display_dev,
DEVICE_IFACE,
{
"Type": dbus.UInt32(0),
"State": dbus.UInt32(0),
"Percentage": dbus.Double(0.0),
"Energy": dbus.Double(0.0),
"EnergyFull": dbus.Double(0.0),
"EnergyRate": dbus.Double(0.0),
"TimeToEmpty": dbus.Int64(0),
"TimeToFull": dbus.Int64(0),
"IsPresent": dbus.Boolean(False),
"IconName": dbus.String(""),
# LEVEL_NONE
"WarningLevel": dbus.UInt32(1),
},
[
("Refresh", "", "", ""),
],
)
mock.AddProperties(MAIN_IFACE, props)
@dbus.service.method(MOCK_IFACE, in_signature="ss", out_signature="s")
def AddAC(self, device_name, model_name):
"""Convenience method to add an AC object
You have to specify a device name which must be a valid part of an object
path, e. g. "mock_ac", and an arbitrary model name.
Please note that this does not set any global properties such as
"on-battery".
Returns the new object path.
"""
path = "/org/freedesktop/UPower/devices/" + device_name
self.AddObject(
path,
DEVICE_IFACE,
{
"PowerSupply": dbus.Boolean(True),
"Model": dbus.String(model_name),
"Online": dbus.Boolean(True),
},
[],
)
self.EmitSignal(MAIN_IFACE, "DeviceAdded", "o", [path])
return path
@dbus.service.method(MOCK_IFACE, in_signature="ssdx", out_signature="s")
def AddDischargingBattery(self, device_name, model_name, percentage, seconds_to_empty):
"""Convenience method to add a discharging battery object
You have to specify a device name which must be a valid part of an object
path, e. g. "mock_ac", an arbitrary model name, the charge percentage, and
the seconds until the battery is empty.
Please note that this does not set any global properties such as
"on-battery".
Returns the new object path.
"""
path = "/org/freedesktop/UPower/devices/" + device_name
self.AddObject(
path,
DEVICE_IFACE,
{
"PowerSupply": dbus.Boolean(True),
"IsPresent": dbus.Boolean(True),
"Model": dbus.String(model_name),
"Percentage": dbus.Double(percentage),
"TimeToEmpty": dbus.Int64(seconds_to_empty),
"EnergyFull": dbus.Double(100.0),
"Energy": dbus.Double(percentage),
# UP_DEVICE_STATE_DISCHARGING
"State": dbus.UInt32(2),
# UP_DEVICE_KIND_BATTERY
"Type": dbus.UInt32(2),
},
[],
)
self.EmitSignal(MAIN_IFACE, "DeviceAdded", "o", [path])
return path
@dbus.service.method(MOCK_IFACE, in_signature="ssdx", out_signature="s")
def AddChargingBattery(self, device_name, model_name, percentage, seconds_to_full):
"""Convenience method to add a charging battery object
You have to specify a device name which must be a valid part of an object
path, e. g. "mock_ac", an arbitrary model name, the charge percentage, and
the seconds until the battery is full.
Please note that this does not set any global properties such as
"on-battery".
Returns the new object path.
"""
path = "/org/freedesktop/UPower/devices/" + device_name
self.AddObject(
path,
DEVICE_IFACE,
{
"PowerSupply": dbus.Boolean(True),
"IsPresent": dbus.Boolean(True),
"Model": dbus.String(model_name),
"Percentage": dbus.Double(percentage),
"TimeToFull": dbus.Int64(seconds_to_full),
"EnergyFull": dbus.Double(100.0),
"Energy": dbus.Double(percentage),
# UP_DEVICE_STATE_CHARGING
"State": dbus.UInt32(1),
# UP_DEVICE_KIND_BATTERY
"Type": dbus.UInt32(2),
},
[],
)
self.EmitSignal(MAIN_IFACE, "DeviceAdded", "o", [path])
return path
@dbus.service.method(MOCK_IFACE, in_signature="uuddddxxbsu", out_signature="")
def SetupDisplayDevice(
self,
_type,
state,
percentage,
energy,
energy_full,
energy_rate,
time_to_empty,
time_to_full,
is_present,
icon_name,
warning_level,
):
"""Convenience method to configure DisplayDevice properties
This calls Set() for all properties that the DisplayDevice is defined to
have, and is shorter if you have to completely set it up instead of
changing just one or two properties.
"""
display_props = mockobject.objects[self.p_display_dev]
display_props.Set(DEVICE_IFACE, "Type", dbus.UInt32(_type))
display_props.Set(DEVICE_IFACE, "State", dbus.UInt32(state))
display_props.Set(DEVICE_IFACE, "Percentage", percentage)
display_props.Set(DEVICE_IFACE, "Energy", energy)
display_props.Set(DEVICE_IFACE, "EnergyFull", energy_full)
display_props.Set(DEVICE_IFACE, "EnergyRate", energy_rate)
display_props.Set(DEVICE_IFACE, "TimeToEmpty", dbus.Int64(time_to_empty))
display_props.Set(DEVICE_IFACE, "TimeToFull", dbus.Int64(time_to_full))
display_props.Set(DEVICE_IFACE, "IsPresent", is_present)
display_props.Set(DEVICE_IFACE, "IconName", icon_name)
display_props.Set(DEVICE_IFACE, "WarningLevel", dbus.UInt32(warning_level))
@dbus.service.method(MOCK_IFACE, in_signature="oa{sv}", out_signature="")
def SetDeviceProperties(_self, object_path, properties):
"""Convenience method to Set a device's properties.
object_path: the device to update
properties: dictionary of keys to dbus variants.
Changing this property will trigger the device's PropertiesChanged signal.
"""
device = dbusmock.get_object(object_path)
# set the properties
for key, value in properties.items():
device.Set(DEVICE_IFACE, key, value)
@dbus.service.method(MOCK_IFACE, in_signature="o", out_signature="")
def RemoveDevice(self, device_path):
self.RemoveObject(device_path)
self.EmitSignal(MAIN_IFACE, "DeviceRemoved", "o", [device_path])