1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00
Files
core/tests/components/heos/test_services.py
T
2025-01-20 22:18:46 +01:00

141 lines
4.4 KiB
Python

"""Tests for the services module."""
from pyheos import CommandAuthenticationError, Heos, HeosError
import pytest
from homeassistant.components.heos.const import (
ATTR_PASSWORD,
ATTR_USERNAME,
DOMAIN,
SERVICE_SIGN_IN,
SERVICE_SIGN_OUT,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from tests.common import MockConfigEntry
async def test_sign_in(
hass: HomeAssistant, config_entry: MockConfigEntry, controller: Heos
) -> None:
"""Test the sign-in service."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.services.async_call(
DOMAIN,
SERVICE_SIGN_IN,
{ATTR_USERNAME: "test@test.com", ATTR_PASSWORD: "password"},
blocking=True,
)
controller.sign_in.assert_called_once_with("test@test.com", "password")
async def test_sign_in_failed(
hass: HomeAssistant,
config_entry: MockConfigEntry,
controller: Heos,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test sign-in service logs error when not connected."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
controller.sign_in.side_effect = CommandAuthenticationError(
"", "Invalid credentials", 6
)
await hass.services.async_call(
DOMAIN,
SERVICE_SIGN_IN,
{ATTR_USERNAME: "test@test.com", ATTR_PASSWORD: "password"},
blocking=True,
)
controller.sign_in.assert_called_once_with("test@test.com", "password")
assert "Sign in failed: Invalid credentials (6)" in caplog.text
async def test_sign_in_unknown_error(
hass: HomeAssistant,
config_entry: MockConfigEntry,
controller: Heos,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test sign-in service logs error for failure."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
controller.sign_in.side_effect = HeosError()
await hass.services.async_call(
DOMAIN,
SERVICE_SIGN_IN,
{ATTR_USERNAME: "test@test.com", ATTR_PASSWORD: "password"},
blocking=True,
)
controller.sign_in.assert_called_once_with("test@test.com", "password")
assert "Unable to sign in" in caplog.text
@pytest.mark.usefixtures("controller")
async def test_sign_in_not_loaded_raises(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test the sign-in service when entry not loaded raises exception."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
assert await hass.config_entries.async_unload(config_entry.entry_id)
with pytest.raises(HomeAssistantError, match="The HEOS integration is not loaded"):
await hass.services.async_call(
DOMAIN,
SERVICE_SIGN_IN,
{ATTR_USERNAME: "test@test.com", ATTR_PASSWORD: "password"},
blocking=True,
)
async def test_sign_out(
hass: HomeAssistant, config_entry: MockConfigEntry, controller: Heos
) -> None:
"""Test the sign-out service."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.services.async_call(DOMAIN, SERVICE_SIGN_OUT, {}, blocking=True)
assert controller.sign_out.call_count == 1
async def test_sign_out_not_loaded_raises(
hass: HomeAssistant, config_entry: MockConfigEntry
) -> None:
"""Test the sign-out service when entry not loaded raises exception."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
assert await hass.config_entries.async_unload(config_entry.entry_id)
with pytest.raises(HomeAssistantError, match="The HEOS integration is not loaded"):
await hass.services.async_call(DOMAIN, SERVICE_SIGN_OUT, {}, blocking=True)
async def test_sign_out_unknown_error(
hass: HomeAssistant,
config_entry: MockConfigEntry,
controller: Heos,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the sign-out service."""
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
controller.sign_out.side_effect = HeosError()
await hass.services.async_call(DOMAIN, SERVICE_SIGN_OUT, {}, blocking=True)
assert controller.sign_out.call_count == 1
assert "Unable to sign out" in caplog.text