test_light.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_async_turn_on_with_brightness_on_packed_dp():
  140. """Switch-on must merge cleanly when the dp is shared across sub-fields.
  141. On packed dps where switch / brightness / color all share the same dp id
  142. (e.g. dp 51 with different masks), the switch-on branch was previously
  143. skipped once the brightness branch had populated `settings[dp_id]`,
  144. leaving the bulb with brightness staged but not actually on.
  145. For masked switch dps, the merge is always safe — `get_values_to_set`
  146. with `pending_map=settings` ORs onto the existing pending value — so the
  147. final write contains the switch byte AND the brightness bytes.
  148. """
  149. mock_device = AsyncMock()
  150. mock_device.get_property = Mock()
  151. # Bulb currently off: switch byte (mask 0001) is 0, brightness is 0.
  152. dps = {"1": "000000000000"}
  153. mock_device.get_property.side_effect = lambda arg: dps[arg]
  154. mock_config = Mock()
  155. config = TuyaEntityConfig(
  156. mock_config,
  157. {
  158. "entity": "light",
  159. "dps": [
  160. {
  161. "id": "1",
  162. "name": "switch",
  163. "type": "hex",
  164. "mask": "000100000000",
  165. },
  166. {
  167. "id": "1",
  168. "name": "brightness",
  169. "type": "hex",
  170. "mask": "0000FFFF0000",
  171. "range": {"min": 0, "max": 1000},
  172. },
  173. ],
  174. },
  175. )
  176. light = TuyaLocalLight(mock_device, config)
  177. await light.async_turn_on(brightness=255)
  178. mock_device.async_set_properties.assert_called_once()
  179. sent = mock_device.async_set_properties.call_args[0][0]
  180. sent_value = int(sent["1"], 16)
  181. # brightness bytes set
  182. assert sent_value & 0x0000FFFF0000 != 0
  183. # switch byte also set (this is the bug — was zero before the fix)
  184. assert sent_value & 0x000100000000 != 0
  185. @pytest.mark.asyncio
  186. async def test_is_off_when_off_by_brightness():
  187. """Test that the light appears off when turned off by brightness."""
  188. mock_device = AsyncMock()
  189. mock_device.get_property = Mock()
  190. dps = {"1": 0}
  191. mock_device.get_property.side_effect = lambda arg: dps[arg]
  192. mock_config = Mock()
  193. config = TuyaEntityConfig(
  194. mock_config,
  195. {
  196. "entity": "light",
  197. "dps": [
  198. {
  199. "id": "1",
  200. "name": "brightness",
  201. "type": "integer",
  202. "range": {"min": 0, "max": 100},
  203. },
  204. ],
  205. },
  206. )
  207. light = TuyaLocalLight(mock_device, config)
  208. assert light.is_on is False
  209. assert light.brightness == 0