summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/tfc/device_info.py
blob: 6a1b35d8bef394efacc581ca4d228eb6b1a5fc0e (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
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
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from host_controller.tfc import api_message
from vts.utils.python.controllers import android_device


class DeviceInfo(api_message.ApiMessage):
    """The device information defined by TFC API.

    Attributes:
        _DEVICE_SNAPSHOT: The parameters of host_events.
        _LEASE_HOST_TASKS: The parameters of leasehosttasks.
        _OTHER: The data retrieved from TradeFed host but not used by the API.
        _ALL_KEYS: Union of above.
    """
    _DEVICE_SNAPSHOT = {
            "battery_level",
            "build_id",
            "device_serial",
            "group_name",
            "mac_address",
            "product",
            "product_variant",
            "run_target",
            "sim_operator",
            "sim_state",
            "sdk_version",
            "state"}
    _LEASE_HOST_TASKS = {
            "device_serial",
            "group_name",
            "run_target",
            "state"}
    _OTHER = {"stub"}
    _ALL_KEYS = (_DEVICE_SNAPSHOT | _LEASE_HOST_TASKS | _OTHER)

    def __init__(self, device_serial, **kwargs):
        """Initializes the attributes.

        Args:
            device_serial: The serial number of the device.
            **kwargs: The optional attributes.
        """
        super(DeviceInfo, self).__init__(self._ALL_KEYS,
                                         device_serial=device_serial, **kwargs)

    def IsAvailable(self):
        """Returns whether the device is available for running commands.

        Returns:
            A boolean.
        """
        return getattr(self, "state", None) == "Available"

    def IsStub(self):
        """Returns whether the device is a stub.

        Returns:
            A boolean.
        """
        return getattr(self, "stub", False)

    def ToDeviceSnapshotJson(self):
        """Converts to the parameter of host_events.

        Returns:
            A JSON object.
        """
        return self.ToJson(self._DEVICE_SNAPSHOT)

    def ToLeaseHostTasksJson(self):
        """Converts to the parameter of leasehosttasks.

        Returns:
            A JSON object.
        """
        return self.ToJson(self._LEASE_HOST_TASKS)

    def Extend(self, properties):
        """Adds properties to a DeviceInfo object, using AndroidDevice

        Args:
            properties: list of strings, list of properties to update
        """
        serial = getattr(self, "device_serial", None)
        ad = android_device.AndroidDevice(serial)
        for prop in properties:
            val = getattr(ad, prop, None)
            if val is None:
                continue
            setattr(self, prop, val)