test_anko_fan.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.fan import (
  4. SUPPORT_OSCILLATE,
  5. SUPPORT_PRESET_MODE,
  6. SUPPORT_SET_SPEED,
  7. )
  8. from homeassistant.const import STATE_UNAVAILABLE
  9. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  10. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  11. from ..const import ANKO_FAN_PAYLOAD
  12. from ..helpers import assert_device_properties_set
  13. SWITCH_DPS = "1"
  14. PRESET_DPS = "2"
  15. SPEED_DPS = "3"
  16. OSCILLATE_DPS = "4"
  17. TIMER_DPS = "6"
  18. class TestAnkoFan(IsolatedAsyncioTestCase):
  19. def setUp(self):
  20. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  21. self.addCleanup(device_patcher.stop)
  22. self.mock_device = device_patcher.start()
  23. cfg = TuyaDeviceConfig("anko_fan.yaml")
  24. entities = {}
  25. entities[cfg.primary_entity.entity] = cfg.primary_entity
  26. for e in cfg.secondary_entities():
  27. entities[e.entity] = e
  28. self.fan_name = (
  29. "missing" if "fan" not in entities.keys() else entities["fan"].name
  30. )
  31. self.subject = TuyaLocalFan(self.mock_device(), entities.get("fan"))
  32. self.dps = ANKO_FAN_PAYLOAD.copy()
  33. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  34. def test_supported_features(self):
  35. self.assertEqual(
  36. self.subject.supported_features,
  37. SUPPORT_OSCILLATE | SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED,
  38. )
  39. def test_should_poll(self):
  40. self.assertTrue(self.subject.should_poll)
  41. def test_name_returns_device_name(self):
  42. self.assertEqual(self.subject.name, self.subject._device.name)
  43. def test_friendly_name_returns_config_name(self):
  44. self.assertEqual(self.subject.friendly_name, self.fan_name)
  45. def test_unique_id_returns_device_unique_id(self):
  46. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  47. def test_device_info_returns_device_info_from_device(self):
  48. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  49. def test_is_on(self):
  50. self.dps[SWITCH_DPS] = True
  51. self.assertTrue(self.subject.is_on)
  52. self.dps[SWITCH_DPS] = False
  53. self.assertFalse(self.subject.is_on)
  54. self.dps[SWITCH_DPS] = None
  55. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  56. async def test_turn_on(self):
  57. async with assert_device_properties_set(
  58. self.subject._device, {SWITCH_DPS: True}
  59. ):
  60. await self.subject.async_turn_on()
  61. async def test_turn_off(self):
  62. async with assert_device_properties_set(
  63. self.subject._device, {SWITCH_DPS: False}
  64. ):
  65. await self.subject.async_turn_off()
  66. def test_preset_mode(self):
  67. self.dps[PRESET_DPS] = "normal"
  68. self.assertEqual(self.subject.preset_mode, "normal")
  69. self.dps[PRESET_DPS] = "nature"
  70. self.assertEqual(self.subject.preset_mode, "nature")
  71. self.dps[PRESET_DPS] = "sleep"
  72. self.assertEqual(self.subject.preset_mode, "sleep")
  73. self.dps[PRESET_DPS] = None
  74. self.assertIs(self.subject.preset_mode, None)
  75. def test_preset_modes(self):
  76. self.assertCountEqual(self.subject.preset_modes, ["normal", "nature", "sleep"])
  77. async def test_set_preset_mode_to_normal(self):
  78. async with assert_device_properties_set(
  79. self.subject._device,
  80. {PRESET_DPS: "normal"},
  81. ):
  82. await self.subject.async_set_preset_mode("normal")
  83. async def test_set_preset_mode_to_nature(self):
  84. async with assert_device_properties_set(
  85. self.subject._device,
  86. {PRESET_DPS: "nature"},
  87. ):
  88. await self.subject.async_set_preset_mode("nature")
  89. async def test_set_preset_mode_to_sleep(self):
  90. async with assert_device_properties_set(
  91. self.subject._device,
  92. {PRESET_DPS: "sleep"},
  93. ):
  94. await self.subject.async_set_preset_mode("sleep")
  95. def test_oscillating(self):
  96. self.dps[OSCILLATE_DPS] = False
  97. self.assertFalse(self.subject.oscillating)
  98. self.dps[OSCILLATE_DPS] = True
  99. self.assertTrue(self.subject.oscillating)
  100. self.dps[OSCILLATE_DPS] = None
  101. self.assertFalse(self.subject.oscillating)
  102. async def test_oscillate_off(self):
  103. async with assert_device_properties_set(
  104. self.subject._device, {OSCILLATE_DPS: "off"}
  105. ):
  106. await self.subject.async_oscillate(False)
  107. async def test_oscillate_on(self):
  108. async with assert_device_properties_set(
  109. self.subject._device, {OSCILLATE_DPS: "auto"}
  110. ):
  111. await self.subject.async_oscillate(True)
  112. def test_speed(self):
  113. self.dps[PRESET_DPS] = "normal"
  114. self.dps[SPEED_DPS] = "4"
  115. self.assertEqual(self.subject.percentage, 50)
  116. def test_speed_step(self):
  117. self.assertEqual(self.subject.percentage_step, 12.5)
  118. self.assertEqual(self.subject.speed_count, 8)
  119. async def test_set_speed_in_normal_mode(self):
  120. self.dps[PRESET_DPS] = "normal"
  121. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 2}):
  122. await self.subject.async_set_percentage(25)
  123. async def test_set_speed_in_normal_mode_snaps(self):
  124. self.dps[PRESET_DPS] = "normal"
  125. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 6}):
  126. await self.subject.async_set_percentage(80)
  127. def test_device_state_attributes(self):
  128. self.dps[TIMER_DPS] = "5"
  129. self.assertEqual(self.subject.device_state_attributes, {"timer": 5})
  130. async def test_update(self):
  131. result = AsyncMock()
  132. self.subject._device.async_refresh.return_value = result()
  133. await self.subject.async_update()
  134. self.subject._device.async_refresh.assert_called_once()
  135. result.assert_awaited()