File: basetv.py

package info (click to toggle)
python-androidtv 0.0.73-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 792 kB
  • sloc: python: 7,123; makefile: 188; sh: 105
file content (1007 lines) | stat: -rw-r--r-- 36,641 bytes parent folder | download
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
"""Communicate with an Android TV or Amazon Fire TV device via ADB over a network.

ADB Debugging must be enabled.
"""


import logging
import re

from .. import constants

_LOGGER = logging.getLogger(__name__)


class BaseTV(object):  # pylint: disable=too-few-public-methods
    """Base class for representing an Android TV / Fire TV device.

    The ``state_detection_rules`` parameter is of the format:

    .. code-block:: python

       state_detection_rules = {'com.amazon.tv.launcher': ['idle'],
                                'com.netflix.ninja': ['media_session_state'],
                                'com.ellation.vrv': ['audio_state'],
                                'com.hulu.plus': [{'playing': {'wake_lock_size' : 4}},
                                                  {'paused': {'wake_lock_size': 2}}],
                                'com.plexapp.android': [{'paused': {'media_session_state': 3, 'wake_lock_size': 1}},
                                                        {'playing': {'media_session_state': 3}},
                                                        'idle']}

    The keys are app IDs, and the values are lists of rules that are evaluated in order.

    :py:const:`~androidtv.constants.VALID_STATES`

    .. code-block:: python

       VALID_STATES = ('idle', 'off', 'playing', 'paused', 'standby')


    **Valid rules:**

    * ``'idle'``, ``'playing'``, ``'paused'``, ``'standby'``, or ``'off'`` = always report the specified state when this app is open
    * ``'media_session_state'`` = try to use the :meth:`media_session_state` property to determine the state
    * ``'audio_state'`` = try to use the :meth:`audio_state` property to determine the state
    * ``{'<VALID_STATE>': {'<PROPERTY1>': VALUE1, '<PROPERTY2>': VALUE2, ...}}`` = check if each of the properties is equal to the specified value, and if so return the state

      * The valid properties are ``'media_session_state'``, ``'audio_state'``, and ``'wake_lock_size'``


    Parameters
    ----------
    adb : ADBPythonSync, ADBServerSync, ADBPythonAsync, ADBServerAsync
        The handler for ADB commands
    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 above)

    """

    DEVICE_ENUM = constants.DeviceEnum.BASETV

    def __init__(
        self,
        adb,
        host,
        port=5555,
        adbkey="",
        adb_server_ip="",
        adb_server_port=5037,
        state_detection_rules=None,
    ):
        self._adb = adb
        self.host = host
        self.port = int(port)
        self.adbkey = adbkey
        self.adb_server_ip = adb_server_ip
        self.adb_server_port = adb_server_port
        self._state_detection_rules = state_detection_rules
        self.device_properties = {}
        self.installed_apps = []

        # make sure the rules are valid
        if self._state_detection_rules:
            for app_id, rules in self._state_detection_rules.items():
                if not isinstance(app_id, str):
                    raise TypeError("{0} is of type {1}, not str".format(app_id, type(app_id).__name__))
                state_detection_rules_validator(rules)

        # the max volume level (determined when first getting the volume level)
        self.max_volume = None

        # Customizable commands
        self._custom_commands = {}

    # ======================================================================= #
    #                                                                         #
    #                      Device-specific ADB commands                       #
    #                                                                         #
    # ======================================================================= #
    def customize_command(self, custom_command, value):
        """Customize a command used to retrieve properties.

        Parameters
        ----------
        custom_command : str
            The name of the command that will be customized; it must be in `constants.CUSTOMIZABLE_COMMANDS`
        value : str, None
            The custom ADB command that will be used, or ``None`` if the custom command should be deleted

        """
        if custom_command in constants.CUSTOMIZABLE_COMMANDS:
            if value is not None:
                self._custom_commands[custom_command] = value
            elif custom_command in self._custom_commands:
                del self._custom_commands[custom_command]

    def _cmd_audio_state(self):
        """Get the command used to retrieve the current audio state for this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to determine the current audio state

        """
        if constants.CUSTOM_AUDIO_STATE in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_AUDIO_STATE]

        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_AUDIO_STATE11

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_AUDIO_STATE11

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_AUDIO_STATE11
        return constants.CMD_AUDIO_STATE

    def _cmd_current_app(self):
        """Get the command used to retrieve the current app for this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to determine the current app

        """
        if constants.CUSTOM_CURRENT_APP in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_CURRENT_APP]

        # Is this a Google Chromecast Android TV?
        if (
            self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV
            and "Google" in self.device_properties.get("manufacturer", "")
            and "Chromecast" in self.device_properties.get("model", "")
        ):
            return constants.CMD_CURRENT_APP_GOOGLE_TV

        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_CURRENT_APP11

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_CURRENT_APP12

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_CURRENT_APP13

        return constants.CMD_CURRENT_APP

    def _cmd_current_app_media_session_state(self):
        """Get the command used to retrieve the current app and media session state for this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to determine the current app and media session state

        """
        if constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_CURRENT_APP_MEDIA_SESSION_STATE]

        # Is this a Google Chromecast Android TV?
        if (
            self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV
            and "Google" in self.device_properties.get("manufacturer", "")
            and "Chromecast" in self.device_properties.get("model", "")
        ):
            return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV

        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE12

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE13

        return constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE

    def _cmd_hdmi_input(self):
        """Get the command used to retrieve the current HDMI input for this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to determine the current HDMI input

        """
        if constants.CUSTOM_HDMI_INPUT in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_HDMI_INPUT]

        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_HDMI_INPUT11

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_HDMI_INPUT11

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_HDMI_INPUT11

        return constants.CMD_HDMI_INPUT

    def _cmd_volume_set(self, new_volume):
        """Get the command used to set volume for this device.

        Parameters
        ----------
        new_volume : int
            The new volume level

        Returns
        -------
        str
            The device-specific ADB shell command used to set volume

        """
        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_VOLUME_SET_COMMAND11.format(new_volume)

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_VOLUME_SET_COMMAND11.format(new_volume)

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_VOLUME_SET_COMMAND11.format(new_volume)

        return constants.CMD_VOLUME_SET_COMMAND.format(new_volume)

    def _cmd_launch_app(self, app):
        """Get the command to launch the specified app for this device.

        Parameters
        ----------
        app : str
            The app that will be launched

        Returns
        -------
        str
            The device-specific command to launch the app

        """
        if constants.CUSTOM_LAUNCH_APP in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_LAUNCH_APP].format(app)

        # Is this a Google Chromecast Android TV?
        if (
            self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV
            and "Google" in self.device_properties.get("manufacturer", "")
            and "Chromecast" in self.device_properties.get("model", "")
        ):
            return constants.CMD_LAUNCH_APP_GOOGLE_TV.format(app)

        if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
            return constants.CMD_LAUNCH_APP_FIRETV.format(app)

        # Is this an Android 11 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "11":
            return constants.CMD_LAUNCH_APP11.format(app)

        # Is this an Android 12 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "12":
            return constants.CMD_LAUNCH_APP12.format(app)

        # Is this an Android 13 device?
        if self.DEVICE_ENUM == constants.DeviceEnum.ANDROIDTV and self.device_properties.get("sw_version", "") == "13":
            return constants.CMD_LAUNCH_APP13.format(app)

        return constants.CMD_LAUNCH_APP.format(app)

    def _cmd_running_apps(self):
        """Get the command used to retrieve the running apps for this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to determine the running apps

        """
        if constants.CUSTOM_RUNNING_APPS in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_RUNNING_APPS]

        if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
            return constants.CMD_RUNNING_APPS_FIRETV

        return constants.CMD_RUNNING_APPS_ANDROIDTV

    def _cmd_turn_off(self):
        """Get the command used to turn off this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to turn off the device

        """
        if constants.CUSTOM_TURN_OFF in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_TURN_OFF]

        if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
            return constants.CMD_TURN_OFF_FIRETV

        return constants.CMD_TURN_OFF_ANDROIDTV

    def _cmd_turn_on(self):
        """Get the command used to turn on this device.

        Returns
        -------
        str
            The device-specific ADB shell command used to turn on the device

        """
        if constants.CUSTOM_TURN_ON in self._custom_commands:
            return self._custom_commands[constants.CUSTOM_TURN_ON]

        if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
            return constants.CMD_TURN_ON_FIRETV

        return constants.CMD_TURN_ON_ANDROIDTV

    # ======================================================================= #
    #                                                                         #
    #                               ADB methods                               #
    #                                                                         #
    # ======================================================================= #
    @property
    def available(self):
        """Whether the ADB connection is intact.

        Returns
        -------
        bool
            Whether or not the ADB connection is intact

        """
        return self._adb.available

    @staticmethod
    def _remove_adb_shell_prefix(cmd):
        """Remove the 'adb shell ' prefix from ``cmd``, if present.

        Parameters
        ----------
        cmd : str
            The ADB shell command

        Returns
        -------
        str
            ``cmd`` with the 'adb shell ' prefix removed, if it was present

        """
        return cmd[len("adb shell ") :] if cmd.startswith("adb shell ") else cmd

    # ======================================================================= #
    #                                                                         #
    #                        Home Assistant device info                       #
    #                                                                         #
    # ======================================================================= #
    def _parse_device_properties(self, properties):
        """Return a dictionary of device properties.

        Parameters
        ----------
        properties : str, None
            The output of the ADB command that retrieves the device properties

        This method fills in the ``device_properties`` attribute, which is a dictionary with keys
        ``'serialno'``, ``'manufacturer'``, ``'model'``, and ``'sw_version'``

        """
        _LOGGER.debug(
            "%s:%d `get_device_properties` response: %s",
            self.host,
            self.port,
            properties,
        )

        if not properties:
            self.device_properties = {}
            return

        lines = properties.strip().splitlines()
        if len(lines) != 4:
            self.device_properties = {}
            return

        manufacturer, model, serialno, version = lines

        if not serialno.strip():
            _LOGGER.warning(
                "Could not obtain serialno for %s:%d, got: '%s'",
                self.host,
                self.port,
                serialno,
            )
            serialno = None

        self.device_properties = {
            "manufacturer": manufacturer,
            "model": model,
            "serialno": serialno,
            "sw_version": version,
        }

    @staticmethod
    def _parse_mac_address(mac_response):
        """Parse a MAC address from the ADB shell response.

        Parameters
        ----------
        mac_response : str, None
            The response from the MAC address ADB shell command

        Returns
        -------
        str, None
            The parsed MAC address, or ``None`` if it could not be determined

        """
        if not mac_response:
            return None

        mac_matches = re.findall(constants.MAC_REGEX_PATTERN, mac_response)
        if mac_matches:
            return mac_matches[0]

        return None

    # ======================================================================= #
    #                                                                         #
    #                         Custom state detection                          #
    #                                                                         #
    # ======================================================================= #
    def _custom_state_detection(
        self,
        current_app=None,
        media_session_state=None,
        wake_lock_size=None,
        audio_state=None,
    ):
        """Use the rules in ``self._state_detection_rules`` to determine the state.

        Parameters
        ----------
        current_app : str, None
            The :meth:`current_app` property
        media_session_state : int, None
            The :meth:`media_session_state` property
        wake_lock_size : int, None
            The :meth:`wake_lock_size` property
        audio_state : str, None
            The :meth:`audio_state` property

        Returns
        -------
        str, None
            The state, if it could be determined using the rules in ``self._state_detection_rules``; otherwise, ``None``

        """
        if not self._state_detection_rules or current_app is None or current_app not in self._state_detection_rules:
            return None

        rules = self._state_detection_rules[current_app]

        for rule in rules:
            # The state is always the same for this app
            if rule in constants.VALID_STATES:
                return rule

            # Use the `media_session_state` property
            if rule == "media_session_state":
                if media_session_state == 2:
                    return constants.STATE_PAUSED
                if media_session_state == 3:
                    return constants.STATE_PLAYING
                if media_session_state is not None:
                    return constants.STATE_IDLE

            # Use the `audio_state` property
            if rule == "audio_state" and audio_state in constants.VALID_STATES:
                return audio_state

            # Check conditions and if they are true, return the specified state
            if isinstance(rule, dict):
                for state, conditions in rule.items():
                    if state in constants.VALID_STATES and self._conditions_are_true(
                        conditions, media_session_state, wake_lock_size, audio_state
                    ):
                        return state

        return None

    @staticmethod
    def _conditions_are_true(conditions, media_session_state=None, wake_lock_size=None, audio_state=None):
        """Check whether the conditions in ``conditions`` are true.

        Parameters
        ----------
        conditions : dict
            A dictionary of conditions to be checked (see the ``state_detection_rules`` parameter in :class:`~androidtv.basetv.basetv.BaseTV`)
        media_session_state : int, None
            The :meth:`media_session_state` property
        wake_lock_size : int, None
            The :meth:`wake_lock_size` property
        audio_state : str, None
            The :meth:`audio_state` property

        Returns
        -------
        bool
            Whether or not all the conditions in ``conditions`` are true

        """
        for key, val in conditions.items():
            if key == "media_session_state":
                if media_session_state is None or media_session_state != val:
                    return False

            elif key == "wake_lock_size":
                if wake_lock_size is None or wake_lock_size != val:
                    return False

            elif key == "audio_state":
                if audio_state is None or audio_state != val:
                    return False

            # key is invalid
            else:
                return False

        return True

    # ======================================================================= #
    #                                                                         #
    #                            Parse properties                             #
    #                                                                         #
    # ======================================================================= #
    @staticmethod
    def _audio_output_device(stream_music):
        """Get the current audio playback device from the ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``.

        Parameters
        ----------
        stream_music : str, None
            The ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``

        Returns
        -------
        str, None
            The current audio playback device, or ``None`` if it could not be determined

        """
        if not stream_music:
            return None

        matches = re.findall(constants.DEVICE_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE)
        if matches:
            return matches[0]

        return None

    @staticmethod
    def _audio_state(audio_state_response):
        """Parse the :meth:`audio_state` property from the ADB shell output.

        Parameters
        ----------
        audio_state_response : str, None
            The output from the ADB command `androidtv.basetv.basetv.BaseTV._cmd_audio_state``

        Returns
        -------
        str, None
            The audio state, or ``None`` if it could not be determined

        """
        if not audio_state_response:
            return None
        if audio_state_response == "1":
            return constants.STATE_PAUSED
        if audio_state_response == "2":
            return constants.STATE_PLAYING
        return constants.STATE_IDLE

    @staticmethod
    def _current_app(current_app_response):
        """Get the current app from the output of the command `androidtv.basetv.basetv.BaseTV._cmd_current_app`.

        Parameters
        ----------
        current_app_response : str, None
            The output from the ADB command `androidtv.basetv.basetv.BaseTV._cmd_current_app`

        Returns
        -------
        str, None
            The current app, or ``None`` if it could not be determined

        """
        if not current_app_response or "=" in current_app_response or "{" in current_app_response:
            return None

        return current_app_response

    def _current_app_media_session_state(self, current_app_media_session_state_response):
        """Get the current app and the media session state properties from the output of `androidtv.basetv.basetv.BaseTV._cmd_current_app_media_session_state`.

        Parameters
        ----------
        current_app_media_session_state_response : str, None
            The output of `androidtv.basetv.basetv.BaseTV._cmd_current_app_media_session_state`

        Returns
        -------
        current_app : str, None
            The current app, or ``None`` if it could not be determined
        media_session_state : int, None
            The state from the output of the ADB shell command, or ``None`` if it could not be determined

        """
        if not current_app_media_session_state_response:
            return None, None

        lines = current_app_media_session_state_response.splitlines()

        current_app = self._current_app(lines[0].strip())

        if len(lines) > 1:
            matches = constants.REGEX_MEDIA_SESSION_STATE.search(current_app_media_session_state_response)
            if matches:
                return current_app, int(matches.group("state"))

        return current_app, None

    @staticmethod
    def _get_hdmi_input(hdmi_response):
        """Get the HDMI input from the from the ADB shell output`.

        Parameters
        ----------
        hdmi_response : str, None
            The output from the ADB command `androidtv.basetv.basetv.BaseTV._cmd_hdmi_input``

        Returns
        -------
        str, None
            The HDMI input, or ``None`` if it could not be determined

        """
        return hdmi_response.strip() if hdmi_response and hdmi_response.strip() else None

    @staticmethod
    def _get_installed_apps(installed_apps_response):
        """Get the installed apps from the output of :py:const:`androidtv.constants.CMD_INSTALLED_APPS`.

        Parameters
        ----------
        installed_apps_response : str, None
            The output of :py:const:`androidtv.constants.CMD_INSTALLED_APPS`

        Returns
        -------
        list, None
            A list of the installed apps, or ``None`` if it could not be determined

        """
        if installed_apps_response is not None:
            return [
                line.strip().rsplit("package:", 1)[-1] for line in installed_apps_response.splitlines() if line.strip()
            ]

        return None

    @staticmethod
    def _is_volume_muted(stream_music):
        """Determine whether or not the volume is muted from the ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``.

        Parameters
        ----------
        stream_music : str, None
            The ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``

        Returns
        -------
        bool, None
            Whether or not the volume is muted, or ``None`` if it could not be determined

        """
        if not stream_music:
            return None

        matches = re.findall(constants.MUTED_REGEX_PATTERN, stream_music, re.DOTALL | re.MULTILINE)
        if matches:
            return matches[0] == "true"

        return None

    @staticmethod
    def _parse_stream_music(stream_music_raw):
        """Parse the output of the command :py:const:`androidtv.constants.CMD_STREAM_MUSIC`.

        Parameters
        ----------
        stream_music_raw : str, None
            The output of the command :py:const:`androidtv.constants.CMD_STREAM_MUSIC`

        Returns
        -------
        str, None
            The ``STREAM_MUSIC`` block from the output of :py:const:`androidtv.constants.CMD_STREAM_MUSIC`, or ``None`` if it could not be determined

        """
        if not stream_music_raw:
            return None

        matches = re.findall(
            constants.STREAM_MUSIC_REGEX_PATTERN,
            stream_music_raw,
            re.DOTALL | re.MULTILINE,
        )
        if matches:
            return matches[0]

        return None

    @staticmethod
    def _running_apps(running_apps_response):
        """Get the running apps from the output of :py:const:`androidtv.constants.CMD_RUNNING_APPS`.

        Parameters
        ----------
        running_apps_response : str, None
            The output of :py:const:`androidtv.constants.CMD_RUNNING_APPS`

        Returns
        -------
        list, None
            A list of the running apps, or ``None`` if it could not be determined

        """
        if running_apps_response:
            return [line.strip().rsplit(" ", 1)[-1] for line in running_apps_response.splitlines() if line.strip()]

        return None

    @staticmethod
    def _screen_on_awake_wake_lock_size(output):
        """Check if the screen is on and the device is awake, and get the wake lock size.

        Parameters
        ----------
        output : str, None
            The output from :py:const:`androidtv.constants.CMD_SCREEN_ON_AWAKE_WAKE_LOCK_SIZE`

        Returns
        -------
        bool, None
            Whether or not the device is on, or ``None`` if it could not be determined
        bool, None
            Whether or not the device is awake (screensaver is not running), or ``None`` if it could not be determined
        int, None
            The size of the current wake lock, or ``None`` if it could not be determined

        """
        if output is None:
            return None, None, None

        if output == "":
            return False, False, None

        screen_on = output[0] == "1"
        awake = None if len(output) < 2 else output[1] == "1"
        wake_lock_size = None if len(output) < 3 else BaseTV._wake_lock_size(output[2:])

        return screen_on, awake, wake_lock_size

    def _volume(self, stream_music, audio_output_device):
        """Get the absolute volume level from the ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``.

        Parameters
        ----------
        stream_music : str, None
            The ``STREAM_MUSIC`` block from ``adb shell dumpsys audio``
        audio_output_device : str, None
            The current audio playback device

        Returns
        -------
        int, None
            The absolute volume level, or ``None`` if it could not be determined

        """
        if not stream_music:
            return None

        if not self.max_volume:
            max_volume_matches = re.findall(
                constants.MAX_VOLUME_REGEX_PATTERN,
                stream_music,
                re.DOTALL | re.MULTILINE,
            )
            if max_volume_matches:
                self.max_volume = float(max_volume_matches[0])

        if not audio_output_device:
            return None

        volume_matches = re.findall(
            audio_output_device + constants.VOLUME_REGEX_PATTERN,
            stream_music,
            re.DOTALL | re.MULTILINE,
        )
        if volume_matches:
            return int(volume_matches[0])

        return None

    def _volume_level(self, volume):
        """Get the relative volume level from the absolute volume level.

        Parameters
        -------
        volume: int, None
            The absolute volume level

        Returns
        -------
        float, None
            The volume level (between 0 and 1), or ``None`` if it could not be determined

        """
        if volume is not None and self.max_volume:
            return volume / self.max_volume

        return None

    @staticmethod
    def _wake_lock_size(wake_lock_size_response):
        """Get the size of the current wake lock from the output of :py:const:`androidtv.constants.CMD_WAKE_LOCK_SIZE`.

        Parameters
        ----------
        wake_lock_size_response : str, None
            The output of :py:const:`androidtv.constants.CMD_WAKE_LOCK_SIZE`

        Returns
        -------
        int, None
            The size of the current wake lock, or ``None`` if it could not be determined

        """
        if wake_lock_size_response:
            wake_lock_size_matches = constants.REGEX_WAKE_LOCK_SIZE.search(wake_lock_size_response)
            if wake_lock_size_matches:
                return int(wake_lock_size_matches.group("size"))

        return None

    @staticmethod
    def _parse_getevent_line(line):
        """Parse a line of the output received in ``learn_sendevent``.

        Parameters
        ----------
        line : str
            A line of output from ``learn_sendevent``

        Returns
        -------
        str
            The properly formatted ``sendevent`` command

        """
        device_name, event_info = line.split(":", 1)
        integers = [int(x, 16) for x in event_info.strip().split()[:3]]
        return "sendevent {} {} {} {}".format(device_name, *integers)


