mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Move hardware initialisation to package module (#144540)
This commit is contained in:
parent
21e2bbd066
commit
b4ae08f83d
@ -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)
|
||||
|
||||
|
@ -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
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -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"):
|
||||
|
@ -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, {})
|
||||
|
Loading…
x
Reference in New Issue
Block a user