Example #1
0
    def update_devices():
        """Ping Alexa API to identify all devices, bluetooth, and last called device.

        This will add new devices and services when discovered. By default this
        runs every SCAN_INTERVAL seconds unless another method calls it. While
        throttled at MIN_TIME_BETWEEN_SCANS, care should be taken to reduce the
        number of runs to avoid flooding. Slow changing states should be
        checked here instead of in spawned components like media_player since
        this object is one per account.
        Each AlexaAPI call generally results in one webpage request.
        """
        from alexapy import AlexaAPI
        email = login_obj.get_email()
        _LOGGER.debug("Updating devices for %s", hide_email(email))
        devices = AlexaAPI.get_devices(login_obj)
        bluetooth = AlexaAPI.get_bluetooth(login_obj)
        last_called = AlexaAPI.get_last_device_serial(login_obj)
        _LOGGER.debug("Found %s devices, %s bluetooth, last_called: %s",
                      len(devices), len(bluetooth), last_called)
        if ((devices is None or bluetooth is None) and len(_CONFIGURING) == 0):
            _LOGGER.debug("Alexa API disconnected; attempting to relogin")
            login_obj.login_with_cookie()
            testLoginStatus(hass, config, login_obj, setup_platform_callback)

        new_alexa_clients = []  # list of newly discovered device jsons
        available_client_ids = []  # list of known serial numbers
        for device in devices:
            if include and device['accountName'] not in include:
                continue
            elif exclude and device['accountName'] in exclude:
                continue

            for b_state in bluetooth['bluetoothStates']:
                if device['serialNumber'] == b_state['deviceSerialNumber']:
                    device['bluetooth_state'] = b_state

            available_client_ids.append(device['serialNumber'])
            (hass.data[DATA_ALEXAMEDIA]['accounts'][email]['devices']
             ['media_player'][device['serialNumber']]) = device

            if device['serialNumber'] not in alexa_clients:
                new_alexa_clients.append(device)

        if new_alexa_clients:
            for component in ALEXA_COMPONENTS:
                load_platform(hass, component, DOMAIN, {}, config)
        # Process last_called data to fire events
        stored_data = hass.data[DATA_ALEXAMEDIA]['accounts'][email]
        if (('last_called' in stored_data
             and last_called != stored_data['last_called']) or
            ('last_called' not in stored_data and last_called is not None)):
            hass.bus.fire('{}_{}'.format(DOMAIN, email),
                          {'last_called_change': last_called})
        (hass.data[DATA_ALEXAMEDIA]['accounts'][email]['last_called']
         ) = AlexaAPI.get_last_device_serial(login_obj)
Example #2
0
    def update_last_called(login_obj, last_called=None):
        """Update the last called device for the login_obj.

        This will store the last_called in hass.data and also fire an event
        to notify listeners.
        """
        from alexapy import AlexaAPI
        if last_called:
            last_called = last_called
        else:
            last_called = AlexaAPI.get_last_device_serial(login_obj)
        _LOGGER.debug("%s: Updated last_called: %s", hide_email(email),
                      hide_serial(last_called))
        stored_data = hass.data[DATA_ALEXAMEDIA]['accounts'][email]
        if (('last_called' in stored_data
             and last_called != stored_data['last_called']) or
            ('last_called' not in stored_data and last_called is not None)):
            _LOGGER.debug(
                "%s: last_called changed: %s to %s", hide_email(email),
                hide_serial(stored_data['last_called'] if 'last_called' in
                            stored_data else None), hide_serial(last_called))
            hass.bus.fire(('{}_{}'.format(DOMAIN, hide_email(email)))[0:32],
                          {'last_called_change': last_called})
        (hass.data[DATA_ALEXAMEDIA]['accounts'][email]['last_called']
         ) = last_called