|
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 |
|
4 | 4 | import pytest |
| 5 | +import time |
| 6 | +import pytz |
| 7 | +from datetime import datetime |
5 | 8 | from homeassistant.components.light import ( |
6 | 9 | ColorMode, |
7 | 10 | brightness_supported, |
8 | 11 | ) |
9 | 12 | from homeassistant.core import HomeAssistant |
10 | 13 | from onyx_client.data.numeric_value import NumericValue |
| 14 | +from onyx_client.data.animation_value import AnimationValue |
| 15 | +from onyx_client.data.animation_keyframe import AnimationKeyframe |
11 | 16 | from onyx_client.device.light import Light |
12 | 17 | from onyx_client.enum.action import Action |
13 | 18 | from onyx_client.enum.device_type import DeviceType |
@@ -92,6 +97,29 @@ def test_brightness(self, api, entity, device): |
92 | 97 | assert entity.brightness == 25.5 |
93 | 98 | assert api.device.called |
94 | 99 |
|
| 100 | + def test_brightness_with_animation(self, api, entity, device): |
| 101 | + animation = AnimationValue( |
| 102 | + start=0, |
| 103 | + current_value=0, |
| 104 | + keyframes=[ |
| 105 | + AnimationKeyframe( |
| 106 | + interpolation="linear", duration=10, delay=0, value=10 |
| 107 | + ) |
| 108 | + ], |
| 109 | + ) |
| 110 | + device.actual_brightness = NumericValue( |
| 111 | + value=10, |
| 112 | + minimum=0, |
| 113 | + maximum=100, |
| 114 | + read_only=False, |
| 115 | + animation=animation, |
| 116 | + ) |
| 117 | + api.device.return_value = device |
| 118 | + with patch.object(entity, "_start_update_device") as mock_start_update_device: |
| 119 | + assert entity.brightness == 25.5 |
| 120 | + mock_start_update_device.assert_called_with(animation) |
| 121 | + assert api.device.called |
| 122 | + |
95 | 123 | def test_is_on(self, api, entity, device): |
96 | 124 | device.actual_brightness = NumericValue( |
97 | 125 | value=10, minimum=0, maximum=100, read_only=False |
@@ -198,3 +226,57 @@ def test__get_dim_duration_invalid_value(self, api, entity, device): |
198 | 226 | api.device.return_value = device |
199 | 227 | assert entity._get_dim_duration(90) == 5450 |
200 | 228 | assert api.device.called |
| 229 | + |
| 230 | + @patch("asyncio.run_coroutine_threadsafe") |
| 231 | + def test_start_update_device_end(self, api, entity, device): |
| 232 | + current_time = time.mktime(datetime.now(pytz.timezone("UTC")).timetuple()) |
| 233 | + animation = AnimationValue( |
| 234 | + start=current_time - 100, |
| 235 | + current_value=0, |
| 236 | + keyframes=[ |
| 237 | + AnimationKeyframe( |
| 238 | + interpolation="linear", |
| 239 | + value=0, |
| 240 | + duration=10, |
| 241 | + delay=0, |
| 242 | + ) |
| 243 | + ], |
| 244 | + ) |
| 245 | + with patch.object(entity, "_end_update_device") as mock_end_update_device: |
| 246 | + entity._start_update_device(animation) |
| 247 | + mock_end_update_device.assert_called() |
| 248 | + |
| 249 | + @patch("asyncio.run_coroutine_threadsafe") |
| 250 | + def test__end_update_device(self, mock_run_coroutine_threadsafe, api, entity): |
| 251 | + entity._device.actual_brightness.animation = AnimationValue( |
| 252 | + time.time() - 1000, 10, [AnimationKeyframe("linear", 0, 100, 90)] |
| 253 | + ) |
| 254 | + with patch.object(entity, "async_write_ha_state") as mock_async_write_ha_state: |
| 255 | + entity._end_update_device() |
| 256 | + assert mock_async_write_ha_state.called |
| 257 | + assert mock_run_coroutine_threadsafe.called |
| 258 | + api.send_device_command_action.assert_called_with("uuid", Action.STOP) |
| 259 | + |
| 260 | + @patch("asyncio.run_coroutine_threadsafe") |
| 261 | + def test__end_update_device_within_time( |
| 262 | + self, mock_run_coroutine_threadsafe, api, entity |
| 263 | + ): |
| 264 | + entity._device.actual_brightness.animation = AnimationValue( |
| 265 | + time.time(), 10, [AnimationKeyframe("linear", 0, 20000, 90)] |
| 266 | + ) |
| 267 | + with patch.object(entity, "async_write_ha_state") as mock_async_write_ha_state: |
| 268 | + entity._end_update_device() |
| 269 | + assert mock_async_write_ha_state.called |
| 270 | + assert not mock_run_coroutine_threadsafe.called |
| 271 | + |
| 272 | + @patch("asyncio.run_coroutine_threadsafe") |
| 273 | + def test__end_update_device_within_time_using_delay( |
| 274 | + self, mock_run_coroutine_threadsafe, api, entity |
| 275 | + ): |
| 276 | + entity._device.actual_brightness.animation = AnimationValue( |
| 277 | + time.time() - 100, 10, [AnimationKeyframe("linear", 100000, 10, 90)] |
| 278 | + ) |
| 279 | + with patch.object(entity, "async_write_ha_state") as mock_async_write_ha_state: |
| 280 | + entity._end_update_device() |
| 281 | + assert mock_async_write_ha_state.called |
| 282 | + assert not mock_run_coroutine_threadsafe.called |
0 commit comments