How to use the pytradfri.error.RequestError function in pytradfri

To help you get started, we’ve selected a few pytradfri examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ggravlingen / pytradfri / pytradfri / api / libcoap_api.py View on Github external
}
        try:
            proc = subprocess.Popen(command, **kwargs)
        except subprocess.CalledProcessError as err:
            raise RequestError(
                'Error executing request: %s'.format(err)) from None

        output = ''
        open_obj = 0
        start = time()

        for data in iter(lambda: proc.stdout.read(1), ''):
            if data == '\n':
                _LOGGER.debug('Observing stopped for %s after %.1fs',
                              path, time() - start)
                err_callback(RequestError("Observing stopped."))
                break

            if data == '{':
                open_obj += 1
            elif data == '}':
                open_obj -= 1

            output += data

            if open_obj == 0:
                api_command.result = _process_output(output)
                output = ''
github ggravlingen / pytradfri / pytradfri / error.py View on Github external
class RequestError(PytradfriError):
    """An error happened sending or receiving a command."""
    pass


class ColorError(PytradfriError):
    """An error happened matching color name."""
    pass


class RequestTimeout(RequestError):
    """Error when sending or receiving the command timed out."""
    pass


class ClientError(RequestError):
    """Error when the client caused the error.

    See section 5.9.2 of draft-ietf-core-coap-04.
    """
    pass


class ServerError(RequestError):
    """Error when the server caused the error.

    See section 5.9.3 of draft-ietf-core-coap-04.
    """
    pass
github ggravlingen / pytradfri / pytradfri / api / libcoap_api.py View on Github external
def _process_output(output, parse_json=True):
    """Process output."""
    output = output.strip()
    _LOGGER.debug('Received: %s', output)

    if not output:
        return None

    elif 'decrypt_verify' in output:
        raise RequestError(
            'Please compile coap-client without debug output. See '
            'instructions at '
            'https://github.com/ggravlingen/pytradfri#installation')

    elif output.startswith(CLIENT_ERROR_PREFIX):
        raise ClientError(output)

    elif output.startswith(SERVER_ERROR_PREFIX):
        raise ServerError(output)

    elif not parse_json:
        return output

    return json.loads(output)
github ggravlingen / pytradfri / pytradfri / error.py View on Github external
class PytradfriError(Exception):
    """Base Error"""
    pass


class RequestError(PytradfriError):
    """An error happened sending or receiving a command."""
    pass


class ColorError(PytradfriError):
    """An error happened matching color name."""
    pass


class RequestTimeout(RequestError):
    """Error when sending or receiving the command timed out."""
    pass


class ClientError(RequestError):
    """Error when the client caused the error.

    See section 5.9.2 of draft-ietf-core-coap-04.
    """
    pass


class ServerError(RequestError):
    """Error when the server caused the error.

    See section 5.9.3 of draft-ietf-core-coap-04.
github ggravlingen / pytradfri / pytradfri / api / libcoap_api.py View on Github external
kwargs['input'] = json.dumps(data)
            command.append('-f')
            command.append('-')
            _LOGGER.debug('Executing %s %s %s: %s', self._host, method, path,
                          data)
        else:
            _LOGGER.debug('Executing %s %s %s', self._host, method, path)

        command.append(url)

        try:
            return_value = subprocess.check_output(command, **kwargs)
        except subprocess.TimeoutExpired:
            raise RequestTimeout() from None
        except subprocess.CalledProcessError as err:
            raise RequestError(
                'Error executing request: {}'.format(err)) from None

        api_command.result = _process_output(return_value, parse_json)
        return api_command.result
github ggravlingen / pytradfri / pytradfri / api / libcoap_api.py View on Github external
raise ValueError("Observation duration has to be greater than 0.")
        url = api_command.url(self._host)
        err_callback = api_command.err_callback

        command = (self._base_command('get')
                   + ['-s', str(duration), '-B', str(duration), url])

        kwargs = {
            'stdout': subprocess.PIPE,
            'stderr': subprocess.DEVNULL,
            'universal_newlines': True
        }
        try:
            proc = subprocess.Popen(command, **kwargs)
        except subprocess.CalledProcessError as err:
            raise RequestError(
                'Error executing request: %s'.format(err)) from None

        output = ''
        open_obj = 0
        start = time()

        for data in iter(lambda: proc.stdout.read(1), ''):
            if data == '\n':
                _LOGGER.debug('Observing stopped for %s after %.1fs',
                              path, time() - start)
                err_callback(RequestError("Observing stopped."))
                break

            if data == '{':
                open_obj += 1
            elif data == '}':