4
0

test_light.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """Tests for the light entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from pytest_homeassistant_custom_component.common import MockConfigEntry
  5. from custom_components.tuya_local.const import (
  6. CONF_DEVICE_ID,
  7. CONF_PROTOCOL_VERSION,
  8. CONF_TYPE,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
  12. from custom_components.tuya_local.light import TuyaLocalLight, async_setup_entry
  13. @pytest.mark.asyncio
  14. async def test_init_entry(hass):
  15. """Test the initialisation."""
  16. entry = MockConfigEntry(
  17. domain=DOMAIN,
  18. data={
  19. CONF_TYPE: "goldair_gpph_heater",
  20. CONF_DEVICE_ID: "dummy",
  21. CONF_PROTOCOL_VERSION: "auto",
  22. },
  23. )
  24. # although async, the async_add_entities function passed to
  25. # async_setup_entry is called truly asynchronously. If we use
  26. # AsyncMock, it expects us to await the result.
  27. m_add_entities = Mock()
  28. m_device = AsyncMock()
  29. hass.data[DOMAIN] = {}
  30. hass.data[DOMAIN]["dummy"] = {}
  31. hass.data[DOMAIN]["dummy"]["device"] = m_device
  32. await async_setup_entry(hass, entry, m_add_entities)
  33. assert type(hass.data[DOMAIN]["dummy"]["light_display"]) is TuyaLocalLight
  34. m_add_entities.assert_called_once()
  35. @pytest.mark.asyncio
  36. async def test_init_entry_fails_if_device_has_no_light(hass):
  37. """Test initialisation when device has no matching entity"""
  38. entry = MockConfigEntry(
  39. domain=DOMAIN,
  40. data={
  41. CONF_TYPE: "smartplugv1",
  42. CONF_DEVICE_ID: "dummy",
  43. CONF_PROTOCOL_VERSION: "auto",
  44. },
  45. )
  46. # although async, the async_add_entities function passed to
  47. # async_setup_entry is called truly asynchronously. If we use
  48. # AsyncMock, it expects us to await the result.
  49. m_add_entities = Mock()
  50. m_device = AsyncMock()
  51. hass.data[DOMAIN] = {}
  52. hass.data[DOMAIN]["dummy"] = {}
  53. hass.data[DOMAIN]["dummy"]["device"] = m_device
  54. try:
  55. await async_setup_entry(hass, entry, m_add_entities)
  56. assert False
  57. except ValueError:
  58. pass
  59. m_add_entities.assert_not_called()
  60. @pytest.mark.asyncio
  61. async def test_init_entry_fails_if_config_is_missing(hass):
  62. """Test initialisation when device has no matching entity"""
  63. entry = MockConfigEntry(
  64. domain=DOMAIN,
  65. data={
  66. CONF_TYPE: "non_existing",
  67. CONF_DEVICE_ID: "dummy",
  68. CONF_PROTOCOL_VERSION: "auto",
  69. },
  70. )
  71. # although async, the async_add_entities function passed to
  72. # async_setup_entry is called truly asynchronously. If we use
  73. # AsyncMock, it expects us to await the result.
  74. m_add_entities = Mock()
  75. m_device = AsyncMock()
  76. hass.data[DOMAIN] = {}
  77. hass.data[DOMAIN]["dummy"] = {}
  78. hass.data[DOMAIN]["dummy"]["device"] = m_device
  79. try:
  80. await async_setup_entry(hass, entry, m_add_entities)
  81. assert False
  82. except ValueError:
  83. pass
  84. m_add_entities.assert_not_called()
  85. @pytest.mark.asyncio
  86. async def test_async_turn_on_with_white_param():
  87. """Test using WHITE param for async_turn_on."""
  88. mock_device = AsyncMock()
  89. mock_device.get_property = Mock()
  90. dps = {"1": True, "2": "colour", "3": 1000, "4": "ABCDEFFF"}
  91. mock_device.get_property.side_effect = lambda arg: dps[arg]
  92. mock_config = Mock()
  93. config = TuyaEntityConfig(
  94. mock_config,
  95. {
  96. "entity": "light",
  97. "dps": [
  98. {
  99. "id": "1",
  100. "name": "switch",
  101. "type": "boolean",
  102. },
  103. {
  104. "id": "2",
  105. "name": "color_mode",
  106. "type": "string",
  107. "mapping": [
  108. {
  109. "dps_val": "white",
  110. "value": "white",
  111. },
  112. {
  113. "dps_val": "colour",
  114. "value": "hs",
  115. },
  116. ],
  117. },
  118. {
  119. "id": "3",
  120. "name": "brightness",
  121. "type": "integer",
  122. "range": {
  123. "min": 10,
  124. "max": 1000,
  125. },
  126. },
  127. {
  128. "id": "4",
  129. "name": "hs",
  130. "type": "hex",
  131. },
  132. ],
  133. },
  134. )
  135. light = TuyaLocalLight(mock_device, config)
  136. await light.async_turn_on(white=128)
  137. mock_device.async_set_properties.assert_called_once_with({"2": "white", "3": 506})
  138. @pytest.mark.asyncio
  139. async def test_is_off_when_off_by_brightness():
  140. """Test that the light appears off when turned off by brightness."""
  141. mock_device = AsyncMock()
  142. mock_device.get_property = Mock()
  143. dps = {"1": 0}
  144. mock_device.get_property.side_effect = lambda arg: dps[arg]
  145. mock_config = Mock()
  146. config = TuyaEntityConfig(
  147. mock_config,
  148. {
  149. "entity": "light",
  150. "dps": [
  151. {
  152. "id": "1",
  153. "name": "brightness",
  154. "type": "integer",
  155. "range": {"min": 0, "max": 100},
  156. },
  157. ],
  158. },
  159. )
  160. light = TuyaLocalLight(mock_device, config)
  161. assert light.is_on is False
  162. assert light.brightness == 0