-
Notifications
You must be signed in to change notification settings - Fork 57
/
androidtv_sync.py
191 lines (158 loc) · 9.12 KB
/
androidtv_sync.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
"""Communicate with an Android TV device via ADB over a network.
ADB Debugging must be enabled.
"""
import logging
from .base_androidtv import BaseAndroidTV
from ..basetv.basetv_sync import BaseTVSync
from .. import constants
_LOGGER = logging.getLogger(__name__)
class AndroidTVSync(BaseTVSync, BaseAndroidTV):
"""Representation of an Android TV device.
Parameters
----------
host : str
The address of the device; may be an IP address or a host name
port : int
The device port to which we are connecting (default is 5555)
adbkey : str
The path to the ``adbkey`` file for ADB authentication
adb_server_ip : str
The IP address of the ADB server
adb_server_port : int
The port for the ADB server
state_detection_rules : dict, None
A dictionary of rules for determining the state (see :class:`~androidtv.basetv.basetv.BaseTV`)
signer : PythonRSASigner, None
The signer for the ADB keys, as loaded by :meth:`androidtv.adb_manager.adb_manager_sync.ADBPythonSync.load_adbkey`
"""
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, signer=None): # pylint: disable=super-init-not-called
BaseTVSync.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, signer)
# ======================================================================= #
# #
# Home Assistant Update #
# #
# ======================================================================= #
def update(self, get_running_apps=True, lazy=True):
"""Get the info needed for a Home Assistant update.
Parameters
----------
get_running_apps : bool
Whether or not to get the :meth:`~androidtv.androidtv.androidtv_sync.AndroidTVSync.running_apps` property
lazy : bool
Whether or not to continue retrieving properties if the device is off or the screensaver is running
Returns
-------
state : str
The state of the device
current_app : str
The current running app
running_apps : list
A list of the running apps if ``get_running_apps`` is True, otherwise the list ``[current_app]``
audio_output_device : str
The current audio playback device
is_volume_muted : bool
Whether or not the volume is muted
volume_level : float
The volume level (between 0 and 1)
"""
# Get the properties needed for the update
screen_on, awake, audio_state, wake_lock_size, current_app, media_session_state, audio_output_device, is_volume_muted, volume, running_apps = self.get_properties(get_running_apps=get_running_apps, lazy=lazy)
return self._update(screen_on, awake, audio_state, wake_lock_size, current_app, media_session_state, audio_output_device, is_volume_muted, volume, running_apps)
# ======================================================================= #
# #
# Properties #
# #
# ======================================================================= #
def get_properties(self, get_running_apps=True, lazy=False):
"""Get the properties needed for Home Assistant updates.
This will send one of the following ADB commands:
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_LAZY_NO_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS``
Parameters
----------
get_running_apps : bool
Whether or not to get the :meth:`~androidtv.androidtv.androidtv_sync.AndroidTVSync.running_apps` property
lazy : bool
Whether or not to continue retrieving properties if the device is off or the screensaver is running
Returns
-------
screen_on : bool, None
Whether or not the device is on, or ``None`` if it was not determined
awake : bool, None
Whether or not the device is awake (screensaver is not running), or ``None`` if it was not determined
audio_state : str, None
The audio state, as determined from "dumpsys audio", or ``None`` if it was not determined
wake_lock_size : int, None
The size of the current wake lock, or ``None`` if it was not determined
current_app : str, None
The current app property, or ``None`` if it was not determined
media_session_state : int, None
The state from the output of ``dumpsys media_session``, or ``None`` if it was not determined
audio_output_device : str, None
The current audio playback device, or ``None`` if it was not determined
is_volume_muted : bool, None
Whether or not the volume is muted, or ``None`` if it was not determined
volume : int, None
The absolute volume level, or ``None`` if it was not determined
running_apps : list, None
A list of the running apps, or ``None`` if it was not determined
"""
if lazy:
if get_running_apps:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_RUNNING_APPS)
else:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_LAZY_NO_RUNNING_APPS)
else:
if get_running_apps:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
else:
output = self._adb.shell(constants.CMD_ANDROIDTV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS if not self._is_google_tv else constants.CMD_GOOGLE_TV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
_LOGGER.debug("Android TV %s:%d `get_properties` response: %s", self.host, self.port, output)
return self._get_properties(output, get_running_apps)
def get_properties_dict(self, get_running_apps=True, lazy=True):
"""Get the properties needed for Home Assistant updates and return them as a dictionary.
Parameters
----------
get_running_apps : bool
Whether or not to get the :meth:`~androidtv.androidtv.androidtv_sync.AndroidTVSync.running_apps` property
lazy : bool
Whether or not to continue retrieving properties if the device is off or the screensaver is running
Returns
-------
dict
A dictionary with keys ``'screen_on'``, ``'awake'``, ``'wake_lock_size'``, ``'current_app'``,
``'media_session_state'``, ``'audio_state'``, ``'audio_output_device'``, ``'is_volume_muted'``, ``'volume'``, and ``'running_apps'``
"""
screen_on, awake, audio_state, wake_lock_size, current_app, media_session_state, audio_output_device, is_volume_muted, volume, running_apps = self.get_properties(get_running_apps=get_running_apps, lazy=lazy)
return {'screen_on': screen_on,
'awake': awake,
'audio_state': audio_state,
'wake_lock_size': wake_lock_size,
'current_app': current_app,
'media_session_state': media_session_state,
'audio_output_device': audio_output_device,
'is_volume_muted': is_volume_muted,
'volume': volume,
'running_apps': running_apps}
def running_apps(self):
"""Return a list of running user applications.
Returns
-------
list
A list of the running apps
"""
running_apps_response = self._adb.shell(constants.CMD_RUNNING_APPS_ANDROIDTV)
return self._running_apps(running_apps_response)
# ======================================================================= #
# #
# turn on/off methods #
# #
# ======================================================================= #
def turn_on(self):
"""Send ``POWER`` action if the device is off."""
self._adb.shell(constants.CMD_SCREEN_ON + " || input keyevent {0}".format(constants.KEY_POWER))
def turn_off(self):
"""Send ``POWER`` action if the device is not off."""
self._adb.shell(constants.CMD_SCREEN_ON + " && input keyevent {0}".format(constants.KEY_POWER))