Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTTPStatus instead of HTTP_* int constants in mobile_app responses #56418

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions homeassistant/components/mobile_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
from __future__ import annotations

from collections.abc import Callable
from http import HTTPStatus
import json
import logging

from aiohttp.web import Response, json_response
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,15 +91,20 @@ 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
)


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(
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 Down
9 changes: 4 additions & 5 deletions homeassistant/components/mobile_app/webhook.py
Original file line number Diff line number Diff line change
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