Handle tilt position being None in HKC (#117141)

This commit is contained in:
J. Nick Koston 2024-05-10 00:47:13 -05:00 committed by GitHub
parent e4a3cab801
commit 4138c7a0ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View file

@ -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

View file

@ -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(