mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +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
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import psutil_home_assistant as ha_psutil
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from . import websocket_api
|
from . import websocket_api
|
||||||
from .const import DATA_HARDWARE, DOMAIN
|
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)
|
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up Hardware."""
|
"""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)
|
await websocket_api.async_setup(hass)
|
||||||
|
|
||||||
|
@ -16,8 +16,6 @@ async def async_process_hardware_platforms(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Start processing hardware platforms."""
|
"""Start processing hardware platforms."""
|
||||||
hass.data[DATA_HARDWARE].hardware_platform = {}
|
|
||||||
|
|
||||||
await async_process_integration_platforms(
|
await async_process_integration_platforms(
|
||||||
hass, DOMAIN, _register_hardware_platform, wait_for_platforms=True
|
hass, DOMAIN, _register_hardware_platform, wait_for_platforms=True
|
||||||
)
|
)
|
||||||
|
@ -3,20 +3,29 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
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 homeassistant.components import websocket_api
|
||||||
from .websocket_api import SystemStatus
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class HardwareData:
|
class HardwareData:
|
||||||
"""Hardware data."""
|
"""Hardware data."""
|
||||||
|
|
||||||
hardware_platform: dict[str, HardwareProtocol] = None # type: ignore[assignment]
|
hardware_platform: dict[str, HardwareProtocol]
|
||||||
system_status: SystemStatus = None # type: ignore[assignment]
|
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)
|
@dataclass(slots=True)
|
||||||
|
@ -3,41 +3,25 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
from dataclasses import asdict, dataclass
|
from dataclasses import asdict
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import psutil_home_assistant as ha_psutil
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import websocket_api
|
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.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.event import async_track_time_interval
|
from homeassistant.helpers.event import async_track_time_interval
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import DATA_HARDWARE
|
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:
|
async def async_setup(hass: HomeAssistant) -> None:
|
||||||
"""Set up the hardware websocket API."""
|
"""Set up the hardware websocket API."""
|
||||||
websocket_api.async_register_command(hass, ws_info)
|
websocket_api.async_register_command(hass, ws_info)
|
||||||
websocket_api.async_register_command(hass, ws_subscribe_system_status)
|
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(
|
@websocket_api.websocket_command(
|
||||||
@ -52,9 +36,6 @@ async def ws_info(
|
|||||||
"""Return hardware info."""
|
"""Return hardware info."""
|
||||||
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
|
hardware_platform = hass.data[DATA_HARDWARE].hardware_platform
|
||||||
for platform in hardware_platform.values():
|
for platform in hardware_platform.values():
|
||||||
if hasattr(platform, "async_info"):
|
if hasattr(platform, "async_info"):
|
||||||
|
@ -50,7 +50,7 @@ async def test_system_status_subscription(
|
|||||||
return mock_psutil
|
return mock_psutil
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.hardware.websocket_api.ha_psutil.PsutilWrapper",
|
"homeassistant.components.hardware.ha_psutil.PsutilWrapper",
|
||||||
wraps=create_mock_psutil,
|
wraps=create_mock_psutil,
|
||||||
):
|
):
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user