diff --git a/homeassistant/components/homekit_controller/cover.py b/homeassistant/components/homekit_controller/cover.py index ca041d49e11..d0944db38f8 100644 --- a/homeassistant/components/homekit_controller/cover.py +++ b/homeassistant/components/homekit_controller/cover.py @@ -212,13 +212,15 @@ class HomeKitWindowCover(HomeKitEntity, CoverEntity): ) @property - def current_cover_tilt_position(self) -> int: + def current_cover_tilt_position(self) -> int | None: """Return current position of cover tilt.""" tilt_position = self.service.value(CharacteristicsTypes.VERTICAL_TILT_CURRENT) if not tilt_position: tilt_position = self.service.value( CharacteristicsTypes.HORIZONTAL_TILT_CURRENT ) + if tilt_position is None: + return None # Recalculate to convert from arcdegree scale to percentage scale. if self.is_vertical_tilt: scale = 0.9 diff --git a/tests/components/homekit_controller/test_cover.py b/tests/components/homekit_controller/test_cover.py index 671e9779d30..2157eb51212 100644 --- a/tests/components/homekit_controller/test_cover.py +++ b/tests/components/homekit_controller/test_cover.py @@ -3,6 +3,7 @@ from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.services import ServicesTypes +from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -94,6 +95,24 @@ def create_window_covering_service_with_v_tilt_2(accessory): tilt_target.maxValue = 0 +def create_window_covering_service_with_none_tilt(accessory): + """Define a window-covering characteristics as per page 219 of HAP spec. + + This accessory uses None for the tilt value unexpectedly. + """ + service = create_window_covering_service(accessory) + + tilt_current = service.add_char(CharacteristicsTypes.VERTICAL_TILT_CURRENT) + tilt_current.value = None + tilt_current.minValue = -90 + tilt_current.maxValue = 0 + + tilt_target = service.add_char(CharacteristicsTypes.VERTICAL_TILT_TARGET) + tilt_target.value = None + tilt_target.minValue = -90 + tilt_target.maxValue = 0 + + async def test_change_window_cover_state(hass: HomeAssistant) -> None: """Test that we can turn a HomeKit alarm on and off again.""" helper = await setup_test_component(hass, create_window_covering_service) @@ -212,6 +231,21 @@ async def test_read_window_cover_tilt_vertical_2(hass: HomeAssistant) -> None: assert state.attributes["current_tilt_position"] == 83 +async def test_read_window_cover_tilt_missing_tilt(hass: HomeAssistant) -> None: + """Test that missing tilt is handled.""" + helper = await setup_test_component( + hass, create_window_covering_service_with_none_tilt + ) + + await helper.async_update( + ServicesTypes.WINDOW_COVERING, + {CharacteristicsTypes.OBSTRUCTION_DETECTED: True}, + ) + state = await helper.poll_and_get_state() + assert "current_tilt_position" not in state.attributes + assert state.state != STATE_UNAVAILABLE + + async def test_write_window_cover_tilt_horizontal(hass: HomeAssistant) -> None: """Test that horizontal tilt is written correctly.""" helper = await setup_test_component(