Remove local imports of hass-nabucasa (#112634)

pull/112651/head
Erik Montnemery 2024-03-07 20:37:35 +01:00 committed by GitHub
parent c1d45f63f5
commit 49d20eedd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 22 deletions

View File

@ -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

View File

@ -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()