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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove local imports of hass-nabucasa #112634

Merged
merged 1 commit into from
Mar 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions homeassistant/components/http/forwarded.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from collections.abc import Awaitable, Callable
from ipaddress import IPv4Network, IPv6Network, ip_address
import logging
from types import ModuleType
from typing import Literal

from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO
from aiohttp.web import Application, HTTPBadRequest, Request, StreamResponse, middleware
from hass_nabucasa import remote

from homeassistant.core import callback

Expand Down Expand Up @@ -67,30 +66,13 @@ def async_setup_forwarded(
an HTTP 400 status code is thrown.
"""

remote: Literal[False] | None | ModuleType = None

@middleware
async def forwarded_middleware(
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]]
) -> StreamResponse:
"""Process forwarded data by a reverse proxy."""
nonlocal remote

if remote is None:
# Initialize remote method
try:
from hass_nabucasa import ( # pylint: disable=import-outside-toplevel
remote,
)

# venv users might have an old version installed if they don't have cloud around anymore
if not hasattr(remote, "is_cloud_request"):
remote = False
except ImportError:
remote = False

# Skip requests from Remote UI
if remote and remote.is_cloud_request.get():
if remote.is_cloud_request.get():
return await handler(request)

# Handle X-Forwarded-For
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/helpers/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ipaddress import ip_address
from typing import cast

from hass_nabucasa import remote
import yarl

from homeassistant.components import http
Expand Down Expand Up @@ -314,6 +315,4 @@ def is_cloud_connection(hass: HomeAssistant) -> bool:
if "cloud" not in hass.config.components:
return False

from hass_nabucasa import remote # pylint: disable=import-outside-toplevel

return remote.is_cloud_request.get()
Loading