Use SnapshotAssertion in gree climate tests (#90339)

This commit is contained in:
epenet 2023-03-27 10:01:39 +02:00 committed by GitHub
parent 164482dc08
commit 8c519e1abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 140 additions and 22 deletions

View File

@ -6,6 +6,7 @@ from unittest.mock import AsyncMock, Mock
from greeclimate.discovery import Listener from greeclimate.discovery import Listener
from homeassistant.components.gree.const import DISCOVERY_TIMEOUT, DOMAIN as GREE_DOMAIN from homeassistant.components.gree.const import DISCOVERY_TIMEOUT, DOMAIN as GREE_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -90,8 +91,10 @@ def build_device_mock(name="fake-device-1", ipAddress="1.1.1.1", mac="aabbcc1122
return mock return mock
async def async_setup_gree(hass): async def async_setup_gree(hass: HomeAssistant) -> MockConfigEntry:
"""Set up the gree platform.""" """Set up the gree platform."""
MockConfigEntry(domain=GREE_DOMAIN).add_to_hass(hass) entry = MockConfigEntry(domain=GREE_DOMAIN)
entry.add_to_hass(hass)
await async_setup_component(hass, GREE_DOMAIN, {GREE_DOMAIN: {"climate": {}}}) await async_setup_component(hass, GREE_DOMAIN, {GREE_DOMAIN: {"climate": {}}})
await hass.async_block_till_done() await hass.async_block_till_done()
return entry

View File

@ -0,0 +1,118 @@
# serializer version: 1
# name: test_entity_states
list([
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 25,
'fan_mode': 'auto',
'fan_modes': list([
'auto',
'low',
'medium low',
'medium',
'medium high',
'high',
]),
'friendly_name': 'fake-device-1',
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
<HVACMode.FAN_ONLY: 'fan_only'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': 30,
'min_temp': 8,
'preset_mode': 'none',
'preset_modes': list([
'eco',
'away',
'boost',
'none',
'sleep',
]),
'supported_features': <ClimateEntityFeature: 57>,
'swing_mode': 'off',
'swing_modes': list([
'off',
'vertical',
'horizontal',
'both',
]),
'target_temp_step': 1,
'temperature': 25,
}),
'context': <ANY>,
'entity_id': 'climate.fake_device_1',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
}),
])
# ---
# name: test_registry_settings
list([
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'fan_modes': list([
'auto',
'low',
'medium low',
'medium',
'medium high',
'high',
]),
'hvac_modes': list([
<HVACMode.AUTO: 'auto'>,
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
<HVACMode.FAN_ONLY: 'fan_only'>,
<HVACMode.HEAT: 'heat'>,
<HVACMode.OFF: 'off'>,
]),
'max_temp': 30,
'min_temp': 8,
'preset_modes': list([
'eco',
'away',
'boost',
'none',
'sleep',
]),
'swing_modes': list([
'off',
'vertical',
'horizontal',
'both',
]),
'target_temp_step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.fake_device_1',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'fake-device-1',
'platform': 'gree',
'supported_features': <ClimateEntityFeature: 57>,
'translation_key': None,
'unique_id': 'aabbcc112233',
'unit_of_measurement': None,
}),
])
# ---

View File

@ -5,6 +5,7 @@ from unittest.mock import DEFAULT as DEFAULT_MOCK, AsyncMock, patch
from greeclimate.device import HorizontalSwing, VerticalSwing from greeclimate.device import HorizontalSwing, VerticalSwing
from greeclimate.exceptions import DeviceNotBoundError, DeviceTimeoutError from greeclimate.exceptions import DeviceNotBoundError, DeviceTimeoutError
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.climate import ( from homeassistant.components.climate import (
ATTR_CURRENT_TEMPERATURE, ATTR_CURRENT_TEMPERATURE,
@ -31,15 +32,12 @@ from homeassistant.components.climate import (
SWING_HORIZONTAL, SWING_HORIZONTAL,
SWING_OFF, SWING_OFF,
SWING_VERTICAL, SWING_VERTICAL,
ClimateEntityFeature,
HVACMode, HVACMode,
) )
from homeassistant.components.gree.climate import FAN_MODES_REVERSE, HVAC_MODES_REVERSE from homeassistant.components.gree.climate import FAN_MODES_REVERSE, HVAC_MODES_REVERSE
from homeassistant.components.gree.const import FAN_MEDIUM_HIGH, FAN_MEDIUM_LOW from homeassistant.components.gree.const import FAN_MEDIUM_HIGH, FAN_MEDIUM_LOW
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_SUPPORTED_FEATURES,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
SERVICE_TURN_ON, SERVICE_TURN_ON,
@ -47,6 +45,7 @@ from homeassistant.const import (
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .common import async_setup_gree, build_device_mock from .common import async_setup_gree, build_device_mock
@ -797,22 +796,20 @@ async def test_update_swing_mode(
assert state.attributes.get(ATTR_SWING_MODE) == swing_mode assert state.attributes.get(ATTR_SWING_MODE) == swing_mode
async def test_name(hass: HomeAssistant, discovery, device) -> None: @patch("homeassistant.components.gree.PLATFORMS", [DOMAIN])
"""Test for name property.""" async def test_registry_settings(
await async_setup_gree(hass) hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
state = hass.states.get(ENTITY_ID)
assert state.attributes[ATTR_FRIENDLY_NAME] == "fake-device-1"
async def test_supported_features_with_turnon(
hass: HomeAssistant, discovery, device
) -> None: ) -> None:
"""Test for supported_features property.""" """Test for entity registry settings (unique_id)."""
entry = await async_setup_gree(hass)
entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
assert entries == snapshot
@patch("homeassistant.components.gree.PLATFORMS", [DOMAIN])
async def test_entity_states(hass: HomeAssistant, snapshot: SnapshotAssertion) -> None:
"""Test for entity registry settings (unique_id)."""
await async_setup_gree(hass) await async_setup_gree(hass)
state = hass.states.get(ENTITY_ID) states = hass.states.async_all()
assert state.attributes[ATTR_SUPPORTED_FEATURES] == ( assert states == snapshot
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.PRESET_MODE
| ClimateEntityFeature.SWING_MODE
)