# ======================================================================= #
#                                                                         #
#                    Validate the state detection rules                   #
#                                                                         #
# ======================================================================= #
def state_detection_rules_validator(rules, exc=KeyError):
    """Validate the rules (i.e., the ``state_detection_rules`` value) for a given app ID (i.e., a key in ``state_detection_rules``).

    For each ``rule`` in ``rules``, this function checks that:

    * ``rule`` is a string or a dictionary
    * If ``rule`` is a string:

        * Check that ``rule`` is in :py:const:`~androidtv.constants.VALID_STATES` or :py:const:`~androidtv.constants.VALID_STATE_PROPERTIES`

    * If ``rule`` is a dictionary:

        * Check that each key is in :py:const:`~androidtv.constants.VALID_STATES`
        * Check that each value is a dictionary

            * Check that each key is in :py:const:`~androidtv.constants.VALID_PROPERTIES`
            * Check that each value is of the right type, according to :py:const:`~androidtv.constants.VALID_PROPERTIES_TYPES`

    See :class:`~androidtv.basetv.basetv.BaseTV` for more info about the ``state_detection_rules`` parameter.

    Parameters
    ----------
    rules : list
        A list of the rules that will be used to determine the state
    exc : Exception
        The exception that will be raised if a rule is invalid

    Returns
    -------
    rules : list
        The provided list of rules

    """
    for rule in rules:
        # A rule must be either a string or a dictionary
        if not isinstance(rule, (str, dict)):
            raise exc("Expected a string or a map, got {}".format(type(rule).__name__))

        # If a rule is a string, check that it is valid
        if isinstance(rule, str):
            if rule not in constants.VALID_STATE_PROPERTIES + constants.VALID_STATES:
                raise exc(
                    "Invalid rule '{0}' is not in {1}".format(
                        rule, constants.VALID_STATE_PROPERTIES + constants.VALID_STATES
                    )
                )

        # If a rule is a dictionary, check that it is valid
        else:
            for state, conditions in rule.items():
                # The keys of the dictionary must be valid states
                if state not in constants.VALID_STATES:
                    raise exc("'{0}' is not a valid state for the 'state_detection_rules' parameter".format(state))

                # The values of the dictionary must be dictionaries
                if not isinstance(conditions, dict):
                    raise exc(
                        "Expected a map for entry '{0}' in 'state_detection_rules', got {1}".format(
                            state, type(conditions).__name__
                        )
                    )

                for prop, value in conditions.items():
                    # The keys of the dictionary must be valid properties that can be checked
                    if prop not in constants.VALID_PROPERTIES:
                        raise exc("Invalid property '{0}' is not in {1}".format(prop, constants.VALID_PROPERTIES))

                    # Make sure the value is of the right type
                    if not isinstance(value, constants.VALID_PROPERTIES_TYPES[prop]):
                        raise exc(
                            "Conditional value for property '{0}' must be of type {1}, not {2}".format(
                                prop,
                                constants.VALID_PROPERTIES_TYPES[prop].__name__,
                                type(value).__name__,
                            )
                        )

    return rules