test_lexy_f501_fan.py 9.2 KB

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