From 9cae3583ebfe8bee9ae833d7455455cbd3da4226 Mon Sep 17 00:00:00 2001 From: Artem Sorokin Date: Tue, 19 Dec 2023 12:41:36 +0300 Subject: [PATCH] Fix service call for relative only custom range (#482) --- .../yandex_smart_home/capability_custom.py | 2 +- .../yandex_smart_home/capability_range.py | 2 +- tests/test_capability_custom.py | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/custom_components/yandex_smart_home/capability_custom.py b/custom_components/yandex_smart_home/capability_custom.py index 2bdead35..3be22776 100644 --- a/custom_components/yandex_smart_home/capability_custom.py +++ b/custom_components/yandex_smart_home/capability_custom.py @@ -228,7 +228,7 @@ async def set_instance_state(self, context: Context, state: RangeCapabilityInsta if state.relative: if self._increase_value_service_config or self._decrease_value_service_config: - if state.value >= 0: + if state.value > 0: service_config = self._increase_value_service_config else: service_config = self._decrease_value_service_config diff --git a/custom_components/yandex_smart_home/capability_range.py b/custom_components/yandex_smart_home/capability_range.py index add6f018..c48d17f9 100644 --- a/custom_components/yandex_smart_home/capability_range.py +++ b/custom_components/yandex_smart_home/capability_range.py @@ -540,7 +540,7 @@ async def set_instance_state(self, context: Context, state: RangeCapabilityInsta self._state_features & media_player.MediaPlayerEntityFeature.PREVIOUS_TRACK and self._state_features & media_player.MediaPlayerEntityFeature.NEXT_TRACK ): - if state.value >= 0: + if state.value > 0: service = media_player.SERVICE_MEDIA_NEXT_TRACK else: service = media_player.SERVICE_MEDIA_PREVIOUS_TRACK diff --git a/tests/test_capability_custom.py b/tests/test_capability_custom.py index 58c812bb..b8b237f9 100644 --- a/tests/test_capability_custom.py +++ b/tests/test_capability_custom.py @@ -634,14 +634,15 @@ async def test_capability_custom_range_relative_override_no_state(hass): assert calls[0].data == {"entity_id": "input_number.test", "value": "value: 10"} calls = async_mock_service(hass, "test", "decrease_value") - for value in (-3, -50): + for value in (0, -3, -50): await cap.set_instance_state( Context(), RangeCapabilityInstanceActionState(instance=RangeCapabilityInstance.OPEN, value=value, relative=True), ) - assert len(calls) == 2 - assert calls[0].data == {"entity_id": "input_number.test", "value": "value: -3"} - assert calls[1].data == {"entity_id": "input_number.test", "value": "value: -50"} + assert len(calls) == 3 + assert calls[0].data == {"entity_id": "input_number.test", "value": "value: 0"} + assert calls[1].data == {"entity_id": "input_number.test", "value": "value: -3"} + assert calls[2].data == {"entity_id": "input_number.test", "value": "value: -50"} async def test_capability_custom_range_only_relative(hass): @@ -682,14 +683,15 @@ async def test_capability_custom_range_only_relative(hass): assert calls[0].data == {"entity_id": "input_number.test", "value": "value: 10"} calls = async_mock_service(hass, "test", "decrease_value") - for value in (-3, -50): + for value in (0, -3, -50): await cap.set_instance_state( Context(), RangeCapabilityInstanceActionState(instance=RangeCapabilityInstance.OPEN, value=value, relative=True), ) - assert len(calls) == 2 - assert calls[0].data == {"entity_id": "input_number.test", "value": "value: -3"} - assert calls[1].data == {"entity_id": "input_number.test", "value": "value: -50"} + assert len(calls) == 3 + assert calls[0].data == {"entity_id": "input_number.test", "value": "value: 0"} + assert calls[1].data == {"entity_id": "input_number.test", "value": "value: -3"} + assert calls[2].data == {"entity_id": "input_number.test", "value": "value: -50"} async def test_capability_custom_range_no_service(hass):