test_light.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from custom_components.tuya_local.fan.const import (
  4. ATTR_DISPLAY_ON,
  5. ATTR_HVAC_MODE,
  6. PROPERTY_TO_DPS_ID,
  7. )
  8. from custom_components.tuya_local.fan.light import GoldairFanLedDisplayLight
  9. from ..const import FAN_PAYLOAD
  10. from ..helpers import assert_device_properties_set
  11. class TestGoldairFanLedDisplayLight(IsolatedAsyncioTestCase):
  12. def setUp(self):
  13. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  14. self.addCleanup(device_patcher.stop)
  15. self.mock_device = device_patcher.start()
  16. self.subject = GoldairFanLedDisplayLight(self.mock_device())
  17. self.dps = FAN_PAYLOAD.copy()
  18. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  19. def test_should_poll(self):
  20. self.assertTrue(self.subject.should_poll)
  21. def test_name_returns_device_name(self):
  22. self.assertEqual(self.subject.name, self.subject._device.name)
  23. def test_unique_id_returns_device_unique_id(self):
  24. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  25. def test_device_info_returns_device_info_from_device(self):
  26. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  27. def test_icon(self):
  28. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = True
  29. self.assertEqual(self.subject.icon, "mdi:led-on")
  30. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = False
  31. self.assertEqual(self.subject.icon, "mdi:led-off")
  32. def test_is_on(self):
  33. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = True
  34. self.assertEqual(self.subject.is_on, True)
  35. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = False
  36. self.assertEqual(self.subject.is_on, False)
  37. async def test_turn_on(self):
  38. async with assert_device_properties_set(
  39. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: True}
  40. ):
  41. await self.subject.async_turn_on()
  42. async def test_turn_off(self):
  43. async with assert_device_properties_set(
  44. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: False}
  45. ):
  46. await self.subject.async_turn_off()
  47. async def test_toggle_takes_no_action_when_fan_off(self):
  48. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  49. await self.subject.async_toggle()
  50. self.subject._device.async_set_property.assert_not_called
  51. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  52. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  53. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = False
  54. async with assert_device_properties_set(
  55. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: True}
  56. ):
  57. await self.subject.async_toggle()
  58. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  59. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  60. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = True
  61. async with assert_device_properties_set(
  62. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: False}
  63. ):
  64. await self.subject.async_toggle()
  65. async def test_update(self):
  66. result = AsyncMock()
  67. self.subject._device.async_refresh.return_value = result()
  68. await self.subject.async_update()
  69. self.subject._device.async_refresh.assert_called_once()
  70. result.assert_awaited()