Exemple #1
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the FireTV platform."""
    from firetv import FireTV

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

    if CONF_ADBKEY in config:
        ftv = FireTV(host, config[CONF_ADBKEY])
        adb_log = " using adbkey='{0}'".format(config[CONF_ADBKEY])
    else:
        ftv = FireTV(host)
        adb_log = ""

    if not ftv.available and ping(config[CONF_HOST]):
        _LOGGER.warning("Could not connect to Fire TV at %s%s", host, adb_log)
        return

    ip = config[CONF_HOST]
    name = config[CONF_NAME]
    get_source = config[CONF_GET_SOURCE]
    get_sources = config[CONF_GET_SOURCES]
    set_states = config[CONF_SET_STATES]

    device = FireTVDevice(ftv, ip, name, get_source, get_sources, set_states)
    add_entities([device])
    _LOGGER.info("Setup Fire TV at %s%s", host, adb_log)
Exemple #2
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the FireTV platform."""
    from firetv import FireTV

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

    if CONF_ADB_SERVER_IP not in config:
        # Use "python-adb" (Python ADB implementation)
        if CONF_ADBKEY in config:
            ftv = FireTV(host, config[CONF_ADBKEY])
            adb_log = " using adbkey='{0}'".format(config[CONF_ADBKEY])
        else:
            ftv = FireTV(host)
            adb_log = ""
    else:
        # Use "pure-python-adb" (communicate with ADB server)
        ftv = FireTV(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 ftv.available:
        _LOGGER.warning("Could not connect to Fire TV at %s%s", host, adb_log)
        return

    name = config[CONF_NAME]
    get_sources = config[CONF_GET_SOURCES]

    device = FireTVDevice(ftv, name, get_sources)
    add_entities([device])
    _LOGGER.debug("Setup Fire TV at %s%s", host, adb_log)
Exemple #3
0
 def __init__(self, host, name, adbkey):
     """Initialize the FireTV device."""
     from firetv import FireTV
     self._host = host
     self._adbkey = adbkey
     self._firetv = FireTV(host, adbkey)
     self._adb_lock = False
     self._name = name
     self._state = STATE_UNKNOWN
     self._running_apps = None
     self._current_app = None
Exemple #4
0
def add(device_id, host):
    """ Add a device.

    Creates FireTV instance associated with device identifier.

    :param device_id: Device identifier.
    :param host: Host in <address>:<port> format.
    :returns: Added successfully or not.
    """
    valid = is_valid_device_id(device_id) and is_valid_host(host)
    if valid:
        devices[device_id] = FireTV(str(host))
    return valid
Exemple #5
0
def add(device_id, host, adbkey='', adb_server_ip='', adb_server_port=5037):
    """ Add a device.

    Creates FireTV instance associated with device identifier.

    :param device_id: Device identifier.
    :param host: Host in <address>:<port> format.
    :param adbkey: The path to the "adbkey" file
    :param adb_server_ip: the IP address for the ADB server
    :param adb_server_port: the port for the ADB server
    :returns: Added successfully or not.
    """
    valid = is_valid_device_id(device_id) and is_valid_host(host)
    if valid:
        devices[device_id] = FireTV(str(host), str(adbkey), str(adb_server_ip), str(adb_server_port))
    return valid
Exemple #6
0
class FireTVDevice(MediaPlayerDevice):
    """Representation of an Amazon Fire TV device on the network."""
    def __init__(self, host, name, adbkey):
        """Initialize the FireTV device."""
        from firetv import FireTV
        self._host = host
        self._adbkey = adbkey
        self._firetv = FireTV(host, adbkey)
        self._adb_lock = False
        self._name = name
        self._state = STATE_UNKNOWN
        self._running_apps = None
        self._current_app = None

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

    @property
    def should_poll(self):
        """Device should be polled."""
        return True

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

    @property
    def state(self):
        """Return the state of the player."""
        return self._state

    @property
    def source(self):
        """Return the current app."""
        return self._current_app

    @property
    def source_list(self):
        """Return a list of running apps."""
        return self._running_apps

    @adb_wrapper
    def update(self):
        """Get the latest date and update device state."""
        try:
            # Check if device is disconnected.
            if not self._firetv._adb:
                self._state = STATE_UNKNOWN
                self._running_apps = None
                self._current_app = None

                # Try to connect
                self._firetv.connect()

            # Check if device is off.
            elif not self._firetv._screen_on:
                self._state = STATE_OFF
                self._running_apps = None
                self._current_app = None

            # Check if screen saver is on.
            elif not self._firetv._awake:
                self._state = STATE_IDLE
                self._running_apps = None
                self._current_app = None

            else:
                # Get the running apps.
                self._running_apps = self._firetv.running_apps()

                # Get the current app.
                current_app = self._firetv.current_app
                if isinstance(current_app, dict) and 'package' in current_app:
                    self._current_app = current_app['package']
                else:
                    self._current_app = current_app

                # Check if the launcher is active.
                if self._current_app in [PACKAGE_LAUNCHER, PACKAGE_SETTINGS]:
                    self._state = STATE_STANDBY

                # Check for a wake lock (device is playing).
                elif self._firetv._wake_lock:
                    self._state = STATE_PLAYING

                # Otherwise, device is paused.
                else:
                    self._state = STATE_PAUSED

        except:
            _LOGGER.error('Update encountered an exception; will attempt to ' +
                          're-establish the ADB connection in the next update')
            self._firetv._adb = None

    @adb_wrapper
    def turn_on(self):
        """Turn on the device."""
        self._firetv.turn_on()

    @adb_wrapper
    def turn_off(self):
        """Turn off the device."""
        self._firetv.turn_off()

    @adb_wrapper
    def media_play(self):
        """Send play command."""
        self._firetv.media_play()

    @adb_wrapper
    def media_pause(self):
        """Send pause command."""
        self._firetv.media_pause()

    @adb_wrapper
    def media_play_pause(self):
        """Send play/pause command."""
        self._firetv.media_play_pause()

    @adb_wrapper
    def media_stop(self):
        """Send stop (back) command."""
        self._firetv.back()

    @adb_wrapper
    def volume_up(self):
        """Send volume up command."""
        self._firetv.volume_up()

    @adb_wrapper
    def volume_down(self):
        """Send volume down command."""
        self._firetv.volume_down()

    @adb_wrapper
    def media_previous_track(self):
        """Send previous track command (results in rewind)."""
        self._firetv.media_previous()

    @adb_wrapper
    def media_next_track(self):
        """Send next track command (results in fast-forward)."""
        self._firetv.media_next()

    @adb_wrapper
    def select_source(self, source):
        """Select input source."""
        if isinstance(source, str):
            if not source.startswith('!'):
                self._firetv.launch_app(source)
            else:
                self._firetv.stop_app(source[1:].lstrip())
Exemple #7
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the FireTV platform."""
    if DATA_FIRETV not in hass.data:
        hass.data[DATA_FIRETV] = dict()

    from firetv import FireTV

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

    if CONF_ADB_SERVER_IP not in config:
        # "python-adb"
        if CONF_ADBKEY in config:
            ftv = FireTV(host, config[CONF_ADBKEY])
            adb_log = " using adbkey='{0}'".format(config[CONF_ADBKEY])
        else:
            ftv = FireTV(host)
            adb_log = ""
    else:
        # "pure-python-adb"
        ftv = FireTV(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 ftv.available:
        _LOGGER.warning("Could not connect to Fire TV at %s%s", host, adb_log)
        return

    name = config[CONF_NAME]
    get_source = config[CONF_GET_SOURCE]
    get_sources = config[CONF_GET_SOURCES]
    set_states = config[CONF_SET_STATES]

    device = FireTVDevice(ftv, name, get_source, get_sources, set_states)
    add_entities([device])
    hass.data[DATA_FIRETV][host] = device
    _LOGGER.info("Setup Fire TV at %s%s", host, adb_log)

    if hass.services.has_service(DOMAIN, SERVICE_ADB_SHELL):
        return

    def service_adb_shell(service):
        """Run ADB shell commands and log the output."""
        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_FIRETV].values()
            if dev.entity_id in entity_id
        ]

        for target_device in target_devices:
            cmd = params['cmd']
            output = target_device.firetv.adb_shell(cmd)
            _LOGGER.info("Output from command '%s' to %s: '%s'", cmd,
                         target_device.entity_id, repr(output))

    def service_adb_streaming_shell(service):
        """Run ADB streaming shell commands and log the output."""
        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_FIRETV].values()
            if dev.entity_id in entity_id
        ]

        for target_device in target_devices:
            cmd = params['cmd']
            output = list(target_device.firetv.adb_streaming_shell(cmd))
            _LOGGER.info("Output from command '%s' to %s: '%s'", cmd,
                         target_device.entity_id, repr(output))

    hass.services.register(DOMAIN,
                           SERVICE_ADB_SHELL,
                           service_adb_shell,
                           schema=SERVICE_ADB_SHELL_SCHEMA)

    hass.services.register(DOMAIN,
                           SERVICE_ADB_STREAMING_SHELL,
                           service_adb_streaming_shell,
                           schema=SERVICE_ADB_STREAMING_SHELL_SCHEMA)
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the FireTV platform."""
    from firetv import FireTV
    if DATA_KEY not in hass.data:
        hass.data[DATA_KEY] = {}

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

    if CONF_ADB_SERVER_IP not in config:
        # "python-adb"
        if CONF_ADBKEY in config:
            ftv = FireTV(host, config[CONF_ADBKEY])
            adb_log = " using adbkey='{0}'".format(config[CONF_ADBKEY])
        else:
            ftv = FireTV(host)
            adb_log = ""
    else:
        # "pure-python-adb"
        ftv = FireTV(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 ftv.available:
        _LOGGER.warning("Could not connect to Fire TV at %s%s", host, adb_log)
        return

    name = config[CONF_NAME]
    get_sources = config[CONF_GET_SOURCES]

    if host in hass.data[DATA_KEY]:
        _LOGGER.warning("Platform already setup on %s, skipping.", host)
    else:
        device = FireTVDevice(ftv, name, get_sources)
        add_entities([device])
        _LOGGER.debug("Setup Fire TV at %s%s", host, adb_log)
        hass.data[DATA_KEY][host] = device

    if hass.services.has_service(DOMAIN, SERVICE_ADB_CMD):
        return

    def service_adb_cmd(service):
        """Dispatch service calls to target entities."""
        cmd = service.data.get(ATTR_CMD)
        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:
            output = target_device.adb_cmd(cmd)

            # log the output if there is any
            if output:
                _LOGGER.info("Output of command '%s' from '%s': %s", cmd,
                             target_device.entity_id, repr(output))

    hass.services.register(DOMAIN,
                           SERVICE_ADB_CMD,
                           service_adb_cmd,
                           schema=SERVICE_ADB_CMD_SCHEMA)