diff --git a/homeassistant/components/qbus/cover.py b/homeassistant/components/qbus/cover.py index 4afa0c17f898..601b2aad45f1 100644 --- a/homeassistant/components/qbus/cover.py +++ b/homeassistant/components/qbus/cover.py @@ -21,6 +21,7 @@ from .entity import QbusEntity, create_new_entities PARALLEL_UPDATES = 0 OPEN_TILT_POSITION = 50 +CLOSED_TILT_POSITIONS = (0, 100) async def async_setup_entry( @@ -140,9 +141,16 @@ class QbusCover(QbusEntity, CoverEntity): @override async def async_close_cover_tilt(self, **kwargs: Any) -> None: - """Close the cover tilt.""" + """Close the cover tilt at the nearest closed end.""" + current = self._target_slat_position + + if current in CLOSED_TILT_POSITIONS: + return + state = QbusMqttShutterState(id=self._mqtt_output.id, type=StateType.STATE) - state.write_slat_position(0) + state.write_slat_position( + 100 if current is not None and current > OPEN_TILT_POSITION else 0 + ) await self._async_publish_output_state(state) @override @@ -177,7 +185,7 @@ class QbusCover(QbusEntity, CoverEntity): if self._attr_supported_features & CoverEntityFeature.SET_TILT_POSITION: self._attr_is_closed = ( self._target_shutter_position == 0 - and self._target_slat_position in (0, 100) + and self._target_slat_position in CLOSED_TILT_POSITIONS ) else: self._attr_is_closed = self._target_shutter_position == 0 diff --git a/tests/components/qbus/test_cover.py b/tests/components/qbus/test_cover.py index dc02d9dda13e..01dc530bc72c 100644 --- a/tests/components/qbus/test_cover.py +++ b/tests/components/qbus/test_cover.py @@ -36,7 +36,7 @@ _PAYLOAD_POS_STATE_POSITION = ( ) _PAYLOAD_SLAT_STATE_CLOSED = ( - '{"id":"UL31","properties":{"slatPosition":0},"type":"event"}' + '{"id":"UL31","properties":{"slatPosition":100},"type":"event"}' ) _PAYLOAD_SLAT_STATE_FULLY_CLOSED = ( '{"id":"UL31","properties":{"slatPosition":0,"shutterPosition":0},"type":"event"}' @@ -280,14 +280,82 @@ async def test_cover_slats( ) publish_state = _get_publish_state(mock_publish_state) - assert publish_state.read_slat_position() == 0 + assert publish_state.read_slat_position() == 100 async_fire_mqtt_message(hass, _TOPIC_SLAT_STATE, _PAYLOAD_SLAT_STATE_CLOSED) await hass.async_block_till_done() entity_state = hass.states.get(_ENTITY_ID_SLAT) assert entity_state.state == CoverState.CLOSED - assert entity_state.attributes[ATTR_CURRENT_TILT_POSITION] == 0 + assert entity_state.attributes[ATTR_CURRENT_TILT_POSITION] == 100 + + +@pytest.mark.parametrize( + ("slat_position", "sent_slat_position"), + [ + pytest.param(30, 0, id="nearest_0"), + pytest.param(50, 0, id="open"), + pytest.param(70, 100, id="nearest_100"), + ], +) +@pytest.mark.usefixtures("setup_integration") +async def test_cover_slats_close_tilt( + hass: HomeAssistant, + mock_publish_state: AsyncMock, + slat_position: int, + sent_slat_position: int, +) -> None: + """Test closing the slats moves them to the nearest closed end.""" + _fire_slat_position(hass, slat_position) + await hass.async_block_till_done() + + mock_publish_state.reset_mock() + await hass.services.async_call( + COVER_DOMAIN, + CoverService.CLOSE_TILT, + {TargetKey.ENTITY_ID: _ENTITY_ID_SLAT}, + blocking=True, + ) + + assert _get_publish_state(mock_publish_state).read_slat_position() == ( + sent_slat_position + ) + + +@pytest.mark.parametrize("slat_position", [0, 100]) +@pytest.mark.usefixtures("setup_integration") +async def test_cover_slats_close_tilt_already_closed( + hass: HomeAssistant, mock_publish_state: AsyncMock, slat_position: int +) -> None: + """Test closing slats that are already closed leaves them where they are.""" + _fire_slat_position(hass, slat_position) + await hass.async_block_till_done() + + mock_publish_state.reset_mock() + await hass.services.async_call( + COVER_DOMAIN, + CoverService.CLOSE_TILT, + {TargetKey.ENTITY_ID: _ENTITY_ID_SLAT}, + blocking=True, + ) + + mock_publish_state.assert_not_called() + + +@pytest.mark.usefixtures("setup_integration") +async def test_cover_slats_close_tilt_unknown_position( + hass: HomeAssistant, mock_publish_state: AsyncMock +) -> None: + """Test closing slats with an unknown position closes them at 0.""" + mock_publish_state.reset_mock() + await hass.services.async_call( + COVER_DOMAIN, + CoverService.CLOSE_TILT, + {TargetKey.ENTITY_ID: _ENTITY_ID_SLAT}, + blocking=True, + ) + + assert _get_publish_state(mock_publish_state).read_slat_position() == 0 @pytest.mark.parametrize( @@ -296,6 +364,7 @@ async def test_cover_slats( pytest.param(0, 50, id="closed_at_0"), pytest.param(100, 50, id="closed_at_100"), pytest.param(50, 0, id="open"), + pytest.param(75, 100, id="open_nearest_100"), ], ) @pytest.mark.usefixtures("setup_integration") @@ -306,11 +375,7 @@ async def test_cover_slats_toggle_tilt( sent_slat_position: int, ) -> None: """Test toggling the slats opens them at both closed ends.""" - async_fire_mqtt_message( - hass, - _TOPIC_SLAT_STATE, - f'{{"id":"UL31","properties":{{"slatPosition":{slat_position}}},"type":"event"}}', - ) + _fire_slat_position(hass, slat_position) await hass.async_block_till_done() mock_publish_state.reset_mock() @@ -326,6 +391,14 @@ async def test_cover_slats_toggle_tilt( ) +def _fire_slat_position(hass: HomeAssistant, slat_position: int) -> None: + async_fire_mqtt_message( + hass, + _TOPIC_SLAT_STATE, + f'{{"id":"UL31","properties":{{"slatPosition":{slat_position}}},"type":"event"}}', + ) + + def _get_publish_state(mock_publish_state: AsyncMock) -> QbusMqttShutterState: assert mock_publish_state.call_count == 1 state = mock_publish_state.call_args.args[0]