init home-assistant
This commit is contained in:
1
home-assistant/tests/components/pyload/__init__.py
Normal file
1
home-assistant/tests/components/pyload/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the pyLoad component."""
|
||||
121
home-assistant/tests/components/pyload/conftest.py
Normal file
121
home-assistant/tests/components/pyload/conftest.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""Fixtures for pyLoad integration tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from pyloadapi.types import LoginResponse, StatusServerResponse
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN
|
||||
from homeassistant.const import (
|
||||
CONF_HOST,
|
||||
CONF_MONITORED_VARIABLES,
|
||||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_PLATFORM,
|
||||
CONF_PORT,
|
||||
CONF_SSL,
|
||||
CONF_USERNAME,
|
||||
CONF_VERIFY_SSL,
|
||||
)
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
USER_INPUT = {
|
||||
CONF_HOST: "pyload.local",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PORT: 8000,
|
||||
CONF_SSL: True,
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_VERIFY_SSL: False,
|
||||
}
|
||||
|
||||
YAML_INPUT = {
|
||||
CONF_HOST: "pyload.local",
|
||||
CONF_MONITORED_VARIABLES: ["speed"],
|
||||
CONF_NAME: "test-name",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_PLATFORM: "pyload",
|
||||
CONF_PORT: 8000,
|
||||
CONF_SSL: True,
|
||||
CONF_USERNAME: "test-username",
|
||||
}
|
||||
REAUTH_INPUT = {
|
||||
CONF_PASSWORD: "new-password",
|
||||
CONF_USERNAME: "new-username",
|
||||
}
|
||||
|
||||
NEW_INPUT = {
|
||||
CONF_HOST: "pyload.local",
|
||||
CONF_PASSWORD: "new-password",
|
||||
CONF_PORT: 8000,
|
||||
CONF_SSL: True,
|
||||
CONF_USERNAME: "new-username",
|
||||
CONF_VERIFY_SSL: False,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.pyload.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pyload_config() -> ConfigType:
|
||||
"""Mock pyload configuration entry."""
|
||||
return {"sensor": YAML_INPUT}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_pyloadapi() -> Generator[MagicMock]:
|
||||
"""Mock PyLoadAPI."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.pyload.PyLoadAPI", autospec=True
|
||||
) as mock_client,
|
||||
patch("homeassistant.components.pyload.config_flow.PyLoadAPI", new=mock_client),
|
||||
):
|
||||
client = mock_client.return_value
|
||||
client.username = "username"
|
||||
client.api_url = "https://pyload.local:8000/"
|
||||
client.login.return_value = LoginResponse(
|
||||
{
|
||||
"_permanent": True,
|
||||
"authenticated": True,
|
||||
"id": 2,
|
||||
"name": "username",
|
||||
"role": 0,
|
||||
"perms": 0,
|
||||
"template": "default",
|
||||
"_flashes": [["message", "Logged in successfully"]],
|
||||
}
|
||||
)
|
||||
|
||||
client.get_status.return_value = StatusServerResponse(
|
||||
{
|
||||
"pause": False,
|
||||
"active": 1,
|
||||
"queue": 6,
|
||||
"total": 37,
|
||||
"speed": 5405963.0,
|
||||
"download": True,
|
||||
"reconnect": False,
|
||||
"captcha": False,
|
||||
}
|
||||
)
|
||||
client.version.return_value = "0.5.0"
|
||||
client.free_space.return_value = 99999999999
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(name="config_entry")
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Mock pyLoad configuration entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN, title=DEFAULT_NAME, data=USER_INPUT, entry_id="XXXXXXXXXXXXXX"
|
||||
)
|
||||
@@ -0,0 +1,185 @@
|
||||
# serializer version: 1
|
||||
# name: test_state[button.pyload_abort_all_running_downloads-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.pyload_abort_all_running_downloads',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Abort all running downloads',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadButtonEntity.ABORT_DOWNLOADS: 'abort_downloads'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_abort_downloads',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_abort_all_running_downloads-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'pyLoad Abort all running downloads',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.pyload_abort_all_running_downloads',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_delete_finished_files_packages-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.pyload_delete_finished_files_packages',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Delete finished files/packages',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadButtonEntity.DELETE_FINISHED: 'delete_finished'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_delete_finished',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_delete_finished_files_packages-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'pyLoad Delete finished files/packages',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.pyload_delete_finished_files_packages',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_restart_all_failed_files-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.pyload_restart_all_failed_files',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Restart all failed files',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadButtonEntity.RESTART_FAILED: 'restart_failed'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_restart_failed',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_restart_all_failed_files-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'pyLoad Restart all failed files',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.pyload_restart_all_failed_files',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_restart_pyload_core-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.pyload_restart_pyload_core',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Restart pyload core',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadButtonEntity.RESTART: 'restart'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_restart',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[button.pyload_restart_pyload_core-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'pyLoad Restart pyload core',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.pyload_restart_pyload_core',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,24 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics
|
||||
dict({
|
||||
'config_entry_data': dict({
|
||||
'host': '**REDACTED**',
|
||||
'password': '**REDACTED**',
|
||||
'port': 8000,
|
||||
'ssl': True,
|
||||
'username': '**REDACTED**',
|
||||
'verify_ssl': False,
|
||||
}),
|
||||
'pyload_data': dict({
|
||||
'active': 1,
|
||||
'captcha': False,
|
||||
'download': True,
|
||||
'free_space': 99999999999,
|
||||
'pause': False,
|
||||
'queue': 6,
|
||||
'reconnect': False,
|
||||
'speed': 5405963.0,
|
||||
'total': 37,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
1033
home-assistant/tests/components/pyload/snapshots/test_sensor.ambr
Normal file
1033
home-assistant/tests/components/pyload/snapshots/test_sensor.ambr
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,95 @@
|
||||
# serializer version: 1
|
||||
# name: test_state[switch.pyload_auto_reconnect-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.pyload_auto_reconnect',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Auto-Reconnect',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadSwitch.RECONNECT: 'reconnect'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_reconnect',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[switch.pyload_auto_reconnect-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'pyLoad Auto-Reconnect',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.pyload_auto_reconnect',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_state[switch.pyload_pause_resume_queue-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.pyload_pause_resume_queue',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Pause/Resume queue',
|
||||
'platform': 'pyload',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': <PyLoadSwitch.PAUSE_RESUME_QUEUE: 'download'>,
|
||||
'unique_id': 'XXXXXXXXXXXXXX_download',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_state[switch.pyload_pause_resume_queue-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'pyLoad Pause/Resume queue',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.pyload_pause_resume_queue',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
122
home-assistant/tests/components/pyload/test_button.py
Normal file
122
home-assistant/tests/components/pyload/test_button.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""The tests for the button component."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, call, patch
|
||||
|
||||
from pyloadapi import CannotConnect, InvalidAuth
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.components.pyload.button import PyLoadButtonEntity
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
API_CALL = {
|
||||
PyLoadButtonEntity.ABORT_DOWNLOADS: call.stop_all_downloads,
|
||||
PyLoadButtonEntity.RESTART_FAILED: call.restart_failed,
|
||||
PyLoadButtonEntity.DELETE_FINISHED: call.delete_finished,
|
||||
PyLoadButtonEntity.RESTART: call.restart,
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def button_only() -> Generator[None]:
|
||||
"""Enable only the button platform."""
|
||||
with patch(
|
||||
"homeassistant.components.pyload.PLATFORMS",
|
||||
[Platform.BUTTON],
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_state(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test button state."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_button_press(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test button press method."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
)
|
||||
|
||||
for entity_entry in entity_entries:
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_entry.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
assert API_CALL[entity_entry.translation_key] in mock_pyloadapi.method_calls
|
||||
mock_pyloadapi.reset_mock()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect"),
|
||||
[CannotConnect, InvalidAuth],
|
||||
)
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_button_press_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
side_effect: Exception,
|
||||
) -> None:
|
||||
"""Test button press method."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
)
|
||||
mock_pyloadapi.stop_all_downloads.side_effect = side_effect
|
||||
mock_pyloadapi.restart_failed.side_effect = side_effect
|
||||
mock_pyloadapi.delete_finished.side_effect = side_effect
|
||||
mock_pyloadapi.restart.side_effect = side_effect
|
||||
|
||||
for entity_entry in entity_entries:
|
||||
with pytest.raises(ServiceValidationError):
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_entry.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
310
home-assistant/tests/components/pyload/test_config_flow.py
Normal file
310
home-assistant/tests/components/pyload/test_config_flow.py
Normal file
@@ -0,0 +1,310 @@
|
||||
"""Test the pyLoad config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import NEW_INPUT, REAUTH_INPUT, USER_INPUT, YAML_INPUT
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test we get the form."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == DEFAULT_NAME
|
||||
assert result["data"] == USER_INPUT
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "expected_error"),
|
||||
[
|
||||
(InvalidAuth, "invalid_auth"),
|
||||
(CannotConnect, "cannot_connect"),
|
||||
(ParserError, "cannot_connect"),
|
||||
(ValueError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_form_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
exception: Exception,
|
||||
expected_error: str,
|
||||
) -> None:
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
mock_pyloadapi.login.side_effect = exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": expected_error}
|
||||
|
||||
mock_pyloadapi.login.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == DEFAULT_NAME
|
||||
assert result["data"] == USER_INPUT
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_flow_user_already_configured(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, mock_pyloadapi: AsyncMock
|
||||
) -> None:
|
||||
"""Test we abort user data set when entry is already configured."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_flow_import(
|
||||
hass: HomeAssistant,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test that we can import a YAML config."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=YAML_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "test-name"
|
||||
assert result["data"] == USER_INPUT
|
||||
|
||||
|
||||
async def test_flow_import_already_configured(
|
||||
hass: HomeAssistant, config_entry: MockConfigEntry, mock_pyloadapi: AsyncMock
|
||||
) -> None:
|
||||
"""Test we abort import data set when entry is already configured."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=YAML_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "reason"),
|
||||
[
|
||||
(InvalidAuth, "invalid_auth"),
|
||||
(CannotConnect, "cannot_connect"),
|
||||
(ParserError, "cannot_connect"),
|
||||
(ValueError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_flow_import_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
exception: Exception,
|
||||
reason: str,
|
||||
) -> None:
|
||||
"""Test we abort import data set when entry is already configured."""
|
||||
|
||||
mock_pyloadapi.login.side_effect = exception
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data=YAML_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == reason
|
||||
|
||||
|
||||
async def test_reauth(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test reauth flow."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
REAUTH_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert config_entry.data == NEW_INPUT
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "error_text"),
|
||||
[
|
||||
(InvalidAuth, "invalid_auth"),
|
||||
(CannotConnect, "cannot_connect"),
|
||||
(IndexError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_reauth_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
side_effect: Exception,
|
||||
error_text: str,
|
||||
) -> None:
|
||||
"""Test reauth flow."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await config_entry.start_reauth_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
mock_pyloadapi.login.side_effect = side_effect
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
REAUTH_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error_text}
|
||||
|
||||
mock_pyloadapi.login.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
REAUTH_INPUT,
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert config_entry.data == NEW_INPUT
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
async def test_reconfiguration(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test reconfiguration flow."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert config_entry.data == USER_INPUT
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "error_text"),
|
||||
[
|
||||
(InvalidAuth, "invalid_auth"),
|
||||
(CannotConnect, "cannot_connect"),
|
||||
(IndexError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
side_effect: Exception,
|
||||
error_text: str,
|
||||
) -> None:
|
||||
"""Test reconfiguration flow."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
result = await config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
mock_pyloadapi.login.side_effect = side_effect
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error_text}
|
||||
|
||||
mock_pyloadapi.login.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
USER_INPUT,
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert config_entry.data == USER_INPUT
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
28
home-assistant/tests/components/pyload/test_diagnostics.py
Normal file
28
home-assistant/tests/components/pyload/test_diagnostics.py
Normal file
@@ -0,0 +1,28 @@
|
||||
"""Tests for pyLoad diagnostics."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||
== snapshot
|
||||
)
|
||||
65
home-assistant/tests/components/pyload/test_init.py
Normal file
65
home-assistant/tests/components/pyload/test_init.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Test pyLoad init."""
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
|
||||
import pytest
|
||||
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_entry_setup_unload(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: MagicMock,
|
||||
) -> None:
|
||||
"""Test integration setup and unload."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect"),
|
||||
[CannotConnect, ParserError],
|
||||
)
|
||||
async def test_config_entry_setup_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: MagicMock,
|
||||
side_effect: Exception,
|
||||
) -> None:
|
||||
"""Test config entry not ready."""
|
||||
mock_pyloadapi.login.side_effect = side_effect
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_config_entry_setup_invalid_auth(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: MagicMock,
|
||||
) -> None:
|
||||
"""Test config entry authentication."""
|
||||
mock_pyloadapi.login.side_effect = InvalidAuth
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
assert any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH}))
|
||||
181
home-assistant/tests/components/pyload/test_sensor.py
Normal file
181
home-assistant/tests/components/pyload/test_sensor.py
Normal file
@@ -0,0 +1,181 @@
|
||||
"""Tests for the pyLoad Sensors."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.pyload.const import DOMAIN
|
||||
from homeassistant.components.pyload.coordinator import SCAN_INTERVAL
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er, issue_registry as ir
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def sensor_only() -> Generator[None]:
|
||||
"""Enable only the sensor platform."""
|
||||
with patch(
|
||||
"homeassistant.components.pyload.PLATFORMS",
|
||||
[Platform.SENSOR],
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_setup(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setup of the pyload sensor platform."""
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"exception",
|
||||
[CannotConnect, InvalidAuth, ParserError],
|
||||
)
|
||||
async def test_sensor_update_exceptions(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
exception: Exception,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test if pyLoad sensors go unavailable when exceptions occur (except ParserErrors)."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_pyloadapi.get_status.side_effect = exception
|
||||
freezer.tick(SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_sensor_invalid_auth(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test invalid auth during sensor update."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_pyloadapi.get_status.side_effect = InvalidAuth
|
||||
mock_pyloadapi.login.side_effect = InvalidAuth
|
||||
|
||||
freezer.tick(SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
"Authentication failed for username, verify your login credentials"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_platform_setup_triggers_import_flow(
|
||||
hass: HomeAssistant,
|
||||
pyload_config: ConfigType,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test if an issue is created when attempting setup from yaml config."""
|
||||
|
||||
assert await async_setup_component(hass, SENSOR_DOMAIN, pyload_config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "reason"),
|
||||
[
|
||||
(InvalidAuth, "invalid_auth"),
|
||||
(CannotConnect, "cannot_connect"),
|
||||
(ParserError, "cannot_connect"),
|
||||
(ValueError, "unknown"),
|
||||
],
|
||||
)
|
||||
async def test_deprecated_yaml_import_issue(
|
||||
hass: HomeAssistant,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
pyload_config: ConfigType,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
exception: Exception,
|
||||
reason: str,
|
||||
) -> None:
|
||||
"""Test an issue is created when attempting setup from yaml config and an error happens."""
|
||||
|
||||
mock_pyloadapi.login.side_effect = exception
|
||||
await async_setup_component(hass, SENSOR_DOMAIN, pyload_config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(
|
||||
domain=DOMAIN, issue_id=f"deprecated_yaml_import_issue_{reason}"
|
||||
)
|
||||
|
||||
|
||||
async def test_deprecated_yaml(
|
||||
hass: HomeAssistant,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
pyload_config: ConfigType,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test an issue is created when we import from yaml config."""
|
||||
|
||||
await async_setup_component(hass, SENSOR_DOMAIN, pyload_config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(
|
||||
domain=HOMEASSISTANT_DOMAIN, issue_id=f"deprecated_yaml_{DOMAIN}"
|
||||
)
|
||||
|
||||
|
||||
async def test_pyload_pre_0_5_0(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setup of the pyload sensor platform."""
|
||||
mock_pyloadapi.get_status.return_value = {
|
||||
"pause": False,
|
||||
"active": 1,
|
||||
"queue": 6,
|
||||
"total": 37,
|
||||
"speed": 5405963.0,
|
||||
"download": True,
|
||||
"reconnect": False,
|
||||
}
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
152
home-assistant/tests/components/pyload/test_switch.py
Normal file
152
home-assistant/tests/components/pyload/test_switch.py
Normal file
@@ -0,0 +1,152 @@
|
||||
"""Tests for the pyLoad Switches."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, call, patch
|
||||
|
||||
from pyloadapi import CannotConnect, InvalidAuth
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.pyload.switch import PyLoadSwitch
|
||||
from homeassistant.components.switch import (
|
||||
DOMAIN as SWITCH_DOMAIN,
|
||||
SERVICE_TOGGLE,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
# Maps entity to the mock calls to assert
|
||||
API_CALL = {
|
||||
PyLoadSwitch.PAUSE_RESUME_QUEUE: {
|
||||
SERVICE_TURN_ON: call.unpause,
|
||||
SERVICE_TURN_OFF: call.pause,
|
||||
SERVICE_TOGGLE: call.toggle_pause,
|
||||
},
|
||||
PyLoadSwitch.RECONNECT: {
|
||||
SERVICE_TURN_ON: call.toggle_reconnect,
|
||||
SERVICE_TURN_OFF: call.toggle_reconnect,
|
||||
SERVICE_TOGGLE: call.toggle_reconnect,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def switch_only() -> Generator[None]:
|
||||
"""Enable only the switch platform."""
|
||||
with patch(
|
||||
"homeassistant.components.pyload.PLATFORMS",
|
||||
[Platform.SWITCH],
|
||||
):
|
||||
yield
|
||||
|
||||
|
||||
async def test_state(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
) -> None:
|
||||
"""Test switch state."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service_call"),
|
||||
[
|
||||
SERVICE_TURN_ON,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TOGGLE,
|
||||
],
|
||||
)
|
||||
async def test_turn_on_off(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
service_call: str,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test switch turn on/off, toggle method."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
)
|
||||
|
||||
for entity_entry in entity_entries:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
service_call,
|
||||
{ATTR_ENTITY_ID: entity_entry.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
assert (
|
||||
API_CALL[entity_entry.translation_key][service_call]
|
||||
in mock_pyloadapi.method_calls
|
||||
)
|
||||
mock_pyloadapi.reset_mock()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("service_call"),
|
||||
[
|
||||
SERVICE_TURN_ON,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TOGGLE,
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect"),
|
||||
[CannotConnect, InvalidAuth],
|
||||
)
|
||||
async def test_turn_on_off_errors(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
mock_pyloadapi: AsyncMock,
|
||||
service_call: str,
|
||||
entity_registry: er.EntityRegistry,
|
||||
side_effect: Exception,
|
||||
) -> None:
|
||||
"""Test switch turn on/off, toggle method."""
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
)
|
||||
mock_pyloadapi.unpause.side_effect = side_effect
|
||||
mock_pyloadapi.pause.side_effect = side_effect
|
||||
mock_pyloadapi.toggle_pause.side_effect = side_effect
|
||||
mock_pyloadapi.toggle_reconnect.side_effect = side_effect
|
||||
|
||||
for entity_entry in entity_entries:
|
||||
with pytest.raises(ServiceValidationError):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
service_call,
|
||||
{ATTR_ENTITY_ID: entity_entry.entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
Reference in New Issue
Block a user