From aa216f0298d5a1398f9ed7472bc52f898393df58 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 Mar 2024 16:31:38 -1000 Subject: [PATCH] Add system_health to the hassio pre-imports to avoid a late executor job (#112466) * Add system_health to the hassio pre-imports to avoid a late executor job `2024-03-05 17:01:33.034 DEBUG (MainThread) [homeassistant.loader] Importing platforms for hassio executor=[system_health] loop=[] took 0.12s` This one does not take that much time but it happens at a time where the import executor is the most busy during startup * key * move patch as its too early now --- homeassistant/components/hassio/__init__.py | 13 ++++++++++--- homeassistant/components/hassio/system_health.py | 11 +++++++---- tests/components/hassio/test_system_health.py | 12 ++++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index cd1c6e735ef..1a666fcc42d 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -43,10 +43,17 @@ from homeassistant.loader import bind_hass from homeassistant.util.async_ import create_eager_task from homeassistant.util.dt import now -# config_flow, diagnostics, and entity platforms are imported to ensure -# other dependencies that wait for hassio are not waiting +# config_flow, diagnostics, system_health, and entity platforms are imported to +# ensure other dependencies that wait for hassio are not waiting # for hassio to import its platforms -from . import binary_sensor, config_flow, diagnostics, sensor, update # noqa: F401 +from . import ( # noqa: F401 + binary_sensor, + config_flow, + diagnostics, + sensor, + system_health, + update, +) from .addon_manager import AddonError, AddonInfo, AddonManager, AddonState # noqa: F401 from .addon_panel import async_setup_addon_panel from .auth import async_setup_auth_view diff --git a/homeassistant/components/hassio/system_health.py b/homeassistant/components/hassio/system_health.py index d89224a2476..e46db2f8e75 100644 --- a/homeassistant/components/hassio/system_health.py +++ b/homeassistant/components/hassio/system_health.py @@ -7,10 +7,10 @@ from typing import Any from homeassistant.components import system_health from homeassistant.core import HomeAssistant, callback -from . import get_host_info, get_info, get_os_info, get_supervisor_info +from .data import get_host_info, get_info, get_os_info, get_supervisor_info -SUPERVISOR_PING = f"http://{os.environ['SUPERVISOR']}/supervisor/ping" -OBSERVER_URL = f"http://{os.environ['SUPERVISOR']}:4357" +SUPERVISOR_PING = "http://{ip_address}/supervisor/ping" +OBSERVER_URL = "http://{ip_address}:4357" @callback @@ -23,6 +23,7 @@ def async_register( async def system_health_info(hass: HomeAssistant) -> dict[str, Any]: """Get info for the info page.""" + ip_address = os.environ["SUPERVISOR"] info = get_info(hass) or {} host_info = get_host_info(hass) or {} supervisor_info = get_supervisor_info(hass) @@ -62,7 +63,9 @@ async def system_health_info(hass: HomeAssistant) -> dict[str, Any]: information["board"] = os_info.get("board") information["supervisor_api"] = system_health.async_check_can_reach_url( - hass, SUPERVISOR_PING, OBSERVER_URL + hass, + SUPERVISOR_PING.format(ip_address=ip_address), + OBSERVER_URL.format(ip_address=ip_address), ) information["version_api"] = system_health.async_check_can_reach_url( hass, diff --git a/tests/components/hassio/test_system_health.py b/tests/components/hassio/test_system_health.py index 7a01c9444ab..715bf3bab91 100644 --- a/tests/components/hassio/test_system_health.py +++ b/tests/components/hassio/test_system_health.py @@ -28,8 +28,7 @@ async def test_hassio_system_health( ) hass.config.components.add("hassio") - with patch.dict(os.environ, MOCK_ENVIRON): - assert await async_setup_component(hass, "system_health", {}) + assert await async_setup_component(hass, "system_health", {}) hass.data["hassio_info"] = { "channel": "stable", @@ -50,7 +49,8 @@ async def test_hassio_system_health( "addons": [{"name": "Awesome Addon", "version": "1.0.0"}], } - info = await get_system_health_info(hass, "hassio") + with patch.dict(os.environ, MOCK_ENVIRON): + info = await get_system_health_info(hass, "hassio") for key, val in info.items(): if asyncio.iscoroutine(val): @@ -87,8 +87,7 @@ async def test_hassio_system_health_with_issues( ) hass.config.components.add("hassio") - with patch.dict(os.environ, MOCK_ENVIRON): - assert await async_setup_component(hass, "system_health", {}) + assert await async_setup_component(hass, "system_health", {}) hass.data["hassio_info"] = {"channel": "stable"} hass.data["hassio_host_info"] = {} @@ -98,7 +97,8 @@ async def test_hassio_system_health_with_issues( "supported": False, } - info = await get_system_health_info(hass, "hassio") + with patch.dict(os.environ, MOCK_ENVIRON): + info = await get_system_health_info(hass, "hassio") for key, val in info.items(): if asyncio.iscoroutine(val):