test_light.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Tests for the light entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. from unittest import IsolatedAsyncioTestCase
  4. from unittest.mock import AsyncMock, Mock, patch
  5. from custom_components.tuya_local.const import (
  6. CONF_DISPLAY_LIGHT,
  7. CONF_DEVICE_ID,
  8. CONF_TYPE,
  9. CONF_TYPE_AUTO,
  10. CONF_TYPE_GPPH_HEATER,
  11. DOMAIN,
  12. )
  13. from custom_components.tuya_local.generic.light import TuyaLocalLight
  14. from custom_components.tuya_local.helpers.device_config import config_for_legacy_use
  15. from custom_components.tuya_local.light import async_setup_entry
  16. from .const import GPPH_HEATER_PAYLOAD
  17. from .helpers import assert_device_properties_set
  18. GPPH_LIGHTSWITCH_DPS = "104"
  19. async def test_init_entry(hass):
  20. """Test the initialisation."""
  21. entry = MockConfigEntry(
  22. domain=DOMAIN,
  23. data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
  24. )
  25. # although async, the async_add_entities function passed to
  26. # async_setup_entry is called truly asynchronously. If we use
  27. # AsyncMock, it expects us to await the result.
  28. m_add_entities = Mock()
  29. m_device = AsyncMock()
  30. m_device.async_inferred_type = AsyncMock(return_value=CONF_TYPE_GPPH_HEATER)
  31. hass.data[DOMAIN] = {}
  32. hass.data[DOMAIN]["dummy"] = {}
  33. hass.data[DOMAIN]["dummy"]["device"] = m_device
  34. await async_setup_entry(hass, entry, m_add_entities)
  35. assert type(hass.data[DOMAIN]["dummy"][CONF_DISPLAY_LIGHT]) == TuyaLocalLight
  36. m_add_entities.assert_called_once()
  37. class TestTuyaLocalLight(IsolatedAsyncioTestCase):
  38. def setUp(self):
  39. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  40. self.addCleanup(device_patcher.stop)
  41. self.mock_device = device_patcher.start()
  42. gpph_config = config_for_legacy_use(CONF_TYPE_GPPH_HEATER)
  43. for light in gpph_config.secondary_entities():
  44. if light.entity == "light":
  45. break
  46. self.subject = TuyaLocalLight(self.mock_device(), light)
  47. self.dps = GPPH_HEATER_PAYLOAD.copy()
  48. self.light_name = light.name
  49. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  50. def test_should_poll(self):
  51. self.assertTrue(self.subject.should_poll)
  52. def test_name_returns_device_name(self):
  53. self.assertEqual(self.subject.name, self.subject._device.name)
  54. def test_friendly_name_returns_config_name(self):
  55. self.assertEqual(self.subject.friendly_name, self.light_name)
  56. def test_unique_id_returns_device_unique_id(self):
  57. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  58. def test_device_info_returns_device_info_from_device(self):
  59. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  60. def test_icon(self):
  61. self.dps[GPPH_LIGHTSWITCH_DPS] = True
  62. self.assertEqual(self.subject.icon, "mdi:led-on")
  63. self.dps[GPPH_LIGHTSWITCH_DPS] = False
  64. self.assertEqual(self.subject.icon, "mdi:led-off")
  65. def test_is_on(self):
  66. self.dps[GPPH_LIGHTSWITCH_DPS] = True
  67. self.assertEqual(self.subject.is_on, True)
  68. self.dps[GPPH_LIGHTSWITCH_DPS] = False
  69. self.assertEqual(self.subject.is_on, False)
  70. def test_state_attributes(self):
  71. self.assertEqual(self.subject.device_state_attributes, {})
  72. async def test_turn_on(self):
  73. async with assert_device_properties_set(
  74. self.subject._device, {GPPH_LIGHTSWITCH_DPS: True}
  75. ):
  76. await self.subject.async_turn_on()
  77. async def test_turn_off(self):
  78. async with assert_device_properties_set(
  79. self.subject._device, {GPPH_LIGHTSWITCH_DPS: False}
  80. ):
  81. await self.subject.async_turn_off()
  82. # async def test_toggle_takes_no_action_when_heater_off(self):
  83. # self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  84. # await self.subject.async_toggle()
  85. # self.subject._device.async_set_property.assert_not_called
  86. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  87. # self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  88. self.dps[GPPH_LIGHTSWITCH_DPS] = False
  89. async with assert_device_properties_set(
  90. self.subject._device, {GPPH_LIGHTSWITCH_DPS: True}
  91. ):
  92. await self.subject.async_toggle()
  93. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  94. # self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  95. self.dps[GPPH_LIGHTSWITCH_DPS] = True
  96. async with assert_device_properties_set(
  97. self.subject._device, {GPPH_LIGHTSWITCH_DPS: False}
  98. ):
  99. await self.subject.async_toggle()
  100. async def test_update(self):
  101. result = AsyncMock()
  102. self.subject._device.async_refresh.return_value = result()
  103. await self.subject.async_update()
  104. self.subject._device.async_refresh.assert_called_once()
  105. result.assert_awaited()