Use snapshot test helper in AO Smith (#115890)

This commit is contained in:
Joost Lekkerkerker 2024-04-20 19:10:56 +02:00 committed by GitHub
parent d478b87af7
commit c753093741
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 207 additions and 92 deletions

View file

@ -1,5 +1,43 @@
# serializer version: 1 # serializer version: 1
# name: test_state[sensor.my_water_heater_energy_usage] # name: test_state[sensor.my_water_heater_energy_usage-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.my_water_heater_energy_usage',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 1,
}),
}),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None,
'original_name': 'Energy usage',
'platform': 'aosmith',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'energy_usage',
'unique_id': 'energy_usage_junctionId',
'unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
})
# ---
# name: test_state[sensor.my_water_heater_energy_usage-state]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'device_class': 'energy', 'device_class': 'energy',
@ -15,7 +53,46 @@
'state': '132.825', 'state': '132.825',
}) })
# --- # ---
# name: test_state[sensor.my_water_heater_hot_water_availability] # name: test_state[sensor.my_water_heater_hot_water_availability-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'options': list([
'low',
'medium',
'high',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.my_water_heater_hot_water_availability',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Hot water availability',
'platform': 'aosmith',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'hot_water_availability',
'unique_id': 'hot_water_availability_junctionId',
'unit_of_measurement': None,
})
# ---
# name: test_state[sensor.my_water_heater_hot_water_availability-state]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'device_class': 'enum', 'device_class': 'enum',

View file

@ -1,5 +1,103 @@
# serializer version: 1 # serializer version: 1
# name: test_state # name: test_state[False][water_heater.my_water_heater-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max_temp': 130,
'min_temp': 95,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'water_heater',
'entity_category': None,
'entity_id': 'water_heater.my_water_heater',
'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': 'aosmith',
'previous_unique_id': None,
'supported_features': <WaterHeaterEntityFeature: 5>,
'translation_key': None,
'unique_id': 'junctionId',
'unit_of_measurement': None,
})
# ---
# name: test_state[False][water_heater.my_water_heater-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'away_mode': 'off',
'current_temperature': None,
'friendly_name': 'My water heater',
'max_temp': 130,
'min_temp': 95,
'supported_features': <WaterHeaterEntityFeature: 5>,
'target_temp_high': None,
'target_temp_low': None,
'temperature': 130,
}),
'context': <ANY>,
'entity_id': 'water_heater.my_water_heater',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'electric',
})
# ---
# name: test_state[True][water_heater.my_water_heater-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max_temp': 130,
'min_temp': 95,
'operation_list': list([
'electric',
'eco',
'heat_pump',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'water_heater',
'entity_category': None,
'entity_id': 'water_heater.my_water_heater',
'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': 'aosmith',
'previous_unique_id': None,
'supported_features': <WaterHeaterEntityFeature: 7>,
'translation_key': None,
'unique_id': 'junctionId',
'unit_of_measurement': None,
})
# ---
# name: test_state[True][water_heater.my_water_heater-state]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'away_mode': 'off', 'away_mode': 'off',
@ -26,24 +124,3 @@
'state': 'heat_pump', 'state': 'heat_pump',
}) })
# --- # ---
# name: test_state_non_heat_pump[False]
StateSnapshot({
'attributes': ReadOnlyDict({
'away_mode': 'off',
'current_temperature': None,
'friendly_name': 'My water heater',
'max_temp': 130,
'min_temp': 95,
'supported_features': <WaterHeaterEntityFeature: 5>,
'target_temp_high': None,
'target_temp_low': None,
'temperature': 130,
}),
'context': <ANY>,
'entity_id': 'water_heater.my_water_heater',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'electric',
})
# ---

View file

@ -1,50 +1,30 @@
"""Tests for the sensor platform of the A. O. Smith integration.""" """Tests for the sensor platform of the A. O. Smith integration."""
from collections.abc import AsyncGenerator
from unittest.mock import patch
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize( @pytest.fixture(autouse=True)
("entity_id", "unique_id"), async def platforms() -> AsyncGenerator[list[str], None]:
[ """Return the platforms to be loaded for this test."""
( with patch("homeassistant.components.aosmith.PLATFORMS", [Platform.SENSOR]):
"sensor.my_water_heater_hot_water_availability", yield
"hot_water_availability_junctionId",
),
("sensor.my_water_heater_energy_usage", "energy_usage_junctionId"),
],
)
async def test_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
init_integration: MockConfigEntry,
entity_id: str,
unique_id: str,
) -> None:
"""Test the setup of the sensor entities."""
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == unique_id
@pytest.mark.parametrize(
("entity_id"),
[
"sensor.my_water_heater_hot_water_availability",
"sensor.my_water_heater_energy_usage",
],
)
async def test_state( async def test_state(
hass: HomeAssistant, hass: HomeAssistant,
init_integration: MockConfigEntry, init_integration: MockConfigEntry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
entity_id: str, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test the state of the sensor entities.""" """Test the state of the sensor entities."""
state = hass.states.get(entity_id) await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id)
assert state == snapshot

View file

@ -1,6 +1,7 @@
"""Tests for the water heater platform of the A. O. Smith integration.""" """Tests for the water heater platform of the A. O. Smith integration."""
from unittest.mock import MagicMock from collections.abc import AsyncGenerator
from unittest.mock import MagicMock, patch
from py_aosmith.models import OperationMode from py_aosmith.models import OperationMode
import pytest import pytest
@ -19,53 +20,33 @@ from homeassistant.components.water_heater import (
STATE_HEAT_PUMP, STATE_HEAT_PUMP,
WaterHeaterEntityFeature, WaterHeaterEntityFeature,
) )
from homeassistant.const import ( from homeassistant.const import ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, Platform
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_SUPPORTED_FEATURES,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, snapshot_platform
async def test_setup( @pytest.fixture(autouse=True)
hass: HomeAssistant, async def platforms() -> AsyncGenerator[list[str], None]:
entity_registry: er.EntityRegistry, """Return the platforms to be loaded for this test."""
init_integration: MockConfigEntry, with patch("homeassistant.components.aosmith.PLATFORMS", [Platform.WATER_HEATER]):
) -> None: yield
"""Test the setup of the water heater entity."""
entry = entity_registry.async_get("water_heater.my_water_heater")
assert entry
assert entry.unique_id == "junctionId"
state = hass.states.get("water_heater.my_water_heater")
assert state
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My water heater"
async def test_state(
hass: HomeAssistant, init_integration: MockConfigEntry, snapshot: SnapshotAssertion
) -> None:
"""Test the state of the water heater entity."""
state = hass.states.get("water_heater.my_water_heater")
assert state == snapshot
@pytest.mark.parametrize( @pytest.mark.parametrize(
("get_devices_fixture_heat_pump"), ("get_devices_fixture_heat_pump"),
[ [False, True],
False,
],
) )
async def test_state_non_heat_pump( async def test_state(
hass: HomeAssistant, init_integration: MockConfigEntry, snapshot: SnapshotAssertion hass: HomeAssistant,
init_integration: MockConfigEntry,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test the state of the water heater entity for a non heat pump device.""" """Test the state of the water heater entities."""
state = hass.states.get("water_heater.my_water_heater") await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id)
assert state == snapshot
@pytest.mark.parametrize( @pytest.mark.parametrize(