summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozrunner/mozrunner/devices/emulator_battery.py
blob: 58d42b0a0ec277c3166d4eb7552055f3e710fcf4 (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


class EmulatorBattery(object):
    def __init__(self, emulator):
        self.emulator = emulator

    def get_state(self):
        status = {}
        state = {}

        response = self.emulator._run_telnet("power display")
        for line in response:
            if ":" in line:
                field, value = line.split(":")
                value = value.strip()
                if value == "true":
                    value = True
                elif value == "false":
                    value = False
                elif field == "capacity":
                    value = float(value)
                status[field] = value

        # pylint --py3k W1619
        state["level"] = status.get("capacity", 0.0) / 100
        if status.get("AC") == "online":
            state["charging"] = True
        else:
            state["charging"] = False

        return state

    def get_charging(self):
        return self.get_state()["charging"]

    def get_level(self):
        return self.get_state()["level"]

    def set_level(self, level):
        self.emulator._run_telnet("power capacity %d" % (level * 100))

    def set_charging(self, charging):
        if charging:
            cmd = "power ac on"
        else:
            cmd = "power ac off"
        self.emulator._run_telnet(cmd)

    charging = property(get_charging, set_charging)
    level = property(get_level, set_level)