Use HTTPStatus instead of HTTP_ consts and magic values in comp.../[bc]* (#57989)

This commit is contained in:
Ville Skyttä
2021-10-22 20:43:40 +03:00
committed by GitHub
parent ab7a34fc71
commit 73d192b3f3
25 changed files with 169 additions and 182 deletions

View File

@@ -1,6 +1,7 @@
"""The HTTP api to control the cloud integration."""
import asyncio
from functools import wraps
from http import HTTPStatus
import logging
import aiohttp
@@ -20,12 +21,6 @@ from homeassistant.components.google_assistant import helpers as google_helpers
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.websocket_api import const as ws_const
from homeassistant.const import (
HTTP_BAD_GATEWAY,
HTTP_BAD_REQUEST,
HTTP_INTERNAL_SERVER_ERROR,
HTTP_UNAUTHORIZED,
)
from .const import (
DOMAIN,
@@ -48,19 +43,19 @@ _LOGGER = logging.getLogger(__name__)
_CLOUD_ERRORS = {
InvalidTrustedNetworks: (
HTTP_INTERNAL_SERVER_ERROR,
HTTPStatus.INTERNAL_SERVER_ERROR,
"Remote UI not compatible with 127.0.0.1/::1 as a trusted network.",
),
InvalidTrustedProxies: (
HTTP_INTERNAL_SERVER_ERROR,
HTTPStatus.INTERNAL_SERVER_ERROR,
"Remote UI not compatible with 127.0.0.1/::1 as trusted proxies.",
),
asyncio.TimeoutError: (
HTTP_BAD_GATEWAY,
HTTPStatus.BAD_GATEWAY,
"Unable to reach the Home Assistant cloud.",
),
aiohttp.ClientError: (
HTTP_INTERNAL_SERVER_ERROR,
HTTPStatus.INTERNAL_SERVER_ERROR,
"Error making internal request",
),
}
@@ -96,15 +91,15 @@ async def async_setup(hass):
_CLOUD_ERRORS.update(
{
auth.UserNotFound: (HTTP_BAD_REQUEST, "User does not exist."),
auth.UserNotConfirmed: (HTTP_BAD_REQUEST, "Email not confirmed."),
auth.UserNotFound: (HTTPStatus.BAD_REQUEST, "User does not exist."),
auth.UserNotConfirmed: (HTTPStatus.BAD_REQUEST, "Email not confirmed."),
auth.UserExists: (
HTTP_BAD_REQUEST,
HTTPStatus.BAD_REQUEST,
"An account with the given email already exists.",
),
auth.Unauthenticated: (HTTP_UNAUTHORIZED, "Authentication failed."),
auth.Unauthenticated: (HTTPStatus.UNAUTHORIZED, "Authentication failed."),
auth.PasswordChangeRequired: (
HTTP_BAD_REQUEST,
HTTPStatus.BAD_REQUEST,
"Password change required.",
),
}
@@ -157,7 +152,7 @@ def _process_cloud_exception(exc, where):
if err_info is None:
_LOGGER.exception("Unexpected error processing request for %s", where)
err_info = (HTTP_BAD_GATEWAY, f"Unexpected error: {exc}")
err_info = (HTTPStatus.BAD_GATEWAY, f"Unexpected error: {exc}")
return err_info