test_light.py 3.5 KB

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