Skip to content

Commit

Permalink
Handle tilt position being None in HKC (home-assistant#117141)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 10, 2024
1 parent e4a3cab commit 4138c7a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion homeassistant/components/homekit_controller/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,15 @@ def is_vertical_tilt(self) -> bool:
)

@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
Expand Down
34 changes: 34 additions & 0 deletions tests/components/homekit_controller/test_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4138c7a

Please sign in to comment.