aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik/model/base.py
blob: fdacde054840f2a793ec554da41446931eaf6c1d (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
54
55
56
57
58
59
60
class ParrotZikBase(object):
    def __init__(self, resource_manager):
        self.resource_manager = resource_manager

    @property
    def version(self):
        return self.resource_manager.api_version

    def refresh_battery(self):
        self.resource_manager.fetch('/api/system/battery')

    @property
    def battery_state(self):
        answer = self.resource_manager.get("/api/system/battery")
        return answer.system.battery["state"]

    def get_battery_level(self, field_name):
        answer = self.resource_manager.get("/api/system/battery")
        return int(answer.system.battery[field_name] or 0)

    @property
    def friendly_name(self):
        answer = self.resource_manager.get("/api/bluetooth/friendlyname")
        return answer.bluetooth["friendlyname"]

    @property
    def auto_connect(self):
        answer = self.resource_manager.get("/api/system/auto_connection/enabled")
        return self._result_to_bool(
            answer.system.auto_connection["enabled"])

    @auto_connect.setter
    def auto_connect(self, arg):
        self.resource_manager.set("/api/system/auto_connection/enabled", arg)

    @property
    def anc_phone_mode(self):
        answer = self.resource_manager.get("/api/system/anc_phone_mode/enabled")
        return self._result_to_bool(
            answer.system.anc_phone_mode["enabled"])

    def _result_to_bool(self, result):
        if result == "true":
            return True
        elif result == "false":
            return False
        elif result == "invalid_on":
            return True
        else:
            raise AssertionError(result)

class BatteryStates:
    CHARGED = 'charged'
    IN_USE = 'in_use'
    CHARGING = 'charging'
    representation = {
        CHARGED: 'Charged',
        IN_USE: 'In Use',
        CHARGING: 'Charging',
    }