From c7530937410c925e7bebe9eedc36fac6fc75c6fa Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 20 Apr 2024 19:10:56 +0200 Subject: [PATCH] Use snapshot test helper in AO Smith (#115890) --- .../aosmith/snapshots/test_sensor.ambr | 81 +++++++++++- .../aosmith/snapshots/test_water_heater.ambr | 121 ++++++++++++++---- tests/components/aosmith/test_sensor.py | 44 ++----- tests/components/aosmith/test_water_heater.py | 53 +++----- 4 files changed, 207 insertions(+), 92 deletions(-) diff --git a/tests/components/aosmith/snapshots/test_sensor.ambr b/tests/components/aosmith/snapshots/test_sensor.ambr index 150e0c2934f..7aae9713037 100644 --- a/tests/components/aosmith/snapshots/test_sensor.ambr +++ b/tests/components/aosmith/snapshots/test_sensor.ambr @@ -1,5 +1,43 @@ # 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': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + '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': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + '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': , + }) +# --- +# name: test_state[sensor.my_water_heater_energy_usage-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'energy', @@ -15,7 +53,46 @@ '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': , + 'device_class': None, + 'device_id': , + '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': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + '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({ 'attributes': ReadOnlyDict({ 'device_class': 'enum', diff --git a/tests/components/aosmith/snapshots/test_water_heater.ambr b/tests/components/aosmith/snapshots/test_water_heater.ambr index c3740341c17..deb079570f1 100644 --- a/tests/components/aosmith/snapshots/test_water_heater.ambr +++ b/tests/components/aosmith/snapshots/test_water_heater.ambr @@ -1,5 +1,103 @@ # 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': , + 'device_class': None, + 'device_id': , + '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': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'aosmith', + 'previous_unique_id': None, + 'supported_features': , + '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': , + 'target_temp_high': None, + 'target_temp_low': None, + 'temperature': 130, + }), + 'context': , + 'entity_id': 'water_heater.my_water_heater', + 'last_changed': , + 'last_reported': , + 'last_updated': , + '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': , + 'device_class': None, + 'device_id': , + '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': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'aosmith', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'junctionId', + 'unit_of_measurement': None, + }) +# --- +# name: test_state[True][water_heater.my_water_heater-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'away_mode': 'off', @@ -26,24 +124,3 @@ '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': , - 'target_temp_high': None, - 'target_temp_low': None, - 'temperature': 130, - }), - 'context': , - 'entity_id': 'water_heater.my_water_heater', - 'last_changed': , - 'last_reported': , - 'last_updated': , - 'state': 'electric', - }) -# --- diff --git a/tests/components/aosmith/test_sensor.py b/tests/components/aosmith/test_sensor.py index f94dfdb710c..d6acd8865d8 100644 --- a/tests/components/aosmith/test_sensor.py +++ b/tests/components/aosmith/test_sensor.py @@ -1,50 +1,30 @@ """Tests for the sensor platform of the A. O. Smith integration.""" +from collections.abc import AsyncGenerator +from unittest.mock import patch + 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 tests.common import MockConfigEntry +from tests.common import MockConfigEntry, snapshot_platform -@pytest.mark.parametrize( - ("entity_id", "unique_id"), - [ - ( - "sensor.my_water_heater_hot_water_availability", - "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.fixture(autouse=True) +async def platforms() -> AsyncGenerator[list[str], None]: + """Return the platforms to be loaded for this test.""" + with patch("homeassistant.components.aosmith.PLATFORMS", [Platform.SENSOR]): + yield -@pytest.mark.parametrize( - ("entity_id"), - [ - "sensor.my_water_heater_hot_water_availability", - "sensor.my_water_heater_energy_usage", - ], -) async def test_state( hass: HomeAssistant, init_integration: MockConfigEntry, snapshot: SnapshotAssertion, - entity_id: str, + entity_registry: er.EntityRegistry, ) -> None: """Test the state of the sensor entities.""" - state = hass.states.get(entity_id) - assert state == snapshot + await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id) diff --git a/tests/components/aosmith/test_water_heater.py b/tests/components/aosmith/test_water_heater.py index a256f720c0a..567121ac0b0 100644 --- a/tests/components/aosmith/test_water_heater.py +++ b/tests/components/aosmith/test_water_heater.py @@ -1,6 +1,7 @@ """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 import pytest @@ -19,53 +20,33 @@ from homeassistant.components.water_heater import ( STATE_HEAT_PUMP, WaterHeaterEntityFeature, ) -from homeassistant.const import ( - ATTR_ENTITY_ID, - ATTR_FRIENDLY_NAME, - ATTR_SUPPORTED_FEATURES, -) +from homeassistant.const import ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, snapshot_platform -async def test_setup( - hass: HomeAssistant, - entity_registry: er.EntityRegistry, - init_integration: MockConfigEntry, -) -> None: - """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.fixture(autouse=True) +async def platforms() -> AsyncGenerator[list[str], None]: + """Return the platforms to be loaded for this test.""" + with patch("homeassistant.components.aosmith.PLATFORMS", [Platform.WATER_HEATER]): + yield @pytest.mark.parametrize( ("get_devices_fixture_heat_pump"), - [ - False, - ], + [False, True], ) -async def test_state_non_heat_pump( - hass: HomeAssistant, init_integration: MockConfigEntry, snapshot: SnapshotAssertion +async def test_state( + hass: HomeAssistant, + init_integration: MockConfigEntry, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, ) -> None: - """Test the state of the water heater entity for a non heat pump device.""" - state = hass.states.get("water_heater.my_water_heater") - assert state == snapshot + """Test the state of the water heater entities.""" + await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id) @pytest.mark.parametrize(