Migrate nut to use aionut (#114078)
This commit is contained in:
parent
d4f158d079
commit
4ac439ef88
11 changed files with 123 additions and 107 deletions
|
@ -3,7 +3,7 @@
|
|||
from ipaddress import ip_address
|
||||
from unittest.mock import patch
|
||||
|
||||
from pynut2.nut2 import PyNUTError
|
||||
from aionut import NUTError
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow, setup
|
||||
from homeassistant.components import zeroconf
|
||||
|
@ -20,7 +20,7 @@ from homeassistant.const import (
|
|||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .util import _get_mock_pynutclient
|
||||
from .util import _get_mock_nutclient
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -51,12 +51,12 @@ async def test_form_zeroconf(hass: HomeAssistant) -> None:
|
|||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
|
@ -89,12 +89,12 @@ async def test_form_user_one_ups(hass: HomeAssistant) -> None:
|
|||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
|
@ -138,13 +138,13 @@ async def test_form_user_multiple_ups(hass: HomeAssistant) -> None:
|
|||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
list_ups={"ups1": "UPS 1", "ups2": "UPS2"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
|
@ -161,7 +161,7 @@ async def test_form_user_multiple_ups(hass: HomeAssistant) -> None:
|
|||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
|
@ -199,12 +199,12 @@ async def test_form_user_one_ups_with_ignored_entry(hass: HomeAssistant) -> None
|
|||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage", "ups.status": "OL"}, list_ups=["ups1"]
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
), patch(
|
||||
"homeassistant.components.nut.async_setup_entry",
|
||||
|
@ -238,10 +238,10 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
|||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient()
|
||||
mock_pynut = _get_mock_nutclient()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
|
@ -258,11 +258,11 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
|||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_ups",
|
||||
side_effect=PyNUTError,
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
side_effect=NUTError,
|
||||
), patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_vars",
|
||||
side_effect=PyNUTError,
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
@ -278,11 +278,11 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
|||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_ups",
|
||||
return_value=["ups1"],
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
return_value={"ups1"},
|
||||
), patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_vars",
|
||||
side_effect=TypeError,
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
|
@ -314,13 +314,13 @@ async def test_abort_if_already_setup(hass: HomeAssistant) -> None:
|
|||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
list_ups={"ups1": "UPS 1"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
|
@ -352,13 +352,13 @@ async def test_abort_if_already_setup_alias(hass: HomeAssistant) -> None:
|
|||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_vars={"battery.voltage": "voltage"},
|
||||
list_ups={"ups1": "UPS 1", "ups2": "UPS 2"},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
|
@ -373,7 +373,7 @@ async def test_abort_if_already_setup_alias(hass: HomeAssistant) -> None:
|
|||
assert result2["type"] == data_entry_flow.FlowResultType.FORM
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
"""The tests for Network UPS Tools (NUT) device actions."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pynut2.nut2 import PyNUTError
|
||||
from aionut import NUTError
|
||||
import pytest
|
||||
from pytest_unordered import unordered
|
||||
|
||||
|
@ -99,7 +99,7 @@ async def test_list_commands_exception(
|
|||
) -> None:
|
||||
"""Test there are no actions if list_commands raises exception."""
|
||||
await async_init_integration(
|
||||
hass, list_vars={"ups.status": "OL"}, list_commands_side_effect=PyNUTError
|
||||
hass, list_vars={"ups.status": "OL"}, list_commands_side_effect=NUTError
|
||||
)
|
||||
|
||||
device_entry = next(device for device in device_registry.devices.values())
|
||||
|
@ -137,7 +137,7 @@ async def test_action(hass: HomeAssistant, device_registry: dr.DeviceRegistry) -
|
|||
"beeper.enable": None,
|
||||
"beeper.disable": None,
|
||||
}
|
||||
run_command = MagicMock()
|
||||
run_command = AsyncMock()
|
||||
await async_init_integration(
|
||||
hass,
|
||||
list_ups={"someUps": "Some UPS"},
|
||||
|
@ -196,7 +196,7 @@ async def test_rund_command_exception(
|
|||
|
||||
list_commands_return_value = {"beeper.enable": None}
|
||||
error_message = "Something wrong happened"
|
||||
run_command = MagicMock(side_effect=PyNUTError(error_message))
|
||||
run_command = AsyncMock(side_effect=NUTError(error_message))
|
||||
await async_init_integration(
|
||||
hass,
|
||||
list_vars={"ups.status": "OL"},
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
from unittest.mock import patch
|
||||
|
||||
from aionut import NUTError
|
||||
|
||||
from homeassistant.components.nut.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, STATE_UNAVAILABLE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .util import _get_mock_pynutclient
|
||||
from .util import _get_mock_nutclient
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -20,12 +22,12 @@ async def test_async_setup_entry(hass: HomeAssistant) -> None:
|
|||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"}, list_vars={"ups.status": "OL"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
@ -55,11 +57,11 @@ async def test_config_not_ready(hass: HomeAssistant) -> None:
|
|||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_ups",
|
||||
return_value=["ups1"],
|
||||
"homeassistant.components.nut.AIONUTClient.list_ups",
|
||||
return_value={"ups1"},
|
||||
), patch(
|
||||
"homeassistant.components.nut.PyNUTClient.list_vars",
|
||||
side_effect=ConnectionResetError,
|
||||
"homeassistant.components.nut.AIONUTClient.list_vars",
|
||||
side_effect=NUTError,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
|
|
@ -15,7 +15,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .util import _get_mock_pynutclient, async_init_integration
|
||||
from .util import _get_mock_nutclient, async_init_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
@ -100,12 +100,12 @@ async def test_state_sensors(hass: HomeAssistant) -> None:
|
|||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"}, list_vars={"ups.status": "OL"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
@ -125,12 +125,12 @@ async def test_unknown_state_sensors(hass: HomeAssistant) -> None:
|
|||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"}, list_vars={"ups.status": "OQ"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
@ -155,12 +155,12 @@ async def test_stale_options(hass: HomeAssistant) -> None:
|
|||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups={"ups1": "UPS 1"}, list_vars={"battery.charge": "10"}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Tests for the nut integration."""
|
||||
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from homeassistant.components.nut.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
||||
|
@ -10,25 +10,25 @@ from homeassistant.core import HomeAssistant
|
|||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
def _get_mock_pynutclient(
|
||||
def _get_mock_nutclient(
|
||||
list_vars=None,
|
||||
list_ups=None,
|
||||
list_commands_return_value=None,
|
||||
list_commands_side_effect=None,
|
||||
run_command=None,
|
||||
):
|
||||
pynutclient = MagicMock()
|
||||
type(pynutclient).list_ups = MagicMock(return_value=list_ups)
|
||||
type(pynutclient).list_vars = MagicMock(return_value=list_vars)
|
||||
nutclient = MagicMock()
|
||||
type(nutclient).list_ups = AsyncMock(return_value=list_ups)
|
||||
type(nutclient).list_vars = AsyncMock(return_value=list_vars)
|
||||
if list_commands_return_value is None:
|
||||
list_commands_return_value = {}
|
||||
type(pynutclient).list_commands = MagicMock(
|
||||
type(nutclient).list_commands = AsyncMock(
|
||||
return_value=list_commands_return_value, side_effect=list_commands_side_effect
|
||||
)
|
||||
if run_command is None:
|
||||
run_command = MagicMock()
|
||||
type(pynutclient).run_command = run_command
|
||||
return pynutclient
|
||||
run_command = AsyncMock()
|
||||
type(nutclient).run_command = run_command
|
||||
return nutclient
|
||||
|
||||
|
||||
async def async_init_integration(
|
||||
|
@ -52,7 +52,7 @@ async def async_init_integration(
|
|||
if list_vars is None:
|
||||
list_vars = json.loads(load_fixture(ups_fixture))
|
||||
|
||||
mock_pynut = _get_mock_pynutclient(
|
||||
mock_pynut = _get_mock_nutclient(
|
||||
list_ups=list_ups,
|
||||
list_vars=list_vars,
|
||||
list_commands_return_value=list_commands_return_value,
|
||||
|
@ -61,7 +61,7 @@ async def async_init_integration(
|
|||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nut.PyNUTClient",
|
||||
"homeassistant.components.nut.AIONUTClient",
|
||||
return_value=mock_pynut,
|
||||
):
|
||||
entry = MockConfigEntry(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue