Skip to content

Commit

Permalink
refactor: Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liudger committed Sep 7, 2024
1 parent c6b9a3a commit 589d2bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 194 deletions.
154 changes: 4 additions & 150 deletions tests/components/bsblan/snapshots/test_climate.ambr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_celsius_fahrenheit[static.json-\xb0C][climate.bsb_lan-entry]
# name: test_celsius_fahrenheit[static.json][climate.bsb_lan-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand Down Expand Up @@ -44,7 +44,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_celsius_fahrenheit[static.json-\xb0C][climate.bsb_lan-state]
# name: test_celsius_fahrenheit[static.json][climate.bsb_lan-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 18.6,
Expand Down Expand Up @@ -72,7 +72,7 @@
'state': 'heat',
})
# ---
# name: test_celsius_fahrenheit[static_F.json-\xb0F][climate.bsb_lan-entry]
# name: test_celsius_fahrenheit[static_F.json][climate.bsb_lan-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand Down Expand Up @@ -117,7 +117,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_celsius_fahrenheit[static_F.json-\xb0F][climate.bsb_lan-state]
# name: test_celsius_fahrenheit[static_F.json][climate.bsb_lan-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': -7.4,
Expand Down Expand Up @@ -218,149 +218,3 @@
'state': 'heat',
})
# ---
# name: test_climate_entity_properties[static.json][climate.bsb_lan-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': 20.0,
'min_temp': 8.0,
'preset_modes': list([
'eco',
'none',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.bsb_lan',
'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': None,
'platform': 'bsblan',
'previous_unique_id': None,
'supported_features': <ClimateEntityFeature: 401>,
'translation_key': None,
'unique_id': '00:80:41:19:69:90-climate',
'unit_of_measurement': None,
})
# ---
# name: test_climate_entity_properties[static.json][climate.bsb_lan-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 18.6,
'friendly_name': 'BSB-LAN',
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': 20.0,
'min_temp': 8.0,
'preset_mode': 'none',
'preset_modes': list([
'eco',
'none',
]),
'supported_features': <ClimateEntityFeature: 401>,
'temperature': 18.5,
}),
'context': <ANY>,
'entity_id': 'climate.bsb_lan',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'heat',
})
# ---
# name: test_climate_entity_properties[static_f.json][climate.bsb_lan-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': -6.7,
'min_temp': -13.3,
'preset_modes': list([
'eco',
'none',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.bsb_lan',
'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': None,
'platform': 'bsblan',
'previous_unique_id': None,
'supported_features': <ClimateEntityFeature: 401>,
'translation_key': None,
'unique_id': '00:80:41:19:69:90-climate',
'unit_of_measurement': None,
})
# ---
# name: test_climate_entity_properties[static_f.json][climate.bsb_lan-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': -7.4,
'friendly_name': 'BSB-LAN',
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': -6.7,
'min_temp': -13.3,
'preset_mode': 'none',
'preset_modes': list([
'eco',
'none',
]),
'supported_features': <ClimateEntityFeature: 401>,
'temperature': -7.5,
}),
'context': <ANY>,
'entity_id': 'climate.bsb_lan',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'heat',
})
# ---
91 changes: 47 additions & 44 deletions tests/components/bsblan/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
SERVICE_SET_TEMPERATURE,
HVACMode,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_TEMPERATURE,
Platform,
UnitOfTemperature,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_TEMPERATURE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.entity_registry as er
Expand All @@ -44,10 +39,10 @@


@pytest.mark.parametrize(
("static_file", "temperature_unit"),
("static_file"),
[
("static.json", UnitOfTemperature.CELSIUS),
("static_F.json", UnitOfTemperature.FAHRENHEIT),
("static.json"),
("static_F.json"),
],
)
async def test_celsius_fahrenheit(
Expand All @@ -57,7 +52,6 @@ async def test_celsius_fahrenheit(
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
static_file: str,
temperature_unit: str,
) -> None:
"""Test Celsius and Fahrenheit temperature units."""
# Load static data from fixture
Expand Down Expand Up @@ -135,7 +129,6 @@ async def test_climate_entity_properties(
assert state.attributes["preset_mode"] == PRESET_ECO


@pytest.mark.parametrize("static_file", ["static.json"])
@pytest.mark.parametrize(
"mode",
[HVACMode.HEAT, HVACMode.AUTO, HVACMode.OFF],
Expand All @@ -144,15 +137,10 @@ async def test_async_set_hvac_mode(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
static_file: str,
mode: HVACMode,
) -> None:
"""Test setting HVAC mode via service call."""
static_data = json.loads(load_fixture(static_file, DOMAIN))
with patch.object(
mock_bsblan, "static_values", return_value=StaticState.from_dict(static_data)
):
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])

# Call the service to set HVAC mode
await hass.services.async_call(
Expand All @@ -168,22 +156,52 @@ async def test_async_set_hvac_mode(


@pytest.mark.parametrize(
("hvac_mode", "preset_mode", "expected_success"),
("hvac_mode", "preset_mode"),
[
(HVACMode.AUTO, PRESET_ECO, True),
(HVACMode.AUTO, PRESET_NONE, True),
(HVACMode.HEAT, PRESET_ECO, False),
(HVACMode.AUTO, PRESET_ECO),
(HVACMode.AUTO, PRESET_NONE),
],
)
async def test_async_set_preset_mode(
async def test_async_set_preset_mode_succes(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
hvac_mode: HVACMode,
preset_mode: str,
) -> None:
"""Test setting preset mode via service call."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])

# patch hvac_mode
mock_hvac_mode = MagicMock()
mock_hvac_mode.value = hvac_mode
mock_bsblan.state.return_value.hvac_mode = mock_hvac_mode

# Attempt to set the preset mode
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: preset_mode},
blocking=True,
)
await hass.async_block_till_done()


@pytest.mark.parametrize(
("hvac_mode", "preset_mode"),
[
(
HVACMode.HEAT,
PRESET_ECO,
)
],
)
async def test_async_set_preset_mode_error(
hass: HomeAssistant,
mock_bsblan: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
hvac_mode: HVACMode,
preset_mode: str,
expected_success: bool,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test setting preset mode via service call."""
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.CLIMATE])
Expand All @@ -194,26 +212,14 @@ async def test_async_set_preset_mode(
mock_bsblan.state.return_value.hvac_mode = mock_hvac_mode

# Attempt to set the preset mode
if not expected_success:
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: preset_mode},
blocking=True,
)
assert "set_preset_mode_error" in str(exc_info.value)
else:
ERROR_MSG = "Preset mode can only be set when HVAC mode is set to 'auto'"
with pytest.raises(HomeAssistantError, match=ERROR_MSG):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_PRESET_MODE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_PRESET_MODE: preset_mode},
blocking=True,
)
await hass.async_block_till_done()

# Reset the mock for the next iteration
mock_bsblan.thermostat.reset_mock()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -242,8 +248,6 @@ async def test_async_set_temperature(
# Assert that the thermostat method was called with the correct temperature
mock_bsblan.thermostat.assert_called_once_with(target_temperature=target_temp)

mock_bsblan.thermostat.reset_mock()


async def test_async_set_data(
hass: HomeAssistant,
Expand Down Expand Up @@ -300,12 +304,11 @@ async def test_async_set_data(

# Test error handling
mock_bsblan.thermostat.side_effect = BSBLANError("Test error")
with pytest.raises(HomeAssistantError) as exc_info:
ERROR_MSG = "An error occurred while updating the BSBLAN device"
with pytest.raises(HomeAssistantError, match=ERROR_MSG):
await hass.services.async_call(
CLIMATE_DOMAIN,
SERVICE_SET_TEMPERATURE,
{ATTR_ENTITY_ID: ENTITY_ID, ATTR_TEMPERATURE: 20},
blocking=True,
)
assert "An error occurred while updating the BSBLAN device" in str(exc_info.value)
assert exc_info.value.translation_key == "set_data_error"

0 comments on commit 589d2bb

Please sign in to comment.