Skip to content

Commit

Permalink
fix: do not trigger background action for finished animations
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlba91 committed Dec 12, 2023
1 parent 28bc0ce commit 7812e24
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
19 changes: 14 additions & 5 deletions custom_components/hella_onyx/sensors/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ def brightness(self) -> int:
self._uuid,
brightness,
)
if brightness.animation is not None and len(brightness.animation.keyframes) > 0:
if (
brightness.animation is not None
and len(brightness.animation.keyframes) > 0
and brightness.animation.keyframes[
len(brightness.animation.keyframes) - 1
].value
!= brightness.value
):
self._start_update_device(brightness.animation)
return brightness.value / brightness.maximum * 255

Expand Down Expand Up @@ -166,10 +173,12 @@ def _start_update_device(self, animation: AnimationValue):
)

if updating:
track_point_in_utc_time(
self.hass,
self._end_update_device,
utcnow() + timedelta(seconds=delta + INCREASED_INTERVAL_DELTA),
asyncio.run_coroutine_threadsafe(
track_point_in_utc_time(
self.hass,
self._end_update_device,
utcnow() + timedelta(seconds=delta + INCREASED_INTERVAL_DELTA),
)
)
else:
_LOGGER.debug("end update device %s due to too old data", self._uuid)
Expand Down
18 changes: 16 additions & 2 deletions custom_components/hella_onyx/sensors/shutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ def current_cover_position(self) -> int:
self._uuid,
position,
)
if position.animation is not None and len(position.animation.keyframes) > 0:
if (
position.animation is not None
and len(position.animation.keyframes) > 0
and position.animation.keyframes[
len(position.animation.keyframes) - 1
].value
!= position.value
):
self._start_moving_device(position.animation)
return 100 - int(position.value / position.maximum * 100)

Expand All @@ -114,7 +121,14 @@ def current_cover_tilt_position(self) -> int:
self._uuid,
position,
)
if position.animation is not None and len(position.animation.keyframes) > 0:
if (
position.animation is not None
and len(position.animation.keyframes) > 0
and position.animation.keyframes[
len(position.animation.keyframes) - 1
].value
!= position.value
):
self._start_moving_device(position.animation)
return int(position.value / self._max_angle * 100)

Expand Down
25 changes: 24 additions & 1 deletion tests/sensors/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_brightness_with_animation(self, api, entity, device):
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
interpolation="linear", duration=10, delay=0, value=20
)
],
)
Expand All @@ -120,6 +120,29 @@ def test_brightness_with_animation(self, api, entity, device):
mock_start_update_device.assert_called_with(animation)
assert api.device.called

def test_brightness_with_old_animation(self, api, entity, device):
animation = AnimationValue(
start=0,
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
)
],
)
device.actual_brightness = NumericValue(
value=10,
minimum=0,
maximum=100,
read_only=False,
animation=animation,
)
api.device.return_value = device
with patch.object(entity, "_start_update_device") as mock_start_update_device:
assert entity.brightness == 25.5
mock_start_update_device.assert_not_called
assert api.device.called

def test_is_on(self, api, entity, device):
device.actual_brightness = NumericValue(
value=10, minimum=0, maximum=100, read_only=False
Expand Down
42 changes: 40 additions & 2 deletions tests/sensors/test_shutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_current_cover_position_with_animation(self, api, entity, device):
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
interpolation="linear", duration=10, delay=0, value=20
)
],
)
Expand All @@ -118,6 +118,25 @@ def test_current_cover_position_with_animation(self, api, entity, device):
mock_start_moving_device.assert_called_with(animation)
assert api.device.called

def test_current_cover_position_with_old_animation(self, api, entity, device):
animation = AnimationValue(
start=0,
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
)
],
)
device.actual_position = NumericValue(
value=10, minimum=0, maximum=100, read_only=False, animation=animation
)
api.device.return_value = device
with patch.object(entity, "_start_moving_device") as mock_start_moving_device:
assert entity.current_cover_position == 90
mock_start_moving_device.assert_not_called
assert api.device.called

def test_current_cover_tilt_position(self, api, entity, device):
device.actual_angle = NumericValue(
value=10, minimum=0, maximum=100, read_only=False
Expand All @@ -132,7 +151,7 @@ def test_current_cover_tilt_position_with_animation(self, api, entity, device):
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
interpolation="linear", duration=10, delay=0, value=20
)
],
)
Expand All @@ -145,6 +164,25 @@ def test_current_cover_tilt_position_with_animation(self, api, entity, device):
mock_start_moving_device.assert_called_with(animation)
assert api.device.called

def test_current_cover_tilt_position_with_old_animation(self, api, entity, device):
animation = AnimationValue(
start=0,
current_value=0,
keyframes=[
AnimationKeyframe(
interpolation="linear", duration=10, delay=0, value=10
)
],
)
device.actual_angle = NumericValue(
value=10, minimum=0, maximum=100, read_only=False, animation=animation
)
api.device.return_value = device
with patch.object(entity, "_start_moving_device") as mock_start_moving_device:
assert entity.current_cover_tilt_position == 11
mock_start_moving_device.assert_not_called
assert api.device.called

def test_is_not_opening(self, entity):
assert not entity.is_opening

Expand Down

0 comments on commit 7812e24

Please sign in to comment.