test_lexy_f501_fan.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from homeassistant.components.fan import (
  2. SUPPORT_OSCILLATE,
  3. SUPPORT_PRESET_MODE,
  4. SUPPORT_SET_SPEED,
  5. )
  6. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  7. from homeassistant.const import STATE_UNAVAILABLE
  8. from ..const import LEXY_F501_PAYLOAD
  9. from ..helpers import assert_device_properties_set
  10. from .base_device_tests import TuyaDeviceTestCase
  11. POWER_DPS = "1"
  12. PRESET_DPS = "2"
  13. OSCILLATE_DPS = "4"
  14. TIMER_DPS = "6"
  15. LIGHT_DPS = "9"
  16. LOCK_DPS = "16"
  17. SWITCH_DPS = "17"
  18. SPEED_DPS = "102"
  19. class TestLexyF501Fan(TuyaDeviceTestCase):
  20. __test__ = True
  21. def setUp(self):
  22. self.setUpForConfig("lexy_f501_fan.yaml", LEXY_F501_PAYLOAD)
  23. self.subject = self.entities.get("fan")
  24. self.light = self.entities.get("light")
  25. self.lock = self.entities.get("lock")
  26. self.switch = self.entities.get("switch")
  27. def test_supported_features(self):
  28. self.assertEqual(
  29. self.subject.supported_features,
  30. SUPPORT_OSCILLATE | SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED,
  31. )
  32. def test_is_on(self):
  33. self.dps[POWER_DPS] = True
  34. self.assertTrue(self.subject.is_on)
  35. self.dps[POWER_DPS] = False
  36. self.assertFalse(self.subject.is_on)
  37. self.dps[POWER_DPS] = None
  38. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  39. async def test_turn_on(self):
  40. async with assert_device_properties_set(
  41. self.subject._device, {POWER_DPS: 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, {POWER_DPS: False}
  47. ):
  48. await self.subject.async_turn_off()
  49. def test_preset_mode(self):
  50. self.dps[PRESET_DPS] = "forestwindhigh"
  51. self.assertEqual(self.subject.preset_mode, "Forest High")
  52. self.dps[PRESET_DPS] = "forestwindlow"
  53. self.assertEqual(self.subject.preset_mode, "Forest Low")
  54. self.dps[PRESET_DPS] = "sleepwindlow"
  55. self.assertEqual(self.subject.preset_mode, "Sleep Low")
  56. self.dps[PRESET_DPS] = "sleepwindhigh"
  57. self.assertEqual(self.subject.preset_mode, "Sleep High")
  58. self.dps[PRESET_DPS] = None
  59. self.assertIs(self.subject.preset_mode, None)
  60. def test_preset_modes(self):
  61. self.assertCountEqual(
  62. self.subject.preset_modes,
  63. ["Forest High", "Forest Low", "Sleep High", "Sleep Low"],
  64. )
  65. async def test_set_preset_mode_to_foresthigh(self):
  66. async with assert_device_properties_set(
  67. self.subject._device,
  68. {PRESET_DPS: "forestwindhigh"},
  69. ):
  70. await self.subject.async_set_preset_mode("Forest High")
  71. async def test_set_preset_mode_to_forestlow(self):
  72. async with assert_device_properties_set(
  73. self.subject._device,
  74. {PRESET_DPS: "forestwindlow"},
  75. ):
  76. await self.subject.async_set_preset_mode("Forest Low")
  77. async def test_set_preset_mode_to_sleephigh(self):
  78. async with assert_device_properties_set(
  79. self.subject._device,
  80. {PRESET_DPS: "sleepwindhigh"},
  81. ):
  82. await self.subject.async_set_preset_mode("Sleep High")
  83. async def test_set_preset_mode_to_sleeplow(self):
  84. async with assert_device_properties_set(
  85. self.subject._device,
  86. {PRESET_DPS: "sleepwindlow"},
  87. ):
  88. await self.subject.async_set_preset_mode("Sleep Low")
  89. def test_oscillating(self):
  90. self.dps[OSCILLATE_DPS] = "off"
  91. self.assertFalse(self.subject.oscillating)
  92. self.dps[OSCILLATE_DPS] = "30"
  93. self.assertTrue(self.subject.oscillating)
  94. self.dps[OSCILLATE_DPS] = "60"
  95. self.assertTrue(self.subject.oscillating)
  96. self.dps[OSCILLATE_DPS] = "90"
  97. self.assertTrue(self.subject.oscillating)
  98. self.dps[OSCILLATE_DPS] = "360positive"
  99. self.assertTrue(self.subject.oscillating)
  100. self.dps[OSCILLATE_DPS] = "360negative"
  101. self.assertTrue(self.subject.oscillating)
  102. self.dps[OSCILLATE_DPS] = None
  103. self.assertFalse(self.subject.oscillating)
  104. async def test_oscillate_off(self):
  105. async with assert_device_properties_set(
  106. self.subject._device, {OSCILLATE_DPS: "off"}
  107. ):
  108. await self.subject.async_oscillate(False)
  109. def test_speed(self):
  110. self.dps[SPEED_DPS] = "6"
  111. self.assertEqual(self.subject.percentage, 40)
  112. def test_speed_step(self):
  113. self.assertAlmostEqual(self.subject.percentage_step, 6.7, 1)
  114. self.assertEqual(self.subject.speed_count, 15)
  115. async def test_set_speed(self):
  116. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 3}):
  117. await self.subject.async_set_percentage(20)
  118. async def test_set_speed_snaps(self):
  119. self.dps[PRESET_DPS] = "normal"
  120. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 12}):
  121. await self.subject.async_set_percentage(78)
  122. def test_device_state_attributes(self):
  123. self.dps[TIMER_DPS] = "5"
  124. self.assertEqual(self.subject.device_state_attributes, {"timer": 5})
  125. def test_light_is_on(self):
  126. self.dps[LIGHT_DPS] = True
  127. self.assertEqual(self.light.is_on, True)
  128. self.dps[LIGHT_DPS] = False
  129. self.assertEqual(self.light.is_on, False)
  130. def test_light_state_attributes(self):
  131. self.assertEqual(self.light.device_state_attributes, {})
  132. async def test_light_turn_on(self):
  133. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  134. await self.light.async_turn_on()
  135. async def test_light_turn_off(self):
  136. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  137. await self.light.async_turn_off()
  138. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  139. self.dps[LIGHT_DPS] = False
  140. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  141. await self.light.async_toggle()
  142. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  143. self.dps[LIGHT_DPS] = True
  144. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  145. await self.light.async_toggle()
  146. def test_switch_is_on(self):
  147. self.dps[SWITCH_DPS] = True
  148. self.assertEqual(self.switch.is_on, True)
  149. self.dps[SWITCH_DPS] = False
  150. self.assertEqual(self.switch.is_on, False)
  151. def test_switch_state_attributes(self):
  152. self.assertEqual(self.switch.device_state_attributes, {})
  153. async def test_switch_turn_on(self):
  154. async with assert_device_properties_set(
  155. self.switch._device, {SWITCH_DPS: True}
  156. ):
  157. await self.switch.async_turn_on()
  158. async def test_switch_turn_off(self):
  159. async with assert_device_properties_set(
  160. self.switch._device, {SWITCH_DPS: False}
  161. ):
  162. await self.switch.async_turn_off()
  163. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  164. self.dps[SWITCH_DPS] = False
  165. async with assert_device_properties_set(
  166. self.switch._device, {SWITCH_DPS: True}
  167. ):
  168. await self.switch.async_toggle()
  169. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  170. self.dps[SWITCH_DPS] = True
  171. async with assert_device_properties_set(
  172. self.switch._device, {SWITCH_DPS: False}
  173. ):
  174. await self.switch.async_toggle()
  175. def test_lock_state(self):
  176. self.dps[LOCK_DPS] = True
  177. self.assertEqual(self.lock.state, STATE_LOCKED)
  178. self.dps[LOCK_DPS] = False
  179. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  180. self.dps[LOCK_DPS] = None
  181. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  182. def test_lock_is_locked(self):
  183. self.dps[LOCK_DPS] = True
  184. self.assertTrue(self.lock.is_locked)
  185. self.dps[LOCK_DPS] = False
  186. self.assertFalse(self.lock.is_locked)
  187. self.dps[LOCK_DPS] = None
  188. self.assertFalse(self.lock.is_locked)
  189. async def test_lock_locks(self):
  190. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  191. await self.lock.async_lock()
  192. async def test_lock_unlocks(self):
  193. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  194. await self.lock.async_unlock()
  195. def test_icons(self):
  196. self.dps[LIGHT_DPS] = True
  197. self.assertEqual(self.light.icon, "mdi:led-on")
  198. self.dps[LIGHT_DPS] = False
  199. self.assertEqual(self.light.icon, "mdi:led-off")