Skip to content

Commit

Permalink
Use snapshots in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bieniu committed Jan 11, 2025
1 parent 16c6fdd commit aed07a4
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 26 deletions.
2 changes: 2 additions & 0 deletions tests/components/shelly/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ def mock_white_light_set_state(
"current_C": 15.2,
"target_C": 17.1,
"schedule_rev": 0,
"rssi": -60,
"battery": 100,
"errors": [],
},
}
Expand Down
48 changes: 48 additions & 0 deletions tests/components/shelly/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# serializer version: 1
# name: test_blu_trv_binary_sensor_entity[binary_sensor.trv_name_calibration-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.trv_name_calibration',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
'original_icon': None,
'original_name': 'TRV-Name calibration',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '123456789ABC-blutrv:200-calibration',
'unit_of_measurement': None,
})
# ---
# name: test_blu_trv_binary_sensor_entity[binary_sensor.trv_name_calibration-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'TRV-Name calibration',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.trv_name_calibration',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
113 changes: 113 additions & 0 deletions tests/components/shelly/snapshots/test_number.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# serializer version: 1
# name: test_blu_trv_number_entity[number.trv_name_external_temperature-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 50,
'min': -50,
'mode': <NumberMode.BOX: 'box'>,
'step': 0.1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.trv_name_external_temperature',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'TRV-Name external temperature',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'external_temperature',
'unique_id': '123456789ABC-blutrv:200-external_temperature',
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
})
# ---
# name: test_blu_trv_number_entity[number.trv_name_external_temperature-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'TRV-Name external temperature',
'max': 50,
'min': -50,
'mode': <NumberMode.BOX: 'box'>,
'step': 0.1,
'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>,
}),
'context': <ANY>,
'entity_id': 'number.trv_name_external_temperature',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '15.2',
})
# ---
# name: test_blu_trv_number_entity[number.trv_name_valve_position-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 100,
'min': 0,
'mode': <NumberMode.SLIDER: 'slider'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': None,
'entity_id': 'number.trv_name_valve_position',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'TRV-Name valve position',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'valve_position',
'unique_id': '123456789ABC-blutrv:200-valve_position',
'unit_of_measurement': '%',
})
# ---
# name: test_blu_trv_number_entity[number.trv_name_valve_position-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'TRV-Name valve position',
'max': 100,
'min': 0,
'mode': <NumberMode.SLIDER: 'slider'>,
'step': 1,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'number.trv_name_valve_position',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0',
})
# ---
153 changes: 153 additions & 0 deletions tests/components/shelly/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# serializer version: 1
# name: test_blu_trv_sensor_entity[sensor.trv_name_battery-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.trv_name_battery',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.BATTERY: 'battery'>,
'original_icon': None,
'original_name': 'TRV-Name battery',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '123456789ABC-blutrv:200-blutrv_battery',
'unit_of_measurement': '%',
})
# ---
# name: test_blu_trv_sensor_entity[sensor.trv_name_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery',
'friendly_name': 'TRV-Name battery',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.trv_name_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '100',
})
# ---
# name: test_blu_trv_sensor_entity[sensor.trv_name_signal_strength-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.trv_name_signal_strength',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.SIGNAL_STRENGTH: 'signal_strength'>,
'original_icon': None,
'original_name': 'TRV-Name signal strength',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '123456789ABC-blutrv:200-blutrv_rssi',
'unit_of_measurement': 'dBm',
})
# ---
# name: test_blu_trv_sensor_entity[sensor.trv_name_signal_strength-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'signal_strength',
'friendly_name': 'TRV-Name signal strength',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': 'dBm',
}),
'context': <ANY>,
'entity_id': 'sensor.trv_name_signal_strength',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '-60',
})
# ---
# name: test_blu_trv_sensor_entity[sensor.trv_name_valve_position-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.trv_name_valve_position',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'TRV-Name valve position',
'platform': 'shelly',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'valve_position',
'unique_id': '123456789ABC-blutrv:200-valve_position',
'unit_of_measurement': '%',
})
# ---
# name: test_blu_trv_sensor_entity[sensor.trv_name_valve_position-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'TRV-Name valve position',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'sensor.trv_name_valve_position',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0',
})
# ---
18 changes: 9 additions & 9 deletions tests/components/shelly/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aioshelly.const import MODEL_BLU_GATEWAY_GEN3, MODEL_MOTION
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.shelly.const import UPDATE_PERIOD_MULTIPLIER
Expand Down Expand Up @@ -483,17 +484,16 @@ async def test_blu_trv_binary_sensor_entity(
hass: HomeAssistant,
mock_blu_trv: Mock,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test BLU TRV binary sensor entity."""

entity_id = "binary_sensor.trv_name_calibration"

await init_integration(hass, 3, model=MODEL_BLU_GATEWAY_GEN3)

state = hass.states.get(entity_id)
assert state
assert state.state == STATE_OFF
for entity in ("calibration",):
entity_id = f"{BINARY_SENSOR_DOMAIN}.trv_name_{entity}"

entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "123456789ABC-blutrv:200-calibration"
state = hass.states.get(entity_id)
assert state == snapshot(name=f"{entity_id}-state")

entry = entity_registry.async_get(entity_id)
assert entry == snapshot(name=f"{entity_id}-entry")
17 changes: 9 additions & 8 deletions tests/components/shelly/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aioshelly.const import MODEL_BLU_GATEWAY_GEN3
from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.number import (
ATTR_MAX,
Expand Down Expand Up @@ -398,19 +399,19 @@ async def test_blu_trv_number_entity(
mock_blu_trv: Mock,
entity_registry: EntityRegistry,
monkeypatch: pytest.MonkeyPatch,
snapshot: SnapshotAssertion,
) -> None:
"""Test BLU TRV number entity."""

entity_id = "number.trv_name_valve_position"
# disable automatic temperature control in the device
monkeypatch.setitem(mock_blu_trv.config["blutrv:200"], "enable", False)

await init_integration(hass, 3, model=MODEL_BLU_GATEWAY_GEN3)

state = hass.states.get(entity_id)
assert state
assert state.state == "0"
for entity in ("external_temperature", "valve_position"):
entity_id = f"{NUMBER_DOMAIN}.trv_name_{entity}"

entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "123456789ABC-blutrv:200-valve_position"
state = hass.states.get(entity_id)
assert state == snapshot(name=f"{entity_id}-state")

entry = entity_registry.async_get(entity_id)
assert entry == snapshot(name=f"{entity_id}-entry")
Loading

0 comments on commit aed07a4

Please sign in to comment.