Files
homeassistant-core/tests/components/nut/util.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
2.4 KiB
Python
Raw Normal View History

2020-04-11 11:17:57 -05:00
"""Tests for the nut integration."""
import json
2024-03-23 12:02:02 -10:00
from unittest.mock import AsyncMock, MagicMock, patch
2020-04-11 11:17:57 -05:00
from homeassistant.components.nut.const import DOMAIN
2023-04-23 15:09:36 -04:00
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
2020-04-11 11:17:57 -05:00
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
2024-03-23 12:02:02 -10:00
def _get_mock_nutclient(
2023-04-23 15:09:36 -04:00
list_vars=None,
list_ups=None,
list_commands_return_value=None,
list_commands_side_effect=None,
run_command=None,
):
2024-03-23 12:02:02 -10:00
nutclient = MagicMock()
type(nutclient).list_ups = AsyncMock(return_value=list_ups)
type(nutclient).list_vars = AsyncMock(return_value=list_vars)
2023-04-23 15:09:36 -04:00
if list_commands_return_value is None:
list_commands_return_value = {}
2024-03-23 12:02:02 -10:00
type(nutclient).list_commands = AsyncMock(
2023-04-23 15:09:36 -04:00
return_value=list_commands_return_value, side_effect=list_commands_side_effect
)
if run_command is None:
2024-03-23 12:02:02 -10:00
run_command = AsyncMock()
type(nutclient).run_command = run_command
return nutclient
2020-04-11 11:17:57 -05:00
async def async_init_integration(
2023-04-23 15:09:36 -04:00
hass: HomeAssistant,
2024-04-10 08:55:59 +02:00
ups_fixture: str | None = None,
2023-04-23 15:09:36 -04:00
username: str = "mock",
password: str = "mock",
2024-04-10 08:55:59 +02:00
list_ups: dict[str, str] | None = None,
list_vars: dict[str, str] | None = None,
list_commands_return_value: dict[str, str] | None = None,
2023-04-23 15:09:36 -04:00
list_commands_side_effect=None,
2024-04-10 08:55:59 +02:00
run_command: MagicMock | None = None,
2020-04-11 11:17:57 -05:00
) -> MockConfigEntry:
2023-04-23 15:09:36 -04:00
"""Set up the nut integration in Home Assistant."""
2020-04-11 11:17:57 -05:00
2023-04-23 15:09:36 -04:00
if list_ups is None:
list_ups = {"ups1": "UPS 1"}
2020-04-11 11:17:57 -05:00
2023-04-23 15:09:36 -04:00
if ups_fixture is not None:
ups_fixture = f"nut/{ups_fixture}.json"
if list_vars is None:
list_vars = json.loads(load_fixture(ups_fixture))
2024-03-23 12:02:02 -10:00
mock_pynut = _get_mock_nutclient(
2023-04-23 15:09:36 -04:00
list_ups=list_ups,
list_vars=list_vars,
list_commands_return_value=list_commands_return_value,
list_commands_side_effect=list_commands_side_effect,
run_command=run_command,
)
2020-04-11 11:17:57 -05:00
with patch(
2024-03-23 12:02:02 -10:00
"homeassistant.components.nut.AIONUTClient",
2020-08-27 13:56:20 +02:00
return_value=mock_pynut,
2020-04-11 11:17:57 -05:00
):
entry = MockConfigEntry(
domain=DOMAIN,
2023-04-23 15:09:36 -04:00
data={
CONF_HOST: "mock",
CONF_PASSWORD: password,
CONF_PORT: "mock",
CONF_USERNAME: username,
},
2020-04-11 11:17:57 -05:00
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry