4
0

test_arlec_fan.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from homeassistant.components.fan import (
  2. DIRECTION_FORWARD,
  3. DIRECTION_REVERSE,
  4. SUPPORT_DIRECTION,
  5. SUPPORT_PRESET_MODE,
  6. SUPPORT_SET_SPEED,
  7. )
  8. from homeassistant.const import STATE_UNAVAILABLE
  9. from ..const import ARLEC_FAN_PAYLOAD
  10. from ..helpers import assert_device_properties_set
  11. from .base_device_tests import TuyaDeviceTestCase
  12. SWITCH_DPS = "1"
  13. SPEED_DPS = "3"
  14. DIRECTION_DPS = "4"
  15. PRESET_DPS = "102"
  16. TIMER_DPS = "103"
  17. class TestArlecFan(TuyaDeviceTestCase):
  18. __test__ = True
  19. def setUp(self):
  20. self.setUpForConfig("arlec_fan.yaml", ARLEC_FAN_PAYLOAD)
  21. self.subject = self.entities["fan"]
  22. self.timer = self.entities["select_timer"]
  23. def test_supported_features(self):
  24. self.assertEqual(
  25. self.subject.supported_features,
  26. SUPPORT_DIRECTION | SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED,
  27. )
  28. def test_is_on(self):
  29. self.dps[SWITCH_DPS] = True
  30. self.assertTrue(self.subject.is_on)
  31. self.dps[SWITCH_DPS] = False
  32. self.assertFalse(self.subject.is_on)
  33. self.dps[SWITCH_DPS] = None
  34. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  35. async def test_turn_on(self):
  36. async with assert_device_properties_set(
  37. self.subject._device, {SWITCH_DPS: True}
  38. ):
  39. await self.subject.async_turn_on()
  40. async def test_turn_off(self):
  41. async with assert_device_properties_set(
  42. self.subject._device, {SWITCH_DPS: False}
  43. ):
  44. await self.subject.async_turn_off()
  45. def test_preset_mode(self):
  46. self.dps[PRESET_DPS] = "normal"
  47. self.assertEqual(self.subject.preset_mode, "normal")
  48. self.dps[PRESET_DPS] = "breeze"
  49. self.assertEqual(self.subject.preset_mode, "breeze")
  50. self.dps[PRESET_DPS] = "sleep"
  51. self.assertEqual(self.subject.preset_mode, "sleep")
  52. self.dps[PRESET_DPS] = None
  53. self.assertIs(self.subject.preset_mode, None)
  54. def test_preset_modes(self):
  55. self.assertCountEqual(self.subject.preset_modes, ["normal", "breeze", "sleep"])
  56. async def test_set_preset_mode_to_normal(self):
  57. async with assert_device_properties_set(
  58. self.subject._device,
  59. {PRESET_DPS: "normal"},
  60. ):
  61. await self.subject.async_set_preset_mode("normal")
  62. async def test_set_preset_mode_to_breeze(self):
  63. async with assert_device_properties_set(
  64. self.subject._device,
  65. {PRESET_DPS: "breeze"},
  66. ):
  67. await self.subject.async_set_preset_mode("breeze")
  68. async def test_set_preset_mode_to_sleep(self):
  69. async with assert_device_properties_set(
  70. self.subject._device,
  71. {PRESET_DPS: "sleep"},
  72. ):
  73. await self.subject.async_set_preset_mode("sleep")
  74. def test_direction(self):
  75. self.dps[DIRECTION_DPS] = "forward"
  76. self.assertEqual(self.subject.current_direction, DIRECTION_FORWARD)
  77. self.dps[DIRECTION_DPS] = "reverse"
  78. self.assertEqual(self.subject.current_direction, DIRECTION_REVERSE)
  79. async def test_set_direction_forward(self):
  80. async with assert_device_properties_set(
  81. self.subject._device, {DIRECTION_DPS: "forward"}
  82. ):
  83. await self.subject.async_set_direction(DIRECTION_FORWARD)
  84. async def test_set_direction_reverse(self):
  85. async with assert_device_properties_set(
  86. self.subject._device, {DIRECTION_DPS: "reverse"}
  87. ):
  88. await self.subject.async_set_direction(DIRECTION_REVERSE)
  89. def test_speed(self):
  90. self.dps[SPEED_DPS] = "3"
  91. self.assertEqual(self.subject.percentage, 50)
  92. def test_speed_step(self):
  93. self.assertAlmostEqual(self.subject.percentage_step, 16.67, 2)
  94. self.assertEqual(self.subject.speed_count, 6)
  95. async def test_set_speed(self):
  96. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 2}):
  97. await self.subject.async_set_percentage(33)
  98. async def test_set_speed_in_normal_mode_snaps(self):
  99. self.dps[PRESET_DPS] = "normal"
  100. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 5}):
  101. await self.subject.async_set_percentage(80)
  102. def test_device_state_attributes(self):
  103. self.dps[TIMER_DPS] = "2hour"
  104. self.assertEqual(self.subject.device_state_attributes, {"timer": "2hour"})
  105. self.assertEqual(self.timer.device_state_attributes, {})
  106. def test_timer_options(self):
  107. self.assertCountEqual(
  108. self.timer.options,
  109. {"Off", "2 hours", "4 hours", "8 hours"},
  110. )
  111. def test_timer_current_option(self):
  112. self.dps[TIMER_DPS] = "2hour"
  113. self.assertEqual(self.timer.current_option, "2 hours")
  114. async def test_select_option(self):
  115. async with assert_device_properties_set(
  116. self.timer._device,
  117. {TIMER_DPS: "4hour"},
  118. ):
  119. await self.timer.async_select_option("4 hours")