Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install and start Mosquitto MQTT broker add on from MQTT config flow #124106

Merged
merged 22 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor hassio fixture
  • Loading branch information
jbouwh committed Aug 21, 2024
commit fd89e8819380eb48843e461bdac3c19f354cbe84
112 changes: 37 additions & 75 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from importlib.util import find_spec
from pathlib import Path
from typing import TYPE_CHECKING, Any
from unittest.mock import DEFAULT, AsyncMock, MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

Expand Down Expand Up @@ -185,125 +185,87 @@ def mock_legacy_device_tracker_setup() -> Callable[[HomeAssistant, MockScanner],
@pytest.fixture(name="discovery_info")
def discovery_info_fixture() -> Any:
"""Return the discovery info from the supervisor."""
return DEFAULT
# pylint: disable-next=import-outside-toplevel
from .hassio.common import discovery_info_fixture

return discovery_info_fixture()


@pytest.fixture(name="get_addon_discovery_info", autouse=True)
@pytest.fixture(name="get_addon_discovery_info")
def get_addon_discovery_info_fixture(discovery_info: Any) -> Generator[AsyncMock]:
"""Mock get add-on discovery info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info",
return_value=discovery_info,
) as get_addon_discovery_info:
yield get_addon_discovery_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import get_addon_discovery_info_fixture

yield from get_addon_discovery_info_fixture(discovery_info)


@pytest.fixture(name="addon_store_info")
def addon_store_info_fixture() -> Generator[AsyncMock]:
"""Mock Supervisor add-on store info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_store_info"
) as addon_store_info:
addon_store_info.return_value = {
"available": False,
"installed": None,
"state": None,
"version": "1.0.0",
}
yield addon_store_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import addon_store_info_fixture

yield from addon_store_info_fixture()


@pytest.fixture(name="addon_info")
def addon_info_fixture() -> Generator[AsyncMock]:
"""Mock Supervisor add-on info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_info",
) as addon_info:
addon_info.return_value = {
"available": False,
"hostname": None,
"options": {},
"state": None,
"update_available": False,
"version": None,
}
yield addon_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import addon_info_fixture

yield from addon_info_fixture()


@pytest.fixture(name="addon_not_installed")
def addon_not_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on not installed."""
addon_store_info.return_value["available"] = True
return addon_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import addon_not_installed_fixture

return addon_not_installed_fixture(addon_store_info, addon_info)


@pytest.fixture(name="addon_installed")
def addon_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already installed but not running."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "stopped",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["hostname"] = "core-matter-server"
addon_info.return_value["state"] = "stopped"
addon_info.return_value["version"] = "1.0.0"
return addon_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import addon_installed_fixture

return addon_installed_fixture(addon_store_info, addon_info)


@pytest.fixture(name="addon_running")
def addon_running_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already running."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "started",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["hostname"] = "core-mosquitto"
addon_info.return_value["state"] = "started"
addon_info.return_value["version"] = "1.0.0"
return addon_info
# pylint: disable-next=import-outside-toplevel
from .hassio.common import addon_running_fixture

return addon_running_fixture(addon_store_info, addon_info)


@pytest.fixture(name="install_addon")
def install_addon_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Generator[AsyncMock]:
"""Mock install add-on."""
# pylint: disable-next=import-outside-toplevel
from .hassio.common import install_addon_fixture

async def install_addon_side_effect(hass: HomeAssistant, slug: str) -> None:
"""Mock install add-on."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "stopped",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["state"] = "stopped"
addon_info.return_value["version"] = "1.0.0"

with patch(
"homeassistant.components.hassio.addon_manager.async_install_addon"
) as install_addon:
install_addon.side_effect = install_addon_side_effect
yield install_addon
yield from install_addon_fixture(addon_store_info, addon_info)


@pytest.fixture(name="start_addon")
def start_addon_fixture() -> Generator[AsyncMock]:
"""Mock start add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_start_addon"
) as start_addon:
yield start_addon
# pylint: disable-next=import-outside-toplevel
from .hassio.common import start_addon_fixture

yield from start_addon_fixture()
127 changes: 127 additions & 0 deletions tests/components/hassio/common.py
jbouwh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Provide common test tools for hassio."""

from __future__ import annotations

from collections.abc import Generator
from typing import Any
from unittest.mock import DEFAULT, AsyncMock, patch

from homeassistant.core import HomeAssistant


def discovery_info_fixture() -> Any:
"""Return the discovery info from the supervisor."""
return DEFAULT


def get_addon_discovery_info_fixture(discovery_info: Any) -> Generator[AsyncMock]:
"""Mock get add-on discovery info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info",
return_value=discovery_info,
) as get_addon_discovery_info:
yield get_addon_discovery_info


def addon_store_info_fixture() -> Generator[AsyncMock]:
"""Mock Supervisor add-on store info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_store_info"
) as addon_store_info:
addon_store_info.return_value = {
"available": False,
"installed": None,
"state": None,
"version": "1.0.0",
}
yield addon_store_info


def addon_info_fixture() -> Generator[AsyncMock]:
"""Mock Supervisor add-on info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_info",
) as addon_info:
addon_info.return_value = {
"available": False,
"hostname": None,
"options": {},
"state": None,
"update_available": False,
"version": None,
}
yield addon_info


def addon_not_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on not installed."""
addon_store_info.return_value["available"] = True
return addon_info


def addon_installed_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already installed but not running."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "stopped",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["hostname"] = "core-matter-server"
addon_info.return_value["state"] = "stopped"
addon_info.return_value["version"] = "1.0.0"
return addon_info


def addon_running_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already running."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "started",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["hostname"] = "core-mosquitto"
addon_info.return_value["state"] = "started"
addon_info.return_value["version"] = "1.0.0"
return addon_info


def install_addon_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Generator[AsyncMock]:
"""Mock install add-on."""

async def install_addon_side_effect(hass: HomeAssistant, slug: str) -> None:
"""Mock install add-on."""
addon_store_info.return_value = {
"available": True,
"installed": "1.0.0",
"state": "stopped",
"version": "1.0.0",
}
addon_info.return_value["available"] = True
addon_info.return_value["state"] = "stopped"
addon_info.return_value["version"] = "1.0.0"

with patch(
"homeassistant.components.hassio.addon_manager.async_install_addon"
) as install_addon:
install_addon.side_effect = install_addon_side_effect
yield install_addon


def start_addon_fixture() -> Generator[AsyncMock]:
"""Mock start add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_start_addon"
) as start_addon:
yield start_addon
5 changes: 5 additions & 0 deletions tests/components/mqtt/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ def addon_setup_time_fixture() -> Generator[int]:
yield addon_setup_time


@pytest.fixture(autouse=True)
def mock_get_addon_discovery_info(get_addon_discovery_info: AsyncMock) -> None:
"""Mock get add-on discovery info."""


@pytest.mark.usefixtures("mqtt_client_mock")
async def test_user_connection_works(
hass: HomeAssistant,
Expand Down
Loading