diff --git a/homeassistant/components/valve/__init__.py b/homeassistant/components/valve/__init__.py index 9521d597303..c04e25355ff 100644 --- a/homeassistant/components/valve/__init__.py +++ b/homeassistant/components/valve/__init__.py @@ -186,9 +186,10 @@ class ValveEntity(Entity): @final @property - def state_attributes(self) -> dict[str, Any]: + def state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" - + if not self.reports_position: + return None return {ATTR_CURRENT_POSITION: self.current_valve_position} @property diff --git a/tests/components/valve/snapshots/test_init.ambr b/tests/components/valve/snapshots/test_init.ambr new file mode 100644 index 00000000000..b46d76b6f0c --- /dev/null +++ b/tests/components/valve/snapshots/test_init.ambr @@ -0,0 +1,56 @@ +# serializer version: 1 +# name: test_valve_setup + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Valve', + 'supported_features': , + }), + 'context': , + 'entity_id': 'valve.valve', + 'last_changed': , + 'last_updated': , + 'state': 'open', + }) +# --- +# name: test_valve_setup.1 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_position': 50, + 'friendly_name': 'Valve', + 'supported_features': , + }), + 'context': , + 'entity_id': 'valve.valve_2', + 'last_changed': , + 'last_updated': , + 'state': 'open', + }) +# --- +# name: test_valve_setup.2 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Valve', + 'restored': True, + 'supported_features': , + }), + 'context': , + 'entity_id': 'valve.valve', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- +# name: test_valve_setup.3 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Valve', + 'restored': True, + 'supported_features': , + }), + 'context': , + 'entity_id': 'valve.valve_2', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- diff --git a/tests/components/valve/test_init.py b/tests/components/valve/test_init.py index 08b0771da8e..6f5c49830bb 100644 --- a/tests/components/valve/test_init.py +++ b/tests/components/valve/test_init.py @@ -2,6 +2,7 @@ from collections.abc import Generator import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.valve import ( DOMAIN, @@ -193,26 +194,34 @@ def mock_config_entry(hass) -> tuple[MockConfigEntry, list[ValveEntity]]: async def test_valve_setup( - hass: HomeAssistant, mock_config_entry: tuple[MockConfigEntry, list[ValveEntity]] + hass: HomeAssistant, + mock_config_entry: tuple[MockConfigEntry, list[ValveEntity]], + snapshot: SnapshotAssertion, ) -> None: """Test setup and tear down of valve platform and entity.""" config_entry = mock_config_entry[0] assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - entity_id = mock_config_entry[1][0].entity_id assert config_entry.state == ConfigEntryState.LOADED - assert hass.states.get(entity_id) + for entity in mock_config_entry[1]: + entity_id = entity.entity_id + state = hass.states.get(entity_id) + assert state + assert state == snapshot assert await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() assert config_entry.state == ConfigEntryState.NOT_LOADED - entity_state = hass.states.get(entity_id) - assert entity_state - assert entity_state.state == STATE_UNAVAILABLE + for entity in mock_config_entry[1]: + entity_id = entity.entity_id + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_UNAVAILABLE + assert state == snapshot async def test_services(