* Opt in to install Mosquitto broker add-on in MQTT config flow * rephrase * Tests with supervisor and running add-on * Complete tests for success flows * Also set up entry in success flow * Use realistic names for addon and broker * Finetuning and fail test cases * Spelling * Improve translation strings * Update addon docstr Co-authored-by: Erik Montnemery <erik@montnemery.com> * Raise AddonError if add-on does not start * Only show the option to use the add-on * Simplify flow, rework and cleanup * Revert unrelated cleanup, process suggestion * Move ADDON_SLUG const to addon module * Move fixture to component level * Move back supervisor fixture * Move addon_setup_time_fixture and superfixe to config flow model tests * Refactor hassio fixture * Rename helpers as they are no fixtures, remove fixture from their names --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
125 lines
3.8 KiB
Python
125 lines
3.8 KiB
Python
"""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 mock_discovery_info() -> Any:
|
|
"""Return the discovery info from the supervisor."""
|
|
return DEFAULT
|
|
|
|
|
|
def mock_get_addon_discovery_info(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 mock_addon_store_info() -> 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 mock_addon_info() -> 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 mock_addon_not_installed(
|
|
addon_store_info: AsyncMock, addon_info: AsyncMock
|
|
) -> AsyncMock:
|
|
"""Mock add-on not installed."""
|
|
addon_store_info.return_value["available"] = True
|
|
return addon_info
|
|
|
|
|
|
def mock_addon_installed(
|
|
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 mock_addon_running(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 mock_install_addon(
|
|
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 mock_start_addon() -> Generator[AsyncMock]:
|
|
"""Mock start add-on."""
|
|
with patch(
|
|
"homeassistant.components.hassio.addon_manager.async_start_addon"
|
|
) as start_addon:
|
|
yield start_addon
|