Skip to content

Commit 8f70612

Browse files
committed
fix: set dim duration correctly; add tests for dim duration variations
1 parent d197388 commit 8f70612

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

custom_components/hella_onyx/sensors/light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def _get_dim_duration(self, target) -> int:
239239
brightness = self._actual_brightness
240240
return abs(
241241
int(
242-
(target - brightness.value)
242+
abs(target - brightness.value)
243243
/ brightness.maximum
244244
* (MAX_USED_DIM_DURATION - MIN_USED_DIM_DURATION)
245245
+ MIN_USED_DIM_DURATION

tests/sensors/test_light.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_turn_on(self, mock_run_coroutine_threadsafe, api, entity, device):
186186
"uuid",
187187
{
188188
"target_brightness": 4,
189-
"dim_duration": 4780,
189+
"dim_duration": 5780,
190190
},
191191
)
192192
assert mock_run_coroutine_threadsafe.called
@@ -208,14 +208,6 @@ def test_turn_on_no_brightness(
208208
assert mock_run_coroutine_threadsafe.called
209209
assert not api.device.called
210210

211-
def test__get_dim_duration(self, api, entity, device):
212-
device.actual_brightness = NumericValue(
213-
value=14645, maximum=65535, minimum=0, read_only=False
214-
)
215-
api.device.return_value = device
216-
assert entity._get_dim_duration(31) == 726
217-
assert api.device.called
218-
219211
def test__actual_brightness_no_value(self, api, entity, device):
220212
device.actual_brightness = NumericValue(
221213
value=None, maximum=100, minimum=0, read_only=False
@@ -232,6 +224,14 @@ def test__actual_brightness(self, api, entity, device):
232224
assert entity._actual_brightness == NumericValue(1, 0, 100, False)
233225
assert api.device.called
234226

227+
def test__get_dim_duration(self, api, entity, device):
228+
device.actual_brightness = NumericValue(
229+
value=14645, maximum=65535, minimum=0, read_only=False
230+
)
231+
api.device.return_value = device
232+
assert entity._get_dim_duration(31) == 1726
233+
assert api.device.called
234+
235235
def test__get_dim_duration_same(self, api, entity, device):
236236
device.actual_brightness = NumericValue(
237237
value=100, maximum=100, minimum=0, read_only=False
@@ -248,6 +248,22 @@ def test__get_dim_duration_invalid_value(self, api, entity, device):
248248
assert entity._get_dim_duration(90) == 5450
249249
assert api.device.called
250250

251+
def test__get_dim_duration_actual_lower_than_new(self, api, entity, device):
252+
device.actual_brightness = NumericValue(
253+
value=0, maximum=65535, minimum=0, read_only=False
254+
)
255+
api.device.return_value = device
256+
assert entity._get_dim_duration(65535) == 6000
257+
assert api.device.called
258+
259+
def test__get_dim_duration_new_lower_than_actual(self, api, entity, device):
260+
device.actual_brightness = NumericValue(
261+
value=65535, maximum=65535, minimum=0, read_only=False
262+
)
263+
api.device.return_value = device
264+
assert entity._get_dim_duration(0) == 6000
265+
assert api.device.called
266+
251267
@patch("asyncio.run_coroutine_threadsafe")
252268
def test_start_dim_device_within_time(self, entity):
253269
current_time = time.time()

0 commit comments

Comments
 (0)