Skip to content

Commit 0ac3f77

Browse files
authored
set shorthand atrributes for supported_features in velux cover (home-assistant#156524)
1 parent 8e8a4ff commit 0ac3f77

File tree

3 files changed

+110
-26
lines changed

3 files changed

+110
-26
lines changed

homeassistant/components/velux/cover.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,32 @@ class VeluxCover(VeluxEntity, CoverEntity):
5656
def __init__(self, node: OpeningDevice, config_entry_id: str) -> None:
5757
"""Initialize VeluxCover."""
5858
super().__init__(node, config_entry_id)
59+
# Features common to all covers
60+
self._attr_supported_features = (
61+
CoverEntityFeature.OPEN
62+
| CoverEntityFeature.CLOSE
63+
| CoverEntityFeature.SET_POSITION
64+
| CoverEntityFeature.STOP
65+
)
5966
# Window is the default device class for covers
6067
self._attr_device_class = CoverDeviceClass.WINDOW
6168
if isinstance(node, Awning):
6269
self._attr_device_class = CoverDeviceClass.AWNING
63-
if isinstance(node, Blind):
64-
self._attr_device_class = CoverDeviceClass.BLIND
65-
self._is_blind = True
6670
if isinstance(node, GarageDoor):
6771
self._attr_device_class = CoverDeviceClass.GARAGE
6872
if isinstance(node, Gate):
6973
self._attr_device_class = CoverDeviceClass.GATE
7074
if isinstance(node, RollerShutter):
7175
self._attr_device_class = CoverDeviceClass.SHUTTER
72-
73-
@property
74-
def supported_features(self) -> CoverEntityFeature:
75-
"""Flag supported features."""
76-
supported_features = (
77-
CoverEntityFeature.OPEN
78-
| CoverEntityFeature.CLOSE
79-
| CoverEntityFeature.SET_POSITION
80-
| CoverEntityFeature.STOP
81-
)
82-
if self.current_cover_tilt_position is not None:
83-
supported_features |= (
76+
if isinstance(node, Blind):
77+
self._attr_device_class = CoverDeviceClass.BLIND
78+
self._is_blind = True
79+
self._attr_supported_features |= (
8480
CoverEntityFeature.OPEN_TILT
8581
| CoverEntityFeature.CLOSE_TILT
8682
| CoverEntityFeature.SET_TILT_POSITION
8783
| CoverEntityFeature.STOP_TILT
8884
)
89-
return supported_features
9085

9186
@property
9287
def current_cover_position(self) -> int:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# serializer version: 1
2+
# name: test_cover_setup[cover.test_window-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': None,
8+
'config_entry_id': <ANY>,
9+
'config_subentry_id': <ANY>,
10+
'device_class': None,
11+
'device_id': <ANY>,
12+
'disabled_by': None,
13+
'domain': 'cover',
14+
'entity_category': None,
15+
'entity_id': 'cover.test_window',
16+
'has_entity_name': True,
17+
'hidden_by': None,
18+
'icon': None,
19+
'id': <ANY>,
20+
'labels': set({
21+
}),
22+
'name': None,
23+
'options': dict({
24+
}),
25+
'original_device_class': <CoverDeviceClass.WINDOW: 'window'>,
26+
'original_icon': None,
27+
'original_name': None,
28+
'platform': 'velux',
29+
'previous_unique_id': None,
30+
'suggested_object_id': None,
31+
'supported_features': <CoverEntityFeature: 15>,
32+
'translation_key': None,
33+
'unique_id': '123456789',
34+
'unit_of_measurement': None,
35+
})
36+
# ---
37+
# name: test_cover_setup[cover.test_window-state]
38+
StateSnapshot({
39+
'attributes': ReadOnlyDict({
40+
'current_position': 70,
41+
'device_class': 'window',
42+
'friendly_name': 'Test Window',
43+
'supported_features': <CoverEntityFeature: 15>,
44+
}),
45+
'context': <ANY>,
46+
'entity_id': 'cover.test_window',
47+
'last_changed': <ANY>,
48+
'last_reported': <ANY>,
49+
'last_updated': <ANY>,
50+
'state': 'open',
51+
})
52+
# ---

tests/components/velux/test_cover.py

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,69 @@
11
"""Tests for the Velux cover platform."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock
44

5-
from freezegun.api import FrozenDateTimeFactory
65
import pytest
76

7+
from homeassistant.components.velux import DOMAIN
88
from homeassistant.const import STATE_CLOSED, STATE_OPEN, Platform
99
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers import device_registry as dr, entity_registry as er
1011

1112
from . import update_callback_entity
1213

13-
from tests.common import MockConfigEntry
14+
from tests.common import MockConfigEntry, SnapshotAssertion, snapshot_platform
1415

1516

16-
@pytest.mark.usefixtures("mock_pyvlx")
17+
@pytest.fixture
18+
def platform() -> Platform:
19+
"""Fixture to specify platform to test."""
20+
return Platform.COVER
21+
22+
23+
@pytest.mark.usefixtures("setup_integration")
24+
async def test_cover_setup(
25+
hass: HomeAssistant,
26+
mock_config_entry: MockConfigEntry,
27+
entity_registry: er.EntityRegistry,
28+
device_registry: dr.DeviceRegistry,
29+
snapshot: SnapshotAssertion,
30+
) -> None:
31+
"""Snapshot the cover entity (registry + state)."""
32+
await snapshot_platform(
33+
hass,
34+
entity_registry,
35+
snapshot,
36+
mock_config_entry.entry_id,
37+
)
38+
39+
# Get the cover entity setup and test device association
40+
entity_entries = er.async_entries_for_config_entry(
41+
entity_registry, mock_config_entry.entry_id
42+
)
43+
assert len(entity_entries) == 1
44+
entry = entity_entries[0]
45+
46+
assert entry.device_id is not None
47+
device_entry = device_registry.async_get(entry.device_id)
48+
assert device_entry is not None
49+
assert (DOMAIN, f"{123456789}") in device_entry.identifiers
50+
assert device_entry.via_device_id is not None
51+
via_device_entry = device_registry.async_get(device_entry.via_device_id)
52+
assert via_device_entry is not None
53+
assert (
54+
DOMAIN,
55+
f"gateway_{mock_config_entry.entry_id}",
56+
) in via_device_entry.identifiers
57+
58+
59+
@pytest.mark.usefixtures("setup_integration")
1760
async def test_cover_closed(
1861
hass: HomeAssistant,
1962
mock_window: AsyncMock,
2063
mock_config_entry: MockConfigEntry,
21-
freezer: FrozenDateTimeFactory,
2264
) -> None:
2365
"""Test the cover closed state."""
2466

25-
mock_config_entry.add_to_hass(hass)
26-
with patch("homeassistant.components.velux.PLATFORMS", [Platform.COVER]):
27-
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
28-
await hass.async_block_till_done()
29-
3067
test_entity_id = "cover.test_window"
3168

3269
# Initial state should be open

0 commit comments

Comments
 (0)