mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Use proper signals (#18613)
* Emulated Hue not use deprecated handler * Remove no longer needed workaround * Add middleware directly * Dont always load the ban config file * Update homeassistant/components/http/ban.py Co-Authored-By: balloob <paulus@home-assistant.io> * Update __init__.py
This commit is contained in:
@@ -302,12 +302,6 @@ class HomeAssistantHTTP:
|
||||
|
||||
async def start(self):
|
||||
"""Start the aiohttp server."""
|
||||
# We misunderstood the startup signal. You're not allowed to change
|
||||
# anything during startup. Temp workaround.
|
||||
# pylint: disable=protected-access
|
||||
self.app._on_startup.freeze()
|
||||
await self.app.startup()
|
||||
|
||||
if self.ssl_certificate:
|
||||
try:
|
||||
if self.ssl_profile == SSL_INTERMEDIATE:
|
||||
@@ -335,6 +329,7 @@ class HomeAssistantHTTP:
|
||||
# However in Home Assistant components can be discovered after boot.
|
||||
# This will now raise a RunTimeError.
|
||||
# To work around this we now prevent the router from getting frozen
|
||||
# pylint: disable=protected-access
|
||||
self.app._router.freeze = lambda: None
|
||||
|
||||
self.runner = web.AppRunner(self.app)
|
||||
|
||||
@@ -96,11 +96,7 @@ def setup_auth(app, trusted_networks, use_auth,
|
||||
request[KEY_AUTHENTICATED] = authenticated
|
||||
return await handler(request)
|
||||
|
||||
async def auth_startup(app):
|
||||
"""Initialize auth middleware when app starts up."""
|
||||
app.middlewares.append(auth_middleware)
|
||||
|
||||
app.on_startup.append(auth_startup)
|
||||
app.middlewares.append(auth_middleware)
|
||||
|
||||
|
||||
def _is_trusted_ip(request, trusted_networks):
|
||||
|
||||
@@ -9,7 +9,7 @@ from aiohttp.web import middleware
|
||||
from aiohttp.web_exceptions import HTTPForbidden, HTTPUnauthorized
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import callback, HomeAssistant
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@@ -36,13 +36,14 @@ SCHEMA_IP_BAN_ENTRY = vol.Schema({
|
||||
@callback
|
||||
def setup_bans(hass, app, login_threshold):
|
||||
"""Create IP Ban middleware for the app."""
|
||||
app.middlewares.append(ban_middleware)
|
||||
app[KEY_FAILED_LOGIN_ATTEMPTS] = defaultdict(int)
|
||||
app[KEY_LOGIN_THRESHOLD] = login_threshold
|
||||
|
||||
async def ban_startup(app):
|
||||
"""Initialize bans when app starts up."""
|
||||
app.middlewares.append(ban_middleware)
|
||||
app[KEY_BANNED_IPS] = await hass.async_add_job(
|
||||
load_ip_bans_config, hass.config.path(IP_BANS_FILE))
|
||||
app[KEY_FAILED_LOGIN_ATTEMPTS] = defaultdict(int)
|
||||
app[KEY_LOGIN_THRESHOLD] = login_threshold
|
||||
app[KEY_BANNED_IPS] = await async_load_ip_bans_config(
|
||||
hass, hass.config.path(IP_BANS_FILE))
|
||||
|
||||
app.on_startup.append(ban_startup)
|
||||
|
||||
@@ -149,7 +150,7 @@ class IpBan:
|
||||
self.banned_at = banned_at or datetime.utcnow()
|
||||
|
||||
|
||||
def load_ip_bans_config(path: str):
|
||||
async def async_load_ip_bans_config(hass: HomeAssistant, path: str):
|
||||
"""Load list of banned IPs from config file."""
|
||||
ip_list = []
|
||||
|
||||
@@ -157,7 +158,7 @@ def load_ip_bans_config(path: str):
|
||||
return ip_list
|
||||
|
||||
try:
|
||||
list_ = load_yaml_config_file(path)
|
||||
list_ = await hass.async_add_executor_job(load_yaml_config_file, path)
|
||||
except HomeAssistantError as err:
|
||||
_LOGGER.error('Unable to load %s: %s', path, str(err))
|
||||
return ip_list
|
||||
|
||||
@@ -33,8 +33,4 @@ def setup_real_ip(app, use_x_forwarded_for, trusted_proxies):
|
||||
|
||||
return await handler(request)
|
||||
|
||||
async def app_startup(app):
|
||||
"""Initialize bans when app starts up."""
|
||||
app.middlewares.append(real_ip_middleware)
|
||||
|
||||
app.on_startup.append(app_startup)
|
||||
app.middlewares.append(real_ip_middleware)
|
||||
|
||||
Reference in New Issue
Block a user