|
| 1 | +"""Test for the ONYX Light Entity.""" |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from homeassistant.components.light import ( |
| 6 | + ColorMode, |
| 7 | +) |
| 8 | +from homeassistant.core import HomeAssistant |
| 9 | +from onyx_client.data.numeric_value import NumericValue |
| 10 | +from onyx_client.device.light import Light |
| 11 | +from onyx_client.enum.action import Action |
| 12 | +from onyx_client.enum.device_type import DeviceType |
| 13 | + |
| 14 | +from custom_components.hella_onyx.light import OnyxLight |
| 15 | + |
| 16 | + |
| 17 | +class TestOnyxLight: |
| 18 | + @pytest.fixture |
| 19 | + def api(self): |
| 20 | + yield MagicMock() |
| 21 | + |
| 22 | + @pytest.fixture |
| 23 | + def coordinator(self): |
| 24 | + yield MagicMock() |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def hass(self): |
| 28 | + hass = MagicMock(spec=HomeAssistant) |
| 29 | + hass.loop = MagicMock() |
| 30 | + yield hass |
| 31 | + |
| 32 | + @pytest.fixture |
| 33 | + def entity(self, api, coordinator, hass): |
| 34 | + shutter = OnyxLight( |
| 35 | + api, "UTC", coordinator, "name", DeviceType.BASIC_LIGHT, "uuid" |
| 36 | + ) |
| 37 | + shutter.hass = hass |
| 38 | + yield shutter |
| 39 | + |
| 40 | + @pytest.fixture |
| 41 | + def dimmable_entity(self, api, coordinator): |
| 42 | + yield OnyxLight( |
| 43 | + api, "UTC", coordinator, "name", DeviceType.DIMMABLE_LIGHT, "uuid" |
| 44 | + ) |
| 45 | + |
| 46 | + @pytest.fixture |
| 47 | + def device(self): |
| 48 | + yield Light( |
| 49 | + "id", |
| 50 | + "name", |
| 51 | + DeviceType.BASIC_LIGHT, |
| 52 | + None, |
| 53 | + list(Action), |
| 54 | + ) |
| 55 | + |
| 56 | + def test_icon(self, entity): |
| 57 | + assert entity.icon == "mdi:lightbulb-on-outline" |
| 58 | + |
| 59 | + def test_name(self, entity): |
| 60 | + assert entity.name == "name" |
| 61 | + |
| 62 | + def test_unique_id(self, entity): |
| 63 | + assert entity.unique_id == "uuid/Light" |
| 64 | + |
| 65 | + def test_supported_features(self, entity): |
| 66 | + assert len(entity.supported_features) == 0 |
| 67 | + |
| 68 | + def test_color_mode(self, api, entity, device): |
| 69 | + device.device_type = DeviceType.BASIC_LIGHT |
| 70 | + api.device.return_value = device |
| 71 | + assert entity.color_mode == ColorMode.ONOFF |
| 72 | + assert len(entity.supported_color_modes) == 1 |
| 73 | + assert entity.supported_color_modes[0] == ColorMode.ONOFF |
| 74 | + assert api.device.called |
| 75 | + |
| 76 | + def test_color_mode_brightness(self, api, dimmable_entity, device): |
| 77 | + device.device_type = DeviceType.DIMMABLE_LIGHT |
| 78 | + api.device.return_value = device |
| 79 | + assert dimmable_entity.color_mode == ColorMode.BRIGHTNESS |
| 80 | + assert len(dimmable_entity.supported_color_modes) == 1 |
| 81 | + assert dimmable_entity.supported_color_modes[0] == ColorMode.BRIGHTNESS |
| 82 | + assert api.device.called |
| 83 | + |
| 84 | + def test_brightness(self, api, entity, device): |
| 85 | + device.actual_brightness = NumericValue( |
| 86 | + value=10, minimum=0, maximum=100, read_only=False |
| 87 | + ) |
| 88 | + api.device.return_value = device |
| 89 | + assert entity.brightness == 25.5 |
| 90 | + assert api.device.called |
| 91 | + |
| 92 | + @patch("asyncio.run_coroutine_threadsafe") |
| 93 | + def test_turn_off(self, mock_run_coroutine_threadsafe, api, entity, device): |
| 94 | + device.actual_brightness = NumericValue( |
| 95 | + value=100, maximum=100, minimum=0, read_only=False |
| 96 | + ) |
| 97 | + api.device.return_value = device |
| 98 | + entity.turn_off() |
| 99 | + api.send_device_command_action.assert_called_with("uuid", Action.LIGHT_OFF) |
| 100 | + assert mock_run_coroutine_threadsafe.called |
| 101 | + |
| 102 | + @patch("asyncio.run_coroutine_threadsafe") |
| 103 | + def test_turn_on(self, mock_run_coroutine_threadsafe, api, entity, device): |
| 104 | + device.actual_brightness = NumericValue( |
| 105 | + value=100, maximum=100, minimum=0, read_only=False |
| 106 | + ) |
| 107 | + api.device.return_value = device |
| 108 | + entity.turn_on(brightness=10) |
| 109 | + api.send_device_command_properties.assert_called_with( |
| 110 | + "uuid", |
| 111 | + { |
| 112 | + "target_brightness": 4, |
| 113 | + "dim_duration": 4780, |
| 114 | + }, |
| 115 | + ) |
| 116 | + assert mock_run_coroutine_threadsafe.called |
| 117 | + assert api.device.called |
| 118 | + |
| 119 | + def test__get_dim_duration(self, api, entity, device): |
| 120 | + device.actual_brightness = NumericValue( |
| 121 | + value=100, maximum=100, minimum=0, read_only=False |
| 122 | + ) |
| 123 | + api.device.return_value = device |
| 124 | + assert entity._get_dim_duration(90) == 50 |
| 125 | + assert api.device.called |
| 126 | + |
| 127 | + def test__get_dim_duration_same(self, api, entity, device): |
| 128 | + device.actual_brightness = NumericValue( |
| 129 | + value=100, maximum=100, minimum=0, read_only=False |
| 130 | + ) |
| 131 | + api.device.return_value = device |
| 132 | + assert entity._get_dim_duration(100) == 500 |
| 133 | + assert api.device.called |
0 commit comments