How to use the pydeconz.errors.Unauthorized function in pydeconz

To help you get started, we’ve selected a few pydeconz 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 home-assistant / home-assistant / tests / components / deconz / test_gateway.py View on Github external
async def test_get_gateway_fails_unauthorized(hass):
    """Failed call."""
    with patch(
        "pydeconz.DeconzSession.async_load_parameters",
        side_effect=pydeconz.errors.Unauthorized,
    ), pytest.raises(deconz.errors.AuthenticationRequired):
        assert (
            await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock())
            is False
        )
github Kane610 / deconz / tests / test_utils.py View on Github external
async def test_request_fails_raise_error() -> None:
    """Test a successful call of request."""
    response = Mock()
    response.content_type = "application/json"
    response.json = CoroutineMock(
        return_value=[
            {"error": {"type": 1, "address": "address", "description": "description"}}
        ]
    )
    session = CoroutineMock(return_value=response)

    with pytest.raises(errors.Unauthorized) as e_info:
        await utils.async_request(session, "url")

    assert str(e_info.value) == "address description"
github home-assistant / home-assistant / homeassistant / components / deconz / gateway.py View on Github external
session = aiohttp_client.async_get_clientsession(hass)

    deconz = DeconzSession(
        session,
        config[CONF_HOST],
        config[CONF_PORT],
        config[CONF_API_KEY],
        async_add_device=async_add_device_callback,
        connection_status=async_connection_status_callback,
    )
    try:
        with async_timeout.timeout(10):
            await deconz.initialize()
        return deconz

    except errors.Unauthorized:
        _LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
        raise AuthenticationRequired

    except (asyncio.TimeoutError, errors.RequestError):
        _LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
        raise CannotConnect
github Kane610 / deconz / pydeconz / errors.py View on Github external
class Forbidden(pydeconzException):
    """The caller has no rights to access the requested URI."""


class ResourceNotFound(pydeconzException):
    """The requested resource (light, group, ...) was not found."""


class BridgeBusy(pydeconzException):
    """The Bridge is busy, too many requests (more than 20)."""


ERRORS = {
    1: Unauthorized,  # Unauthorized user
    2: BadRequest,  # Body contains invalid JSON
    3: ResourceNotFound,  # Resource not available
    4: RequestError,  # Method not available for resource
    5: BadRequest,  # Missing parameters in body
    6: RequestError,  # Parameter not available
    7: RequestError,  # Invalid value for parameter
    8: RequestError,  # Parameter is not modifiable
    901: BridgeBusy,  # May occur when sending too fast
}


def raise_error(error):
    if error:
        cls = ERRORS.get(error["type"], pydeconzException)
        raise cls("{} {}".format(error["address"], error["description"]))