Skip to content

Commit

Permalink
Use HTTPStatus instead of HTTP_* int constants in mobile_app responses
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Sep 19, 2021
1 parent 53d4c0c commit d63ee41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
29 changes: 17 additions & 12 deletions homeassistant/components/mobile_app/helpers.py
@@ -1,6 +1,7 @@
"""Helpers for mobile_app."""
from __future__ import annotations

from http import HTTPStatus
import json
import logging
from typing import Callable
Expand All @@ -9,12 +10,7 @@
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox

from homeassistant.const import (
ATTR_DEVICE_ID,
CONTENT_TYPE_JSON,
HTTP_BAD_REQUEST,
HTTP_OK,
)
from homeassistant.const import ATTR_DEVICE_ID, CONTENT_TYPE_JSON
from homeassistant.core import Context, HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.json import JSONEncoder
Expand Down Expand Up @@ -95,20 +91,25 @@ def registration_context(registration: dict) -> Context:
return Context(user_id=registration[CONF_USER_ID])


def empty_okay_response(headers: dict = None, status: int = HTTP_OK) -> Response:
def empty_okay_response(
headers: dict = None, status: HTTPStatus = HTTPStatus.OK
) -> Response:
"""Return a Response with empty JSON object and a 200."""
return Response(
text="{}", status=status, content_type=CONTENT_TYPE_JSON, headers=headers
text="{}", status=status.value, content_type=CONTENT_TYPE_JSON, headers=headers
)


def error_response(
code: str, message: str, status: int = HTTP_BAD_REQUEST, headers: dict = None
code: str,
message: str,
status: HTTPStatus = HTTPStatus.BAD_REQUEST,
headers: dict = None,
) -> Response:
"""Return an error Response."""
return json_response(
{"success": False, "error": {"code": code, "message": message}},
status=status,
status=status.value,
headers=headers,
)

Expand Down Expand Up @@ -147,7 +148,11 @@ def savable_state(hass: HomeAssistant) -> dict:


def webhook_response(
data, *, registration: dict, status: int = HTTP_OK, headers: dict = None
data,
*,
registration: dict,
status: HTTPStatus = HTTPStatus.OK,
headers: dict = None,
) -> Response:
"""Return a encrypted response if registration supports it."""
data = json.dumps(data, cls=JSONEncoder)
Expand All @@ -163,7 +168,7 @@ def webhook_response(
data = json.dumps({"encrypted": True, "encrypted_data": enc_data})

return Response(
text=data, status=status, content_type=CONTENT_TYPE_JSON, headers=headers
text=data, status=status.value, content_type=CONTENT_TYPE_JSON, headers=headers
)


Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/mobile_app/webhook.py
Expand Up @@ -2,6 +2,7 @@
import asyncio
from contextlib import suppress
from functools import wraps
from http import HTTPStatus
import logging
import secrets

Expand Down Expand Up @@ -30,8 +31,6 @@
ATTR_SERVICE_DATA,
ATTR_SUPPORTED_FEATURES,
CONF_WEBHOOK_ID,
HTTP_BAD_REQUEST,
HTTP_CREATED,
)
from homeassistant.core import EventOrigin, HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound
Expand Down Expand Up @@ -158,7 +157,7 @@ async def handle_webhook(
req_data = await request.json()
except ValueError:
_LOGGER.warning("Received invalid JSON from mobile_app device: %s", device_name)
return empty_okay_response(status=HTTP_BAD_REQUEST)
return empty_okay_response(status=HTTPStatus.BAD_REQUEST)

if (
ATTR_WEBHOOK_ENCRYPTED not in req_data
Expand Down Expand Up @@ -265,7 +264,7 @@ async def webhook_stream_camera(hass, config_entry, data):
return webhook_response(
{"success": False},
registration=config_entry.data,
status=HTTP_BAD_REQUEST,
status=HTTPStatus.BAD_REQUEST,
)

resp = {"mjpeg_path": f"/api/camera_proxy_stream/{camera.entity_id}"}
Expand Down Expand Up @@ -435,7 +434,7 @@ async def webhook_register_sensor(hass, config_entry, data):
return webhook_response(
{"success": True},
registration=config_entry.data,
status=HTTP_CREATED,
status=HTTPStatus.CREATED,
)


Expand Down

0 comments on commit d63ee41

Please sign in to comment.