From fcafc56b044a094a28613ab15dfc563f8e5a8087 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 1 Dec 2026 14:43:51 +0100 Subject: [PATCH] Reject non-integer closed tilt positions in cover (#194589) Co-authored-by: Martin Hjelmare --- homeassistant/components/cover/entity.py | 15 ++++++--- tests/components/cover/test_init.py | 42 ++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/cover/entity.py b/homeassistant/components/cover/entity.py index ec4f4fa7fbe2..f98b60db55f8 100644 --- a/homeassistant/components/cover/entity.py +++ b/homeassistant/components/cover/entity.py @@ -192,8 +192,8 @@ class CoverEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Return the tilt position where the slats are open. open_cover_tilt moves the slats to this position. None means the - default: the position where the angle is 0 degrees if the angles - are set, 100 otherwise. + default: the position where the angle is 0 degrees, rounded and + clamped to 0-100, if the angles are set, 100 otherwise. """ return self._attr_open_tilt_position @@ -202,8 +202,8 @@ class CoverEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Return the tilt positions where the slats are closed: 0, 100 or both. close_cover_tilt moves the slats to one of them. None means the - default: the ends where the slats lie flat if the angles are set, - 0 otherwise. + default: the ends where the slats lie the flattest if the angles are + set, otherwise 0, or 100 if the open position is 0. """ return self._attr_closed_tilt_positions @@ -258,7 +258,12 @@ class CoverEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): open_position = self._resolved_open_tilt_position() if (closed := self.closed_tilt_positions) is not None: positions = list(closed) - if positions not in ([0], [100], [0, 100], [100, 0]): + if any(type(p) is not int for p in positions) or positions not in ( + [0], + [100], + [0, 100], + [100, 0], + ): raise ValueError( f"Entity {self.entity_id} ({type(self)}) has invalid closed tilt " f"positions {closed}, expected 0, 100 or both" diff --git a/tests/components/cover/test_init.py b/tests/components/cover/test_init.py index 56595734f843..11035c1e4b07 100644 --- a/tests/components/cover/test_init.py +++ b/tests/components/cover/test_init.py @@ -169,7 +169,7 @@ def _tilt_cover(**values: Any) -> MockCover: ) -def _tilt_attributes(hass: HomeAssistant, ent: MockCover) -> dict[str, Any]: +def _tilt_attributes(hass: HomeAssistant, ent: Entity) -> dict[str, Any]: """Return the tilt capability attributes of the state of a cover.""" attributes = hass.states.get(ent.entity_id).attributes return { @@ -184,7 +184,7 @@ def _tilt_attributes(hass: HomeAssistant, ent: MockCover) -> dict[str, Any]: } -async def _setup_cover(hass: HomeAssistant, ent: MockCover) -> None: +async def _setup_cover(hass: HomeAssistant, ent: Entity) -> None: """Set up a cover entity.""" setup_test_component_platform(hass, cover.DOMAIN, [ent]) assert await async_setup_component( @@ -242,6 +242,11 @@ def _angles(start: int, end: int) -> dict[str, int]: {"open_tilt_position": 67, "closed_tilt_positions": [0]} | _angles(-90, 45), id="rounded_open_position", ), + pytest.param( + _angles(-90, 54), + {"open_tilt_position": 62, "closed_tilt_positions": [0]} | _angles(-90, 54), + id="rounded_open_position_tie_to_even", + ), pytest.param( _angles(-60, 60), {"open_tilt_position": 50, "closed_tilt_positions": [0, 100]} @@ -344,6 +349,16 @@ async def test_tilt_capability_attributes( "invalid closed tilt positions", id="closed_position_twice", ), + pytest.param( + {"open_tilt_position": 50, "closed_tilt_positions": [False]}, + "invalid closed tilt positions", + id="closed_position_is_a_bool", + ), + pytest.param( + {"open_tilt_position": 50, "closed_tilt_positions": [0.0]}, + "invalid closed tilt positions", + id="closed_position_not_an_integer", + ), pytest.param( {"open_tilt_position": 100, "closed_tilt_positions": [0, 100]}, "in its closed tilt positions", @@ -369,6 +384,29 @@ def test_invalid_tilt_values(values: dict[str, Any], match: str) -> None: _ = ent.capability_attributes +async def test_tilt_capability_attributes_from_attr(hass: HomeAssistant) -> None: + """Test the tilt capability attributes set with the _attr_ shorthands.""" + + class AttrCover(cover.CoverEntity): + _attr_name = "Cover" + _attr_unique_id = "cover" + _attr_supported_features = TILT_FEATURES + _attr_current_cover_tilt_position = 50 + _attr_is_closed = False + _attr_open_tilt_position = 40 + _attr_closed_tilt_positions = [100, 0] + _attr_tilt_start_angle = -90 + _attr_tilt_end_angle = 90 + + ent = AttrCover() + await _setup_cover(hass, ent) + + assert _tilt_attributes(hass, ent) == { + "open_tilt_position": 40, + "closed_tilt_positions": [0, 100], + } | _angles(-90, 90) + + @pytest.mark.parametrize( "supported_features", [