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
Exemple #2
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
Exemple #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)