Remove local imports of hass-nabucasa (#112634)
parent
c1d45f63f5
commit
49d20eedd4
|
@ -4,11 +4,10 @@ from __future__ import annotations
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
from ipaddress import IPv4Network, IPv6Network, ip_address
|
from ipaddress import IPv4Network, IPv6Network, ip_address
|
||||||
import logging
|
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.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO
|
||||||
from aiohttp.web import Application, HTTPBadRequest, Request, StreamResponse, middleware
|
from aiohttp.web import Application, HTTPBadRequest, Request, StreamResponse, middleware
|
||||||
|
from hass_nabucasa import remote
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
|
||||||
|
@ -67,30 +66,13 @@ def async_setup_forwarded(
|
||||||
an HTTP 400 status code is thrown.
|
an HTTP 400 status code is thrown.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
remote: Literal[False] | None | ModuleType = None
|
|
||||||
|
|
||||||
@middleware
|
@middleware
|
||||||
async def forwarded_middleware(
|
async def forwarded_middleware(
|
||||||
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]]
|
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]]
|
||||||
) -> StreamResponse:
|
) -> StreamResponse:
|
||||||
"""Process forwarded data by a reverse proxy."""
|
"""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
|
# Skip requests from Remote UI
|
||||||
if remote and remote.is_cloud_request.get():
|
if remote.is_cloud_request.get():
|
||||||
return await handler(request)
|
return await handler(request)
|
||||||
|
|
||||||
# Handle X-Forwarded-For
|
# Handle X-Forwarded-For
|
||||||
|
|
|
@ -6,6 +6,7 @@ from contextlib import suppress
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
|
from hass_nabucasa import remote
|
||||||
import yarl
|
import yarl
|
||||||
|
|
||||||
from homeassistant.components import http
|
from homeassistant.components import http
|
||||||
|
@ -314,6 +315,4 @@ def is_cloud_connection(hass: HomeAssistant) -> bool:
|
||||||
if "cloud" not in hass.config.components:
|
if "cloud" not in hass.config.components:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
from hass_nabucasa import remote # pylint: disable=import-outside-toplevel
|
|
||||||
|
|
||||||
return remote.is_cloud_request.get()
|
return remote.is_cloud_request.get()
|
||||||
|
|
Loading…
Reference in New Issue