mirror of
https://github.com/home-assistant/core.git
synced 2025-04-19 14:57:52 +00:00
Fix hassio mocking in ESPHome dashboard tests (#143212)
This commit is contained in:
parent
221a8597da
commit
9b1ab34352
@ -81,6 +81,7 @@ async def test_restore_dashboard_storage_end_to_end(
|
||||
assert mock_dashboard_api.mock_calls[0][1][0] == "http://new-host:6052"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("hassio_stubs")
|
||||
async def test_restore_dashboard_storage_skipped_if_addon_uninstalled(
|
||||
hass: HomeAssistant,
|
||||
hass_storage: dict[str, Any],
|
||||
@ -105,9 +106,7 @@ async def test_restore_dashboard_storage_skipped_if_addon_uninstalled(
|
||||
return_value={},
|
||||
),
|
||||
):
|
||||
await async_setup_component(hass, "hassio", {})
|
||||
await hass.async_block_till_done()
|
||||
await async_setup_component(hass, DOMAIN, {})
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
assert "test-slug is no longer installed" in caplog.text
|
||||
assert not mock_dashboard_api.called
|
||||
|
@ -3,17 +3,16 @@
|
||||
from collections.abc import Generator
|
||||
import os
|
||||
import re
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aiohasupervisor.models import AddonsStats, AddonState
|
||||
from aiohttp.test_utils import TestClient
|
||||
import pytest
|
||||
|
||||
from homeassistant.auth.models import RefreshToken
|
||||
from homeassistant.components.hassio.handler import HassIO, HassioAPIError
|
||||
from homeassistant.components.hassio.handler import HassIO
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import SUPERVISOR_TOKEN
|
||||
|
||||
@ -31,55 +30,6 @@ def disable_security_filter() -> Generator[None]:
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hassio_env(supervisor_is_connected: AsyncMock) -> Generator[None]:
|
||||
"""Fixture to inject hassio env."""
|
||||
with (
|
||||
patch.dict(os.environ, {"SUPERVISOR": "127.0.0.1"}),
|
||||
patch.dict(os.environ, {"SUPERVISOR_TOKEN": SUPERVISOR_TOKEN}),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_info",
|
||||
Mock(side_effect=HassioAPIError()),
|
||||
),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def hassio_stubs(
|
||||
hassio_env: None,
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
supervisor_client: AsyncMock,
|
||||
) -> RefreshToken:
|
||||
"""Create mock hassio http client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.update_hass_api",
|
||||
return_value={"result": "ok"},
|
||||
) as hass_api,
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.update_hass_timezone",
|
||||
return_value={"result": "ok"},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_info",
|
||||
side_effect=HassioAPIError(),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_ingress_panels",
|
||||
return_value={"panels": []},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.issues.SupervisorIssues.setup",
|
||||
),
|
||||
):
|
||||
await async_setup_component(hass, "hassio", {})
|
||||
|
||||
return hass_api.call_args[0][1]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def hassio_client(
|
||||
hassio_stubs: RefreshToken, hass: HomeAssistant, hass_client: ClientSessionGenerator
|
||||
|
@ -119,8 +119,10 @@ from .typing import (
|
||||
if TYPE_CHECKING:
|
||||
# Local import to avoid processing recorder and SQLite modules when running a
|
||||
# testcase which does not use the recorder.
|
||||
from homeassistant.auth.models import RefreshToken
|
||||
from homeassistant.components import recorder
|
||||
|
||||
|
||||
pytest.register_assert_rewrite("tests.common")
|
||||
|
||||
from .common import ( # noqa: E402, isort:skip
|
||||
@ -1894,6 +1896,67 @@ def mock_bleak_scanner_start() -> Generator[MagicMock]:
|
||||
yield mock_bleak_scanner_start
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hassio_env(supervisor_is_connected: AsyncMock) -> Generator[None]:
|
||||
"""Fixture to inject hassio env."""
|
||||
from homeassistant.components.hassio import ( # pylint: disable=import-outside-toplevel
|
||||
HassioAPIError,
|
||||
)
|
||||
|
||||
from .components.hassio import ( # pylint: disable=import-outside-toplevel
|
||||
SUPERVISOR_TOKEN,
|
||||
)
|
||||
|
||||
with (
|
||||
patch.dict(os.environ, {"SUPERVISOR": "127.0.0.1"}),
|
||||
patch.dict(os.environ, {"SUPERVISOR_TOKEN": SUPERVISOR_TOKEN}),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_info",
|
||||
Mock(side_effect=HassioAPIError()),
|
||||
),
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def hassio_stubs(
|
||||
hassio_env: None,
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
supervisor_client: AsyncMock,
|
||||
) -> RefreshToken:
|
||||
"""Create mock hassio http client."""
|
||||
from homeassistant.components.hassio import ( # pylint: disable=import-outside-toplevel
|
||||
HassioAPIError,
|
||||
)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.update_hass_api",
|
||||
return_value={"result": "ok"},
|
||||
) as hass_api,
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.update_hass_timezone",
|
||||
return_value={"result": "ok"},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_info",
|
||||
side_effect=HassioAPIError(),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.HassIO.get_ingress_panels",
|
||||
return_value={"panels": []},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.hassio.issues.SupervisorIssues.setup",
|
||||
),
|
||||
):
|
||||
await async_setup_component(hass, "hassio", {})
|
||||
|
||||
return hass_api.call_args[0][1]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def integration_frame_path() -> str:
|
||||
"""Return the path to the integration frame.
|
||||
|
Loading…
x
Reference in New Issue
Block a user