Skip to content

Commit b4ae08f

Browse files
authored
Move hardware initialisation to package module (#144540)
1 parent 21e2bbd commit b4ae08f

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

homeassistant/components/hardware/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@
22

33
from __future__ import annotations
44

5+
import psutil_home_assistant as ha_psutil
6+
57
from homeassistant.core import HomeAssistant
68
from homeassistant.helpers import config_validation as cv
79
from homeassistant.helpers.typing import ConfigType
810

911
from . import websocket_api
1012
from .const import DATA_HARDWARE, DOMAIN
11-
from .models import HardwareData
13+
from .hardware import async_process_hardware_platforms
14+
from .models import HardwareData, SystemStatus
1215

1316
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
1417

1518

1619
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
1720
"""Set up Hardware."""
18-
hass.data[DATA_HARDWARE] = HardwareData()
21+
hass.data[DATA_HARDWARE] = HardwareData(
22+
hardware_platform={},
23+
system_status=SystemStatus(
24+
ha_psutil=await hass.async_add_executor_job(ha_psutil.PsutilWrapper),
25+
remove_periodic_timer=None,
26+
subscribers=set(),
27+
),
28+
)
29+
await async_process_hardware_platforms(hass)
1930

2031
await websocket_api.async_setup(hass)
2132

homeassistant/components/hardware/hardware.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ async def async_process_hardware_platforms(
1616
hass: HomeAssistant,
1717
) -> None:
1818
"""Start processing hardware platforms."""
19-
hass.data[DATA_HARDWARE].hardware_platform = {}
20-
2119
await async_process_integration_platforms(
2220
hass, DOMAIN, _register_hardware_platform, wait_for_platforms=True
2321
)

homeassistant/components/hardware/models.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass
6-
from typing import TYPE_CHECKING, Protocol
6+
from typing import Protocol
77

8-
from homeassistant.core import HomeAssistant, callback
8+
import psutil_home_assistant as ha_psutil
99

10-
if TYPE_CHECKING:
11-
from .websocket_api import SystemStatus
10+
from homeassistant.components import websocket_api
11+
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
1212

1313

1414
@dataclass
1515
class HardwareData:
1616
"""Hardware data."""
1717

18-
hardware_platform: dict[str, HardwareProtocol] = None # type: ignore[assignment]
19-
system_status: SystemStatus = None # type: ignore[assignment]
18+
hardware_platform: dict[str, HardwareProtocol]
19+
system_status: SystemStatus
20+
21+
22+
@dataclass(slots=True)
23+
class SystemStatus:
24+
"""System status."""
25+
26+
ha_psutil: ha_psutil
27+
remove_periodic_timer: CALLBACK_TYPE | None
28+
subscribers: set[tuple[websocket_api.ActiveConnection, int]]
2029

2130

2231
@dataclass(slots=True)

homeassistant/components/hardware/websocket_api.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,25 @@
33
from __future__ import annotations
44

55
import contextlib
6-
from dataclasses import asdict, dataclass
6+
from dataclasses import asdict
77
from datetime import datetime, timedelta
88
from typing import Any
99

10-
import psutil_home_assistant as ha_psutil
1110
import voluptuous as vol
1211

1312
from homeassistant.components import websocket_api
14-
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
13+
from homeassistant.core import HomeAssistant, callback
1514
from homeassistant.exceptions import HomeAssistantError
1615
from homeassistant.helpers.event import async_track_time_interval
1716
from homeassistant.util import dt as dt_util
1817

1918
from .const import DATA_HARDWARE
20-
from .hardware import async_process_hardware_platforms
21-
22-
23-
@dataclass(slots=True)
24-
class SystemStatus:
25-
"""System status."""
26-
27-
ha_psutil: ha_psutil
28-
remove_periodic_timer: CALLBACK_TYPE | None
29-
subscribers: set[tuple[websocket_api.ActiveConnection, int]]
3019

3120

3221
async def async_setup(hass: HomeAssistant) -> None:
3322
"""Set up the hardware websocket API."""
3423
websocket_api.async_register_command(hass, ws_info)
3524
websocket_api.async_register_command(hass, ws_subscribe_system_status)
36-
hass.data[DATA_HARDWARE].system_status = SystemStatus(
37-
ha_psutil=await hass.async_add_executor_job(ha_psutil.PsutilWrapper),
38-
remove_periodic_timer=None,
39-
subscribers=set(),
40-
)
4125

4226

4327
@websocket_api.websocket_command(
@@ -52,9 +36,6 @@ async def ws_info(
5236
"""Return hardware info."""
5337
hardware_info = []
5438

55-
if hass.data[DATA_HARDWARE].hardware_platform is None:
56-
await async_process_hardware_platforms(hass)
57-
5839
hardware_platform = hass.data[DATA_HARDWARE].hardware_platform
5940
for platform in hardware_platform.values():
6041
if hasattr(platform, "async_info"):

tests/components/hardware/test_websocket_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def create_mock_psutil():
5050
return mock_psutil
5151

5252
with patch(
53-
"homeassistant.components.hardware.websocket_api.ha_psutil.PsutilWrapper",
53+
"homeassistant.components.hardware.ha_psutil.PsutilWrapper",
5454
wraps=create_mock_psutil,
5555
):
5656
assert await async_setup_component(hass, DOMAIN, {})

0 commit comments

Comments
 (0)