test_light.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from custom_components.goldair_climate.fan.const import (
  4. ATTR_DISPLAY_ON,
  5. ATTR_HVAC_MODE,
  6. PROPERTY_TO_DPS_ID,
  7. )
  8. from custom_components.goldair_climate.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(
  14. "custom_components.goldair_climate.device.GoldairTuyaDevice"
  15. )
  16. self.addCleanup(device_patcher.stop)
  17. self.mock_device = device_patcher.start()
  18. self.subject = GoldairFanLedDisplayLight(self.mock_device())
  19. self.dps = FAN_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_ON]] = True
  31. self.assertEqual(self.subject.icon, "mdi:led-on")
  32. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = False
  33. self.assertEqual(self.subject.icon, "mdi:led-off")
  34. def test_is_on(self):
  35. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = True
  36. self.assertEqual(self.subject.is_on, True)
  37. self.dps[PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]] = False
  38. self.assertEqual(self.subject.is_on, False)
  39. async def test_turn_on(self):
  40. async with assert_device_properties_set(
  41. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: True}
  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_ON]: False}
  47. ):
  48. await self.subject.async_turn_off()
  49. async def test_toggle_takes_no_action_when_fan_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_ON]] = False
  56. async with assert_device_properties_set(
  57. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: True}
  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_ON]] = True
  63. async with assert_device_properties_set(
  64. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_DISPLAY_ON]: False}
  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()