/
test_homeassistant.py
579 lines (463 loc) · 19.5 KB
/
test_homeassistant.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import functools
import logging
import sys
import unittest
from unittest.mock import patch
sys.path.insert(0, '..')
from adb_shell.exceptions import (
InvalidChecksumError,
InvalidCommandError,
InvalidResponseError,
TcpTimeoutException,
)
from aio_androidtv import setup
from aio_androidtv.constants import APPS, KEYS, STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY
from aio_androidtv.exceptions import LockNotAcquiredException
from . import patchers
from .async_wrapper import awaiter
_LOGGER = logging.getLogger(__name__)
SUPPORT_ANDROIDTV = 12345
SUPPORT_FIRETV = 12345
DIRECTION_PULL = "pull"
DIRECTION_PUSH = "push"
class MediaPlayerDevice(object):
_unique_id = None
@staticmethod
def schedule_update_ha_state():
pass
# =========================================================================== #
# #
# media_player.py #
# #
# =========================================================================== #
# Translate from `AndroidTV` / `FireTV` reported state to HA state.
ANDROIDTV_STATES = {
"off": STATE_OFF,
"idle": STATE_IDLE,
"standby": STATE_STANDBY,
"playing": STATE_PLAYING,
"paused": STATE_PAUSED,
}
def adb_decorator(override_available=False):
"""Wrap ADB methods and catch exceptions.
Allows for overriding the available status of the ADB connection via the
`override_available` parameter.
"""
def _adb_decorator(func):
"""Wrap the provided ADB method and catch exceptions."""
@functools.wraps(func)
async def _adb_exception_catcher(self, *args, **kwargs):
"""Call an ADB-related method and catch exceptions."""
if not self.available and not override_available:
return None
try:
return await func(self, *args, **kwargs)
except LockNotAcquiredException:
# If the ADB lock could not be acquired, skip this command
return
except self.exceptions as err:
_LOGGER.error(
"Failed to execute an ADB command. ADB connection re-"
"establishing attempt in the next update. Error: %s",
err,
)
await self.aftv.adb_close()
self._available = False # pylint: disable=protected-access
return None
return _adb_exception_catcher
return _adb_decorator
class ADBDevice(MediaPlayerDevice):
"""Representation of an Android TV or Fire TV device."""
def __init__(
self, aftv, name, apps, get_sources, turn_on_command, turn_off_command
):
"""Initialize the Android TV / Fire TV device."""
self.aftv = aftv
self._name = name
self._app_id_to_name = APPS.copy()
self._app_id_to_name.update(apps)
self._app_name_to_id = {
value: key for key, value in self._app_id_to_name.items()
}
self._get_sources = get_sources
self._keys = KEYS
self._device_properties = self.aftv.device_properties
self._unique_id = self._device_properties.get("serialno")
self.turn_on_command = turn_on_command
self.turn_off_command = turn_off_command
# ADB exceptions to catch
if not self.aftv.adb_server_ip:
# Using "adb_shell" (Python ADB implementation)
self.exceptions = (
AttributeError,
BrokenPipeError,
TypeError,
ValueError,
InvalidChecksumError,
InvalidCommandError,
InvalidResponseError,
TcpTimeoutException,
)
else:
# Using "pure-python-adb" (communicate with ADB server)
self.exceptions = (ConnectionResetError, RuntimeError)
# Property attributes
self._adb_response = None
self._available = True
self._current_app = None
self._sources = None
self._state = None
@property
def app_id(self):
"""Return the current app."""
return self._current_app
@property
def app_name(self):
"""Return the friendly name of the current app."""
return self._app_id_to_name.get(self._current_app, self._current_app)
@property
def available(self):
"""Return whether or not the ADB connection is valid."""
return self._available
@property
def device_state_attributes(self):
"""Provide the last ADB command's response as an attribute."""
return {"adb_response": self._adb_response}
@property
def name(self):
"""Return the device name."""
return self._name
@property
def should_poll(self):
"""Device should be polled."""
return True
@property
def source(self):
"""Return the current app."""
return self._app_id_to_name.get(self._current_app, self._current_app)
@property
def source_list(self):
"""Return a list of running apps."""
return self._sources
@property
def state(self):
"""Return the state of the player."""
return self._state
@property
def unique_id(self):
"""Return the device unique id."""
return self._unique_id
@adb_decorator()
async def media_play(self):
"""Send play command."""
await self.aftv.media_play()
@adb_decorator()
async def media_pause(self):
"""Send pause command."""
await self.aftv.media_pause()
@adb_decorator()
async def media_play_pause(self):
"""Send play/pause command."""
await self.aftv.media_play_pause()
@adb_decorator()
async def turn_on(self):
"""Turn on the device."""
if self.turn_on_command:
await self.aftv.adb_shell(self.turn_on_command)
else:
await self.aftv.turn_on()
@adb_decorator()
async def turn_off(self):
"""Turn off the device."""
if self.turn_off_command:
await self.aftv.adb_shell(self.turn_off_command)
else:
await self.aftv.turn_off()
@adb_decorator()
async def media_previous_track(self):
"""Send previous track command (results in rewind)."""
await self.aftv.media_previous_track()
@adb_decorator()
async def media_next_track(self):
"""Send next track command (results in fast-forward)."""
await self.aftv.media_next_track()
@adb_decorator()
async def select_source(self, source):
"""Select input source.
If the source starts with a '!', then it will close the app instead of
opening it.
"""
if isinstance(source, str):
if not source.startswith("!"):
await self.aftv.launch_app(self._app_name_to_id.get(source, source))
else:
source_ = source[1:].lstrip()
await self.aftv.stop_app(self._app_name_to_id.get(source_, source_))
@adb_decorator()
async def adb_command(self, cmd):
"""Send an ADB command to an Android TV / Fire TV device."""
key = self._keys.get(cmd)
if key:
await self.aftv.adb_shell("input keyevent {}".format(key))
self._adb_response = None
self.schedule_update_ha_state()
return
if cmd == "GET_PROPERTIES":
self._adb_response = str(await self.aftv.get_properties_dict())
self.schedule_update_ha_state()
return self._adb_response
response = await self.aftv.adb_shell(cmd)
if isinstance(response, str) and response.strip():
self._adb_response = response.strip()
else:
self._adb_response = None
self.schedule_update_ha_state()
return self._adb_response
@adb_decorator()
async def adb_filesync(self, direction, local_path, device_path):
"""Transfer a file between your HA instance and an Android TV / Fire TV device."""
if direction == DIRECTION_PULL:
await self.aftv.adb_pull(local_path, device_path)
else:
await self.aftv.adb_push(local_path, device_path)
class AndroidTVDevice(ADBDevice):
"""Representation of an Android TV device."""
def __init__(
self, aftv, name, apps, get_sources, turn_on_command, turn_off_command
):
"""Initialize the Android TV device."""
super().__init__(
aftv, name, apps, get_sources, turn_on_command, turn_off_command
)
self._is_volume_muted = None
self._volume_level = None
@adb_decorator(override_available=True)
async def update(self):
"""Update the device state and, if necessary, re-connect."""
# Check if device is disconnected.
if not self._available:
# Try to connect
self._available = await self.aftv.adb_connect(always_log_errors=False)
# To be safe, wait until the next update to run ADB commands if
# using the Python ADB implementation.
if not self.aftv.adb_server_ip:
return
# If the ADB connection is not intact, don't update.
if not self._available:
return
# Get the updated state and attributes.
(
state,
self._current_app,
running_apps,
_,
self._is_volume_muted,
self._volume_level,
) = await self.aftv.update(self._get_sources)
self._state = ANDROIDTV_STATES.get(state)
if self._state is None:
self._available = False
if running_apps:
self._sources = [
self._app_id_to_name.get(app_id, app_id) for app_id in running_apps
]
else:
self._sources = None
@property
def is_volume_muted(self):
"""Boolean if volume is currently muted."""
return self._is_volume_muted
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_ANDROIDTV
@property
def volume_level(self):
"""Return the volume level."""
return self._volume_level
@adb_decorator()
async def media_stop(self):
"""Send stop command."""
await self.aftv.media_stop()
@adb_decorator()
async def mute_volume(self, mute):
"""Mute the volume."""
await self.aftv.mute_volume()
@adb_decorator()
async def volume_down(self):
"""Send volume down command."""
self._volume_level = await self.aftv.volume_down(self._volume_level)
@adb_decorator()
async def volume_up(self):
"""Send volume up command."""
self._volume_level = await self.aftv.volume_up(self._volume_level)
class FireTVDevice(ADBDevice):
"""Representation of a Fire TV device."""
@adb_decorator(override_available=True)
async def update(self):
"""Update the device state and, if necessary, re-connect."""
# Check if device is disconnected.
if not self._available:
# Try to connect
self._available = await self.aftv.adb_connect(always_log_errors=False)
# To be safe, wait until the next update to run ADB commands if
# using the Python ADB implementation.
if not self.aftv.adb_server_ip:
return
# If the ADB connection is not intact, don't update.
if not self._available:
return
# Get the `state`, `current_app`, and `running_apps`.
state, self._current_app, running_apps = await self.aftv.update(self._get_sources)
self._state = ANDROIDTV_STATES.get(state)
if self._state is None:
self._available = False
if running_apps:
self._sources = [
self._app_id_to_name.get(app_id, app_id) for app_id in running_apps
]
else:
self._sources = None
@property
def supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_FIRETV
@adb_decorator()
async def media_stop(self):
"""Send stop (back) command."""
await self.aftv.back()
# =========================================================================== #
# #
# test_media_player.py #
# #
# =========================================================================== #
class TestAndroidTVPythonImplementation(unittest.TestCase):
"""Test the androidtv media player for an Android TV device."""
PATCH_KEY = "python"
@awaiter
async def setUp(self):
"""Set up an `AndroidTVDevice` media player."""
with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[self.PATCH_KEY]:
aftv = await setup("HOST", 5555, device_class="androidtv")
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
@awaiter
async def test_reconnect(self):
"""Test that the error and reconnection attempts are logged correctly.
"Handles device/service unavailable. Log a warning once when
unavailable, log once when reconnected."
https://developers.home-assistant.io/docs/en/integration_quality_scale_index.html
"""
with self.assertLogs(level=logging.WARNING) as logs:
with patchers.patch_connect(False)[self.PATCH_KEY], patchers.patch_shell(error=True)[self.PATCH_KEY]:
for _ in range(5):
await self.aftv.update()
self.assertFalse(self.aftv.available)
self.assertIsNone(self.aftv.state)
assert len(logs.output) == 2
assert logs.output[0].startswith("ERROR")
assert logs.output[1].startswith("WARNING")
with self.assertLogs(level=logging.DEBUG) as logs:
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[self.PATCH_KEY]:
# Update 1 will reconnect
await self.aftv.update()
self.assertTrue(self.aftv.available)
# Update 2 will update the state
await self.aftv.update()
self.assertTrue(self.aftv.available)
self.assertIsNotNone(self.aftv.state)
assert (
"ADB connection to {}:{} successfully established".format(self.aftv.aftv.host, self.aftv.aftv.port)
in logs.output[0]
)
@awaiter
async def test_adb_shell_returns_none(self):
"""Test the case that the ADB shell command returns `None`.
The state should be `None` and the device should be unavailable.
"""
with patchers.patch_shell(None)[self.PATCH_KEY]:
await self.aftv.update()
self.assertFalse(self.aftv.available)
self.assertIsNone(self.aftv.state)
with patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[self.PATCH_KEY]:
# Update 1 will reconnect
await self.aftv.update()
self.assertTrue(self.aftv.available)
# Update 2 will update the state
await self.aftv.update()
self.assertTrue(self.aftv.available)
self.assertIsNotNone(self.aftv.state)
class TestFireTVPythonImplementation(TestAndroidTVPythonImplementation):
"""Test the androidtv media player for a Fire TV device."""
@awaiter
async def setUp(self):
"""Set up a `FireTVDevice` media player."""
with patchers.PATCH_ADB_DEVICE_TCP, patchers.patch_connect(True)[self.PATCH_KEY], patchers.patch_shell("")[self.PATCH_KEY]:
aftv = await setup("HOST", 5555, device_class="firetv")
self.aftv = FireTVDevice(aftv, "Fake Fire TV", {}, True, None, None)
@unittest.skip
class TestADBCommandAndFileSync(unittest.TestCase):
"""Test ADB and FileSync services."""
@awaiter
async def test_adb_command(self):
"""Test sending a command via the `androidtv.adb_command` service."""
patch_key = "server"
command = "test command"
response = "test response"
with patchers.patch_connect(True)[patch_key], patchers.patch_shell("")[patch_key]:
aftv = setup(
"HOST", 5555, adb_server_ip="ADB_SERVER_IP", device_class="androidtv"
)
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
with patch("aio_androidtv.basetv.BaseTV.adb_shell", return_value=response) as patch_shell:
await self.aftv.adb_command(command)
patch_shell.assert_called_with(command)
assert self.aftv._adb_response == response
@awaiter
async def test_adb_command_key(self):
"""Test sending a key command via the `androidtv.adb_command` service."""
patch_key = "server"
command = "HOME"
response = None
with patchers.patch_connect(True)[patch_key], patchers.patch_shell("")[patch_key]:
aftv = setup(
"HOST", 5555, adb_server_ip="ADB_SERVER_IP", device_class="androidtv"
)
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
with patch("aio_androidtv.basetv.BaseTV.adb_shell", return_value=response) as patch_shell:
self.aftv.adb_command(command)
patch_shell.assert_called_with("input keyevent {}".format(self.aftv._keys[command]))
assert self.aftv._adb_response is None
@awaiter
async def test_adb_command_get_properties(self):
"""Test sending the "GET_PROPERTIES" command via the `androidtv.adb_command` service."""
patch_key = "server"
command = "GET_PROPERTIES"
response = {"key": "value"}
with patchers.patch_connect(True)[patch_key], patchers.patch_shell("")[patch_key]:
aftv = setup(
"HOST", 5555, adb_server_ip="ADB_SERVER_IP", device_class="androidtv"
)
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
with patch("aio_androidtv.androidtv.AndroidTV.get_properties_dict", return_value=response) as patch_get_props:
self.aftv.adb_command(command)
assert patch_get_props.called
assert self.aftv._adb_response == str(response)
@awaiter
async def test_update_lock_not_acquired(self):
"""Test that the state does not get updated when a `LockNotAcquiredException` is raised."""
patch_key = "server"
with patchers.patch_connect(True)[patch_key], patchers.patch_shell("")[patch_key]:
aftv = setup(
"HOST", 5555, adb_server_ip="ADB_SERVER_IP", device_class="androidtv"
)
self.aftv = AndroidTVDevice(aftv, "Fake Android TV", {}, True, None, None)
with patchers.patch_shell("")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_OFF
with patch("aio_androidtv.androidtv.AndroidTV.update", side_effect=LockNotAcquiredException):
with patchers.patch_shell("1")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_OFF
with patchers.patch_shell("1")[patch_key]:
self.aftv.update()
assert self.aftv.state == STATE_IDLE