1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-28 22:06:25 +00:00
Files
core/tests/components/watts/test_climate.py
theobld-ww 0c1af1d613 Add switch entities to Watts Vision + (#162699)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2026-02-11 17:39:17 +01:00

238 lines
6.8 KiB
Python

"""Tests for the Watts Vision climate platform."""
from datetime import timedelta
from unittest.mock import AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from visionpluspython.models import ThermostatMode
from homeassistant.components.climate import (
ATTR_HVAC_MODE,
ATTR_TEMPERATURE,
DOMAIN as CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
SERVICE_SET_TEMPERATURE,
HVACMode,
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the climate entities."""
with patch("homeassistant.components.watts.PLATFORMS", [Platform.CLIMATE]):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_set_temperature(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting temperature."""
await setup_integration(hass, mock_config_entry)
state = hass.states.get("climate.living_room_thermostat")
assert state is not None
assert state.attributes.get(ATTR_TEMPERATURE) == 22.0
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_TEMPERATURE: 23.5,
},
blocking=True,
)
mock_watts_client.set_thermostat_temperature.assert_called_once_with(
"thermostat_123", 23.5
)
async def test_fast_polling(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test that setting temperature triggers fast polling and it stops after duration."""
await setup_integration(hass, mock_config_entry)
# Trigger fast polling
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_TEMPERATURE: 23.5,
},
blocking=True,
)
mock_watts_client.get_device.reset_mock()
# Fast polling should be active
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_watts_client.get_device.called
mock_watts_client.get_device.assert_called_with("thermostat_123", refresh=True)
# Should still be in fast polling after 55s
mock_watts_client.get_device.reset_mock()
freezer.tick(timedelta(seconds=50))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert mock_watts_client.get_device.called
mock_watts_client.get_device.reset_mock()
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
# Fast polling should be done now
mock_watts_client.get_device.reset_mock()
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert not mock_watts_client.get_device.called
async def test_set_hvac_mode_heat(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting HVAC mode to heat."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_HVAC_MODE: HVACMode.HEAT,
},
blocking=True,
)
mock_watts_client.set_thermostat_mode.assert_called_once_with(
"thermostat_123", ThermostatMode.COMFORT
)
async def test_set_hvac_mode_auto(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting HVAC mode to auto."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{
ATTR_ENTITY_ID: "climate.bedroom_thermostat",
ATTR_HVAC_MODE: HVACMode.AUTO,
},
blocking=True,
)
mock_watts_client.set_thermostat_mode.assert_called_once_with(
"thermostat_456", ThermostatMode.PROGRAM
)
async def test_set_hvac_mode_off(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting HVAC mode to off."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_HVAC_MODE: HVACMode.OFF,
},
blocking=True,
)
mock_watts_client.set_thermostat_mode.assert_called_once_with(
"thermostat_123", ThermostatMode.OFF
)
async def test_set_temperature_api_error(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test error handling when setting temperature fails."""
await setup_integration(hass, mock_config_entry)
# Make the API call fail
mock_watts_client.set_thermostat_temperature.side_effect = RuntimeError("API Error")
with pytest.raises(
HomeAssistantError, match="An error occurred while setting the temperature"
):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_TEMPERATURE: 23.5,
},
blocking=True,
)
async def test_set_hvac_mode_value_error(
hass: HomeAssistant,
mock_watts_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test error handling when setting mode fails."""
await setup_integration(hass, mock_config_entry)
mock_watts_client.set_thermostat_mode.side_effect = ValueError("Invalid mode")
with pytest.raises(
HomeAssistantError, match="An error occurred while setting the HVAC mode"
):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_HVAC_MODE,
{
ATTR_ENTITY_ID: "climate.living_room_thermostat",
ATTR_HVAC_MODE: HVACMode.HEAT,
},
blocking=True,
)