From b4ae08f83dfd967b503840402a8187c7afe0c8ab Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 9 May 2025 10:55:41 +0200 Subject: [PATCH] Move hardware initialisation to package module (#144540) --- homeassistant/components/hardware/__init__.py | 15 ++++++++++-- homeassistant/components/hardware/hardware.py | 2 -- homeassistant/components/hardware/models.py | 21 ++++++++++++----- .../components/hardware/websocket_api.py | 23 ++----------------- .../components/hardware/test_websocket_api.py | 2 +- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/hardware/__init__.py b/homeassistant/components/hardware/__init__.py index 8576b29ef19..5db9671a4ed 100644 --- a/homeassistant/components/hardware/__init__.py +++ b/homeassistant/components/hardware/__init__.py @@ -2,20 +2,31 @@ from __future__ import annotations +import psutil_home_assistant as ha_psutil + from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from . import websocket_api from .const import DATA_HARDWARE, DOMAIN -from .models import HardwareData +from .hardware import async_process_hardware_platforms +from .models import HardwareData, SystemStatus CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Hardware.""" - hass.data[DATA_HARDWARE] = HardwareData() + hass.data[DATA_HARDWARE] = HardwareData( + hardware_platform={}, + system_status=SystemStatus( + ha_psutil=await hass.async_add_executor_job(ha_psutil.PsutilWrapper), + remove_periodic_timer=None, + subscribers=set(), + ), + ) + await async_process_hardware_platforms(hass) await websocket_api.async_setup(hass) diff --git a/homeassistant/components/hardware/hardware.py b/homeassistant/components/hardware/hardware.py index e07d970cd1f..9fd257a14a7 100644 --- a/homeassistant/components/hardware/hardware.py +++ b/homeassistant/components/hardware/hardware.py @@ -16,8 +16,6 @@ async def async_process_hardware_platforms( hass: HomeAssistant, ) -> None: """Start processing hardware platforms.""" - hass.data[DATA_HARDWARE].hardware_platform = {} - await async_process_integration_platforms( hass, DOMAIN, _register_hardware_platform, wait_for_platforms=True ) diff --git a/homeassistant/components/hardware/models.py b/homeassistant/components/hardware/models.py index 4b3c234f4c1..a972b567db2 100644 --- a/homeassistant/components/hardware/models.py +++ b/homeassistant/components/hardware/models.py @@ -3,20 +3,29 @@ from __future__ import annotations from dataclasses import dataclass -from typing import TYPE_CHECKING, Protocol +from typing import Protocol -from homeassistant.core import HomeAssistant, callback +import psutil_home_assistant as ha_psutil -if TYPE_CHECKING: - from .websocket_api import SystemStatus +from homeassistant.components import websocket_api +from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback @dataclass class HardwareData: """Hardware data.""" - hardware_platform: dict[str, HardwareProtocol] = None # type: ignore[assignment] - system_status: SystemStatus = None # type: ignore[assignment] + hardware_platform: dict[str, HardwareProtocol] + system_status: SystemStatus + + +@dataclass(slots=True) +class SystemStatus: + """System status.""" + + ha_psutil: ha_psutil + remove_periodic_timer: CALLBACK_TYPE | None + subscribers: set[tuple[websocket_api.ActiveConnection, int]] @dataclass(slots=True) diff --git a/homeassistant/components/hardware/websocket_api.py b/homeassistant/components/hardware/websocket_api.py index fa441beeae5..599eab34135 100644 --- a/homeassistant/components/hardware/websocket_api.py +++ b/homeassistant/components/hardware/websocket_api.py @@ -3,41 +3,25 @@ from __future__ import annotations import contextlib -from dataclasses import asdict, dataclass +from dataclasses import asdict from datetime import datetime, timedelta from typing import Any -import psutil_home_assistant as ha_psutil import voluptuous as vol from homeassistant.components import websocket_api -from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.event import async_track_time_interval from homeassistant.util import dt as dt_util from .const import DATA_HARDWARE -from .hardware import async_process_hardware_platforms - - -@dataclass(slots=True) -class SystemStatus: - """System status.""" - - ha_psutil: ha_psutil - remove_periodic_timer: CALLBACK_TYPE | None - subscribers: set[tuple[websocket_api.ActiveConnection, int]] async def async_setup(hass: HomeAssistant) -> None: """Set up the hardware websocket API.""" websocket_api.async_register_command(hass, ws_info) websocket_api.async_register_command(hass, ws_subscribe_system_status) - hass.data[DATA_HARDWARE].system_status = SystemStatus( - ha_psutil=await hass.async_add_executor_job(ha_psutil.PsutilWrapper), - remove_periodic_timer=None, - subscribers=set(), - ) @websocket_api.websocket_command( @@ -52,9 +36,6 @@ async def ws_info( """Return hardware info.""" hardware_info = [] - if hass.data[DATA_HARDWARE].hardware_platform is None: - await async_process_hardware_platforms(hass) - hardware_platform = hass.data[DATA_HARDWARE].hardware_platform for platform in hardware_platform.values(): if hasattr(platform, "async_info"): diff --git a/tests/components/hardware/test_websocket_api.py b/tests/components/hardware/test_websocket_api.py index 64fcda02df4..f5591ff8480 100644 --- a/tests/components/hardware/test_websocket_api.py +++ b/tests/components/hardware/test_websocket_api.py @@ -50,7 +50,7 @@ async def test_system_status_subscription( return mock_psutil with patch( - "homeassistant.components.hardware.websocket_api.ha_psutil.PsutilWrapper", + "homeassistant.components.hardware.ha_psutil.PsutilWrapper", wraps=create_mock_psutil, ): assert await async_setup_component(hass, DOMAIN, {})