test_light.py 3.5 KB

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