コード例 #1
0
    def __init__(self, host, name, adbkey):
        """Initialize the AndroidTV device."""
        from androidtv import AndroidTV  # pylint: disable=no-name-in-module
        from adb.adb_protocol import (InvalidCommandError,
                                      InvalidResponseError,
                                      InvalidChecksumError)

        self._host = host
        self._adbkey = adbkey
        self._androidtv = AndroidTV(host, adbkey)
        self._adb_lock = False

        self._exceptions = (TypeError, ValueError, AttributeError,
                            InvalidCommandError, InvalidResponseError,
                            InvalidChecksumError)

        self._name = name
        self._state = STATE_UNKNOWN
        self._app_name = None
コード例 #2
0
    def __init__(self, hass, host, name, adbkey):
        """Initialize the AndroidTV device."""
        from androidtv import AndroidTV  # pylint: disable=no-name-in-module

        self._hass = hass
        self._host = host
        self._adbkey = adbkey
        self._androidtv = AndroidTV(host, adbkey)

        self._name = name
        self._state = STATE_UNKNOWN
        self._app_name = None
コード例 #3
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the androidtv platform."""
    from androidtv import AndroidTV
    if DATA_KEY not in hass.data:
        hass.data[DATA_KEY] = {}

    host = '{0}:{1}'.format(config[CONF_HOST], config[CONF_PORT])
    name = config.get(CONF_NAME)

    if CONF_ADB_SERVER_IP not in config:
        atv = AndroidTV(host)
        if not atv.available:
            # "python-adb" with adbkey
            if CONF_ADBKEY in config:
                adbkey = config[CONF_ADBKEY]
            else:
                adbkey = DEFAULT_ADBKEY
            atv = AndroidTV(host, adbkey)
            adb_log = " using adbkey='{0}'".format(adbkey)
        else:
            adb_log = ""

    else:
        # "pure-python-adb"
        atv = AndroidTV(host,
                        adb_server_ip=config[CONF_ADB_SERVER_IP],
                        adb_server_port=config[CONF_ADB_SERVER_PORT])
        adb_log = " using ADB server at {0}:{1}".format(
            config[CONF_ADB_SERVER_IP], config[CONF_ADB_SERVER_PORT])

    if not atv.available:
        _LOGGER.warning("Could not connect to Android TV at %s%s", host,
                        adb_log)
        raise PlatformNotReady

    if host in hass.data[DATA_KEY]:
        _LOGGER.warning("Platform already setup on %s, skipping.", host)
    else:
        device = AndroidTVDevice(atv, name, config[CONF_APPS])
        add_entities([device])
        _LOGGER.info("Setup Android TV at %s%s", host, adb_log)
        hass.data[DATA_KEY][host] = device

    def service_action(service):
        """Dispatch service calls to target entities."""
        params = {
            key: value
            for key, value in service.data.items() if key != ATTR_ENTITY_ID
        }

        entity_id = service.data.get(ATTR_ENTITY_ID)
        target_devices = [
            dev for dev in hass.data[DATA_KEY].values()
            if dev.entity_id in entity_id
        ]

        for target_device in target_devices:
            target_device.do_action(params['action'])

    def service_intent(service):
        """Dispatch service calls to target entities."""
        params = {
            key: value
            for key, value in service.data.items() if key != ATTR_ENTITY_ID
        }

        entity_id = service.data.get(ATTR_ENTITY_ID)
        target_devices = [
            dev for dev in hass.data[DATA_KEY].values()
            if dev.entity_id in entity_id
        ]

        for target_device in target_devices:
            target_device.start_intent(params['intent'])

    def service_key(service):
        """Dispatch service calls to target entities."""
        params = {
            key: value
            for key, value in service.data.items() if key != ATTR_ENTITY_ID
        }

        entity_id = service.data.get(ATTR_ENTITY_ID)
        target_devices = [
            dev for dev in hass.data[DATA_KEY].values()
            if dev.entity_id in entity_id
        ]

        for target_device in target_devices:
            target_device.input_key(params['key'])

    hass.services.register(DOMAIN,
                           ACTION_SERVICE,
                           service_action,
                           schema=SERVICE_ACTION_SCHEMA)
    hass.services.register(DOMAIN,
                           INTENT_SERVICE,
                           service_intent,
                           schema=SERVICE_INTENT_SCHEMA)
    hass.services.register(DOMAIN,
                           KEY_SERVICE,
                           service_key,
                           schema=SERVICE_KEY_SCHEMA)
コード例 #4
0
class AndroidTVDevice(MediaPlayerDevice):
    """Representation of an AndroidTv device."""
    def __init__(self, host, name, adbkey):
        """Initialize the AndroidTV device."""
        from androidtv import AndroidTV  # pylint: disable=no-name-in-module
        from adb.adb_protocol import (InvalidCommandError,
                                      InvalidResponseError,
                                      InvalidChecksumError)

        self._host = host
        self._adbkey = adbkey
        self._androidtv = AndroidTV(host, adbkey)
        self._adb_lock = False

        self._exceptions = (TypeError, ValueError, AttributeError,
                            InvalidCommandError, InvalidResponseError,
                            InvalidChecksumError)

        self._name = name
        self._state = STATE_UNKNOWN
        self._app_name = None
        # self._running_apps = None
        # self._current_app = None

    @adb_wrapper
    def update(self):
        """Get the latest details from the device."""
        self._androidtv.update()

        if self._androidtv.state == 'off':
            self._state = STATE_OFF
        elif self._androidtv.state == 'idle':
            self._state = STATE_IDLE
        elif self._androidtv.state == 'play':
            self._state = STATE_PLAYING
        elif self._androidtv.state == 'pause':
            self._state = STATE_PAUSED
        elif self._androidtv.state == 'standby':
            self._state = STATE_STANDBY
        elif self._androidtv.state == 'unknown':
            self._state = STATE_UNKNOWN

        self._app_name = get_app_name(self._androidtv.app_id)

    @property
    def name(self):
        """Return the name of the device."""
        return self._name

    @property
    def state(self):
        """Return the state of the device."""
        return self._androidtv.state

    @property
    def is_volume_muted(self):
        """Boolean if volume is currently muted."""
        return self._androidtv.muted

    @property
    def volume_level(self):
        """Return the volume level."""
        return self._androidtv.volume

    # @property
    # def source(self):
    #     """Return the current playback device."""
    #     return self._device

    @property
    def app_id(self):
        """ID of the current running app."""
        return self._androidtv.app_id

    @property
    def app_name(self):
        """Name of the current running app."""
        return self._app_name

    # @property
    # def available(self):
    #     """Return True if entity is available."""
    #     return self._available

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        return SUPPORT_ANDROIDTV

    def turn_on(self):
        """Instruct the tv to turn on."""
        self._androidtv.turn_on()

    def turn_off(self):
        """Instruct the tv to turn off."""
        self._androidtv.turn_off()

    def media_play(self):
        """Send play command."""
        self._androidtv.media_play()
        self._state = STATE_PLAYING

    def media_pause(self):
        """Send pause command."""
        self._androidtv.media_pause()
        self._state = STATE_PAUSED

    def media_play_pause(self):
        """Send play/pause command."""
        self._androidtv.media_play_pause()

    def media_stop(self):
        """Send stop command."""
        self._androidtv.media_stop()
        self._state = STATE_IDLE

    def mute_volume(self, mute):
        """Mute the volume."""
        self._androidtv.mute_volume()
        self._androidtv.muted = mute

    def volume_up(self):
        """Increment the volume level."""
        self._androidtv.volume_up()

    def volume_down(self):
        """Decrement the volume level."""
        self._androidtv.volume_down()

    def media_previous_track(self):
        """Send previous track command."""
        self._androidtv.media_previous()

    def media_next_track(self):
        """Send next track command."""
        self._androidtv.media_next()

    def input_key(self, key):
        """Input the key to the device."""
        self._androidtv._key(key)

    def start_intent(self, uri):
        """Start an intent on the device."""
        self._androidtv._adb.Shell(
            "am start -a android.intent.action.VIEW -d {}".format(uri))

    def do_action(self, action):
        """Input the key corresponding to the action."""
        self._androidtv._adb.Shell("input keyevent {}".format(ACTIONS[action]))