mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 08:26:41 +01:00
Refactor Huum test fixtures (#166115)
This commit is contained in:
@@ -39,10 +39,7 @@ rules:
|
||||
log-when-unavailable: done
|
||||
parallel-updates: done
|
||||
reauthentication-flow: done
|
||||
test-coverage:
|
||||
status: todo
|
||||
comment: |
|
||||
PLANNED: Use freezer-based time advancement instead of directly calling async_refresh().
|
||||
test-coverage: todo
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
|
||||
@@ -1,18 +1 @@
|
||||
"""Tests for the huum integration."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_with_selected_platforms(
|
||||
hass: HomeAssistant, entry: MockConfigEntry, platforms: list[Platform]
|
||||
) -> None:
|
||||
"""Set up the Huum integration with the selected platforms."""
|
||||
entry.add_to_hass(hass)
|
||||
with patch("homeassistant.components.huum.PLATFORMS", platforms):
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -1,93 +1,54 @@
|
||||
"""Configuration for Huum tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from huum.const import SaunaStatus
|
||||
from huum.schemas import HuumStatusResponse, SaunaConfig
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.huum.const import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_huum() -> Generator[AsyncMock]:
|
||||
"""Mock data from the API."""
|
||||
huum = AsyncMock()
|
||||
|
||||
def mock_huum_client() -> Generator[AsyncMock]:
|
||||
"""Mock the Huum API client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.huum.config_flow.Huum.status",
|
||||
return_value=huum,
|
||||
"homeassistant.components.huum.coordinator.Huum",
|
||||
autospec=True,
|
||||
) as mock_cls,
|
||||
patch(
|
||||
"homeassistant.components.huum.config_flow.Huum",
|
||||
new=mock_cls,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.huum.coordinator.Huum.status",
|
||||
return_value=huum,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.huum.coordinator.Huum.turn_on",
|
||||
return_value=huum,
|
||||
) as turn_on,
|
||||
patch(
|
||||
"homeassistant.components.huum.coordinator.Huum.toggle_light",
|
||||
return_value=huum,
|
||||
) as toggle_light,
|
||||
):
|
||||
huum.status = SaunaStatus.ONLINE_NOT_HEATING
|
||||
huum.config = 3
|
||||
huum.door_closed = True
|
||||
huum.temperature = 30
|
||||
huum.sauna_name = "Home sauna"
|
||||
huum.target_temperature = 80
|
||||
huum.payment_end_date = "2026-12-31"
|
||||
huum.light = 1
|
||||
huum.humidity = 0
|
||||
huum.target_humidity = 5
|
||||
huum.sauna_config.child_lock = "OFF"
|
||||
huum.sauna_config.max_heating_time = 3
|
||||
huum.sauna_config.min_heating_time = 0
|
||||
huum.sauna_config.max_temp = 110
|
||||
huum.sauna_config.min_temp = 40
|
||||
huum.sauna_config.max_timer = 0
|
||||
huum.sauna_config.min_timer = 0
|
||||
|
||||
def _to_dict() -> dict[str, object]:
|
||||
return {
|
||||
"status": huum.status,
|
||||
"config": huum.config,
|
||||
"door_closed": huum.door_closed,
|
||||
"temperature": huum.temperature,
|
||||
"sauna_name": huum.sauna_name,
|
||||
"target_temperature": huum.target_temperature,
|
||||
"start_date": None,
|
||||
"end_date": None,
|
||||
"duration": None,
|
||||
"steamer_error": None,
|
||||
"payment_end_date": huum.payment_end_date,
|
||||
"is_private": None,
|
||||
"show_modal": None,
|
||||
"light": huum.light,
|
||||
"humidity": huum.humidity,
|
||||
"target_humidity": huum.target_humidity,
|
||||
"remote_safety_state": None,
|
||||
"sauna_config": {
|
||||
"child_lock": huum.sauna_config.child_lock,
|
||||
"max_heating_time": huum.sauna_config.max_heating_time,
|
||||
"min_heating_time": huum.sauna_config.min_heating_time,
|
||||
"max_temp": huum.sauna_config.max_temp,
|
||||
"min_temp": huum.sauna_config.min_temp,
|
||||
"max_timer": huum.sauna_config.max_timer,
|
||||
"min_timer": huum.sauna_config.min_timer,
|
||||
},
|
||||
}
|
||||
|
||||
huum.to_dict = Mock(side_effect=_to_dict)
|
||||
huum.turn_on = turn_on
|
||||
huum.toggle_light = toggle_light
|
||||
|
||||
yield huum
|
||||
client = mock_cls.return_value
|
||||
client.status.return_value = HuumStatusResponse(
|
||||
status=SaunaStatus.ONLINE_NOT_HEATING,
|
||||
door_closed=True,
|
||||
temperature=30,
|
||||
sauna_name="123456",
|
||||
target_temperature=80,
|
||||
config=3,
|
||||
light=1,
|
||||
humidity=0,
|
||||
target_humidity=5,
|
||||
sauna_config=SaunaConfig(
|
||||
child_lock="OFF",
|
||||
max_heating_time=3,
|
||||
min_heating_time=0,
|
||||
max_temp=110,
|
||||
min_temp=40,
|
||||
max_timer=0,
|
||||
min_timer=0,
|
||||
),
|
||||
)
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -110,3 +71,32 @@ def mock_config_entry() -> MockConfigEntry:
|
||||
},
|
||||
entry_id="AABBCC112233",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.CLIMATE,
|
||||
Platform.LIGHT,
|
||||
Platform.NUMBER,
|
||||
Platform.SENSOR,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def init_integration(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
platforms: list[Platform],
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Huum integration for testing."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with patch("homeassistant.components.huum.PLATFORMS", platforms):
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return mock_config_entry
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
'humidity': 0,
|
||||
'is_private': None,
|
||||
'light': 1,
|
||||
'payment_end_date': '**REDACTED**',
|
||||
'payment_end_date': None,
|
||||
'remote_safety_state': None,
|
||||
'sauna_config': dict({
|
||||
'child_lock': 'OFF',
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
"""Tests for the Huum climate entity."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
"""Tests for the Huum binary sensor entity."""
|
||||
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
ENTITY_ID = "binary_sensor.huum_sauna_door"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.BINARY_SENSOR]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_binary_sensor(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the initial parameters."""
|
||||
await setup_with_selected_platforms(
|
||||
hass, mock_config_entry, [Platform.BINARY_SENSOR]
|
||||
)
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
"""Tests for the Huum climate entity."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from huum.const import SaunaStatus
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.climate import (
|
||||
@@ -20,34 +23,35 @@ from homeassistant.const import ATTR_ENTITY_ID, ATTR_TEMPERATURE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
ENTITY_ID = "climate.huum_sauna"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.CLIMATE]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_climate_entity(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the initial parameters."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_set_hvac_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setting HVAC mode."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
mock_huum.status = SaunaStatus.ONLINE_HEATING
|
||||
mock_huum_client.status.return_value.status = SaunaStatus.ONLINE_HEATING
|
||||
await hass.services.async_call(
|
||||
CLIMATE_DOMAIN,
|
||||
SERVICE_SET_HVAC_MODE,
|
||||
@@ -58,18 +62,16 @@ async def test_set_hvac_mode(
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == HVACMode.HEAT
|
||||
|
||||
mock_huum.turn_on.assert_called_once()
|
||||
mock_huum_client.turn_on.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_set_temperature(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setting the temperature."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
mock_huum.status = SaunaStatus.ONLINE_HEATING
|
||||
mock_huum_client.status.return_value.status = SaunaStatus.ONLINE_HEATING
|
||||
await hass.services.async_call(
|
||||
CLIMATE_DOMAIN,
|
||||
SERVICE_SET_TEMPERATURE,
|
||||
@@ -80,39 +82,40 @@ async def test_set_temperature(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_huum.turn_on.assert_called_once_with(60)
|
||||
mock_huum_client.turn_on.assert_awaited_once_with(60)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_temperature_range(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test the temperature range."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
# API response.
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.attributes["min_temp"] == 40
|
||||
assert state.attributes["max_temp"] == 110
|
||||
|
||||
# Empty/unconfigured API response should return default values.
|
||||
mock_huum.sauna_config.min_temp = 0
|
||||
mock_huum.sauna_config.max_temp = 0
|
||||
mock_huum_client.status.return_value.sauna_config.min_temp = 0
|
||||
mock_huum_client.status.return_value.sauna_config.max_temp = 0
|
||||
|
||||
await mock_config_entry.runtime_data.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(timedelta(seconds=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.attributes["min_temp"] == CONFIG_DEFAULT_MIN_TEMP
|
||||
assert state.attributes["max_temp"] == CONFIG_DEFAULT_MAX_TEMP
|
||||
|
||||
# Custom configured API response.
|
||||
mock_huum.sauna_config.min_temp = 50
|
||||
mock_huum.sauna_config.max_temp = 80
|
||||
mock_huum_client.status.return_value.sauna_config.min_temp = 50
|
||||
mock_huum_client.status.return_value.sauna_config.max_temp = 80
|
||||
|
||||
await mock_config_entry.runtime_data.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
freezer.tick(timedelta(seconds=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.attributes["min_temp"] == 50
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Test the huum config flow."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from huum.exceptions import Forbidden
|
||||
import pytest
|
||||
@@ -17,9 +17,8 @@ TEST_USERNAME = "huum@sauna.org"
|
||||
TEST_PASSWORD = "ukuuku"
|
||||
|
||||
|
||||
async def test_form(
|
||||
hass: HomeAssistant, mock_huum: AsyncMock, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
@pytest.mark.usefixtures("mock_huum_client")
|
||||
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
@@ -46,9 +45,9 @@ async def test_form(
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_huum_client")
|
||||
async def test_signup_flow_already_set_up(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
@@ -82,7 +81,7 @@ async def test_signup_flow_already_set_up(
|
||||
)
|
||||
async def test_huum_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_huum_client: AsyncMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
raises: Exception,
|
||||
error_base: str,
|
||||
@@ -92,21 +91,19 @@ async def test_huum_errors(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.huum.config_flow.Huum.status",
|
||||
side_effect=raises,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
},
|
||||
)
|
||||
mock_huum_client.status.side_effect = raises
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: TEST_USERNAME,
|
||||
CONF_PASSWORD: TEST_PASSWORD,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error_base}
|
||||
|
||||
mock_huum_client.status.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
@@ -117,9 +114,9 @@ async def test_huum_errors(
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_huum_client")
|
||||
async def test_reauth_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test reauthentication flow succeeds with valid credentials."""
|
||||
@@ -155,7 +152,7 @@ async def test_reauth_flow(
|
||||
)
|
||||
async def test_reauth_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_huum_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
raises: Exception,
|
||||
error_base: str,
|
||||
@@ -165,19 +162,17 @@ async def test_reauth_errors(
|
||||
|
||||
result = await mock_config_entry.start_reauth_flow(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.huum.config_flow.Huum.status",
|
||||
side_effect=raises,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "wrong_password"},
|
||||
)
|
||||
mock_huum_client.status.side_effect = raises
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "wrong_password"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error_base}
|
||||
|
||||
# Recover with valid credentials
|
||||
mock_huum_client.status.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "new_password"},
|
||||
|
||||
@@ -2,30 +2,31 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.CLIMATE]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test diagnostics."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, mock_config_entry
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Tests for the Huum __init__."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from huum.exceptions import Forbidden, NotAuthenticated
|
||||
import pytest
|
||||
@@ -8,20 +8,16 @@ import pytest
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.huum.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_loading_and_unloading_config_entry(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_huum: AsyncMock
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test loading and unloading a config entry."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
@@ -35,17 +31,15 @@ async def test_loading_and_unloading_config_entry(
|
||||
async def test_auth_error_triggers_reauth(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
side_effect: type[Exception],
|
||||
) -> None:
|
||||
"""Test that an auth error during coordinator refresh triggers reauth."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
mock_huum_client.status.side_effect = side_effect
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.huum.coordinator.Huum.status",
|
||||
side_effect=side_effect,
|
||||
):
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
|
||||
assert any(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Tests for the Huum light entity."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
@@ -16,33 +17,34 @@ from homeassistant.const import (
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
ENTITY_ID = "light.huum_sauna_light"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.LIGHT]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_light(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the initial parameters."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_light_turn_off(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test turning off light."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_ON
|
||||
|
||||
@@ -52,18 +54,21 @@ async def test_light_turn_off(
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
mock_huum.toggle_light.assert_called_once()
|
||||
mock_huum_client.toggle_light.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_light_turn_on(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_huum_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test turning on light."""
|
||||
mock_huum.light = 0
|
||||
mock_huum_client.status.return_value.light = 0
|
||||
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
with patch("homeassistant.components.huum.PLATFORMS", [Platform.LIGHT]):
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OFF
|
||||
@@ -74,4 +79,4 @@ async def test_light_turn_on(
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
mock_huum.toggle_light.assert_called_once()
|
||||
mock_huum_client.toggle_light.assert_awaited_once()
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from huum.const import SaunaStatus
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.number import (
|
||||
@@ -14,34 +15,35 @@ from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
ENTITY_ID = "number.huum_sauna_humidity"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.NUMBER]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_number_entity(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the initial parameters."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.NUMBER])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_set_humidity(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setting the humidity."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.NUMBER])
|
||||
|
||||
mock_huum.status = SaunaStatus.ONLINE_HEATING
|
||||
mock_huum_client.status.return_value.status = SaunaStatus.ONLINE_HEATING
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
@@ -52,18 +54,16 @@ async def test_set_humidity(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_huum.turn_on.assert_called_once_with(temperature=80, humidity=5)
|
||||
mock_huum_client.turn_on.assert_awaited_once_with(temperature=80, humidity=5)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_dont_set_humidity_when_sauna_not_heating(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_huum_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test setting the humidity."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.NUMBER])
|
||||
|
||||
mock_huum.status = SaunaStatus.ONLINE_NOT_HEATING
|
||||
mock_huum_client.status.return_value.status = SaunaStatus.ONLINE_NOT_HEATING
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
@@ -74,4 +74,4 @@ async def test_dont_set_humidity_when_sauna_not_heating(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_huum.turn_on.assert_not_called()
|
||||
mock_huum_client.turn_on.assert_not_called()
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
"""Tests for the Huum sensor entity."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[Platform]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.SENSOR]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("init_integration")
|
||||
async def test_sensor(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the temperature sensor."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.SENSOR])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
Reference in New Issue
Block a user