/
firetv.py
351 lines (291 loc) · 13.9 KB
/
firetv.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Communicate with an Amazon Fire TV device via ADB over a network.
ADB Debugging must be enabled.
"""
import logging
from .basetv import BaseTV
from . import constants
_LOGGER = logging.getLogger(__name__)
class FireTV(BaseTV):
"""Representation of an Amazon Fire 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`)
auth_timeout_s : float
Authentication timeout (in seconds)
"""
DEVICE_CLASS = 'firetv'
def __init__(self, host, port=5555, adbkey='', adb_server_ip='', adb_server_port=5037, state_detection_rules=None, auth_timeout_s=constants.DEFAULT_AUTH_TIMEOUT_S):
BaseTV.__init__(self, host, port, adbkey, adb_server_ip, adb_server_port, state_detection_rules, auth_timeout_s)
# ======================================================================= #
# #
# Home Assistant Update #
# #
# ======================================================================= #
def update(self, get_running_apps=True):
"""Get the info needed for a Home Assistant update.
Parameters
----------
get_running_apps : bool
Whether or not to get the :attr:`~androidtv.basetv.BaseTV.running_apps` property
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]``
"""
# Get the properties needed for the update
screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps = self.get_properties(get_running_apps=get_running_apps, lazy=True)
# Check if device is unavailable
if screen_on is None:
state = None
current_app = None
running_apps = None
# Check if device is off
elif not screen_on:
state = constants.STATE_OFF
current_app = None
running_apps = None
# Check if screen saver is on
elif not awake:
state = constants.STATE_IDLE
current_app = None
running_apps = None
else:
# Get the running apps
if not running_apps and current_app:
running_apps = [current_app]
# Determine the state using custom rules
state = self._custom_state_detection(current_app=current_app, media_session_state=media_session_state, wake_lock_size=wake_lock_size)
if state:
return state, current_app, running_apps
# Determine the state based on the `current_app`
if current_app in [constants.APP_FIRETV_PACKAGE_LAUNCHER, constants.APP_FIRETV_PACKAGE_SETTINGS, None]:
state = constants.STATE_STANDBY
# Amazon Video
elif current_app == constants.APP_AMAZON_VIDEO:
if media_session_state == 2:
state = constants.STATE_PAUSED
elif media_session_state == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Firefox
elif current_app == constants.APP_FIREFOX:
if wake_lock_size == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Hulu
elif current_app == constants.APP_HULU:
if wake_lock_size == 4:
state = constants.STATE_PLAYING
elif wake_lock_size == 2:
state = constants.STATE_PAUSED
else:
state = constants.STATE_STANDBY
# Jellyfin
elif current_app == constants.APP_JELLYFIN_TV:
if wake_lock_size == 2:
state = constants.STATE_PLAYING
else:
state = constants.STATE_PAUSED
# Netflix
elif current_app == constants.APP_NETFLIX:
if media_session_state == 2:
state = constants.STATE_PAUSED
elif media_session_state == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Plex
elif current_app == constants.APP_PLEX:
if media_session_state == 3:
if wake_lock_size == 2:
state = constants.STATE_PAUSED
else:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Sport 1
elif current_app == constants.APP_SPORT1:
if wake_lock_size == 2:
state = constants.STATE_PAUSED
elif wake_lock_size == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Spotify
elif current_app == constants.APP_SPOTIFY:
if media_session_state == 2:
state = constants.STATE_PAUSED
elif media_session_state == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Twitch
elif current_app == constants.APP_TWITCH:
if wake_lock_size == 2:
state = constants.STATE_PAUSED
elif media_session_state == 3:
state = constants.STATE_PLAYING
elif media_session_state == 4:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Waipu TV
elif current_app == constants.APP_WAIPU_TV:
if wake_lock_size == 2:
state = constants.STATE_PAUSED
elif wake_lock_size == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Get the state from `media_session_state`
elif media_session_state:
if media_session_state == 2:
state = constants.STATE_PAUSED
elif media_session_state == 3:
state = constants.STATE_PLAYING
else:
state = constants.STATE_STANDBY
# Get the state from `wake_lock_size`
else:
if wake_lock_size == 1:
state = constants.STATE_PLAYING
else:
state = constants.STATE_PAUSED
return state, current_app, 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_FIRETV_PROPERTIES_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS`
* :py:const:`androidtv.constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS`
Parameters
----------
get_running_apps : bool
Whether or not to get the :attr:`~androidtv.basetv.BaseTV.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
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
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_FIRETV_PROPERTIES_LAZY_RUNNING_APPS)
else:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_LAZY_NO_RUNNING_APPS)
else:
if get_running_apps:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_RUNNING_APPS)
else:
output = self._adb.shell(constants.CMD_FIRETV_PROPERTIES_NOT_LAZY_NO_RUNNING_APPS)
_LOGGER.debug("Fire TV %s:%d `get_properties` response: %s", self.host, self.port, output)
# ADB command was unsuccessful
if output is None:
return None, None, None, None, None, None
# `screen_on` property
if not output:
return False, False, -1, None, None, None
screen_on = output[0] == '1'
# `awake` property
if len(output) < 2:
return screen_on, False, -1, None, None, None
awake = output[1] == '1'
lines = output.strip().splitlines()
# `wake_lock_size` property
if len(lines[0]) < 3:
return screen_on, awake, -1, None, None, None
wake_lock_size = self._wake_lock_size(lines[0])
# `current_app` property
if len(lines) < 2:
return screen_on, awake, wake_lock_size, None, None, None
current_app = self._current_app(lines[1])
# `media_session_state` property
if len(lines) < 3:
return screen_on, awake, wake_lock_size, current_app, None, None
media_session_state = self._media_session_state(lines[2], current_app)
# `running_apps` property
if not get_running_apps or len(lines) < 4:
return screen_on, awake, wake_lock_size, current_app, media_session_state, None
running_apps = self._running_apps(lines[3:])
return screen_on, awake, wake_lock_size, current_app, media_session_state, 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 :attr:`~androidtv.basetv.BaseTV.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'``, and ``'running_apps'``
"""
screen_on, awake, wake_lock_size, current_app, media_session_state, running_apps = self.get_properties(get_running_apps=get_running_apps, lazy=lazy)
return {'screen_on': screen_on,
'awake': awake,
'wake_lock_size': wake_lock_size,
'current_app': current_app,
'media_session_state': media_session_state,
'running_apps': running_apps}
# ======================================================================= #
# #
# Properties #
# #
# ======================================================================= #
@property
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_FIRETV_RUNNING_APPS)
return self._running_apps(running_apps_response)
# ======================================================================= #
# #
# turn on/off methods #
# #
# ======================================================================= #
def turn_on(self):
"""Send ``POWER`` and ``HOME`` actions if the device is off."""
self._adb.shell(constants.CMD_SCREEN_ON + " || (input keyevent {0} && input keyevent {1})".format(constants.KEY_POWER, constants.KEY_HOME))
def turn_off(self):
"""Send ``SLEEP`` action if the device is not off."""
self._adb.shell(constants.CMD_SCREEN_ON + " && input keyevent {0}".format(constants.KEY_SLEEP))