test_goldair_fan.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. from homeassistant.components.climate.const import (
  2. HVAC_MODE_FAN_ONLY,
  3. HVAC_MODE_OFF,
  4. PRESET_ECO,
  5. PRESET_SLEEP,
  6. SUPPORT_FAN_MODE,
  7. SUPPORT_PRESET_MODE as SUPPORT_CLIMATE_PRESET,
  8. SUPPORT_SWING_MODE,
  9. SWING_HORIZONTAL,
  10. SWING_OFF,
  11. )
  12. from homeassistant.components.fan import (
  13. SUPPORT_OSCILLATE,
  14. SUPPORT_PRESET_MODE,
  15. SUPPORT_SET_SPEED,
  16. )
  17. from homeassistant.const import STATE_UNAVAILABLE
  18. from ..const import FAN_PAYLOAD
  19. from ..helpers import assert_device_properties_set
  20. from .base_device_tests import TuyaDeviceTestCase
  21. HVACMODE_DPS = "1"
  22. FANMODE_DPS = "2"
  23. PRESET_DPS = "3"
  24. SWING_DPS = "8"
  25. TIMER_DPS = "11"
  26. LIGHT_DPS = "101"
  27. class TestGoldairFan(TuyaDeviceTestCase):
  28. __test__ = True
  29. def setUp(self):
  30. self.setUpForConfig("goldair_fan.yaml", FAN_PAYLOAD)
  31. self.subject = self.entities.get("fan")
  32. self.climate = self.entities.get("climate")
  33. self.light = self.entities.get("light")
  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. self.assertEqual(
  40. self.climate.supported_features,
  41. SUPPORT_FAN_MODE | SUPPORT_CLIMATE_PRESET | SUPPORT_SWING_MODE,
  42. )
  43. def test_climate_icon_is_fan(self):
  44. self.dps[HVACMODE_DPS] = True
  45. self.assertEqual(self.climate.icon, "mdi:fan")
  46. self.dps[HVACMODE_DPS] = False
  47. self.assertEqual(self.climate.icon, "mdi:fan-off")
  48. def test_temperature_unit_returns_device_temperature_unit(self):
  49. self.assertEqual(
  50. self.climate.temperature_unit, self.climate._device.temperature_unit
  51. )
  52. def test_is_on(self):
  53. self.dps[HVACMODE_DPS] = True
  54. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_FAN_ONLY)
  55. self.assertTrue(self.subject.is_on)
  56. self.dps[HVACMODE_DPS] = False
  57. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
  58. self.assertFalse(self.subject.is_on)
  59. self.dps[HVACMODE_DPS] = None
  60. self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
  61. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  62. def test_climate_hvac_modes(self):
  63. self.assertCountEqual(
  64. self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY]
  65. )
  66. async def test_climate_turn_on(self):
  67. async with assert_device_properties_set(
  68. self.climate._device, {HVACMODE_DPS: True}
  69. ):
  70. await self.climate.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  71. async def test_climate_turn_off(self):
  72. async with assert_device_properties_set(
  73. self.climate._device, {HVACMODE_DPS: False}
  74. ):
  75. await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
  76. async def test_turn_on(self):
  77. async with assert_device_properties_set(
  78. self.subject._device, {HVACMODE_DPS: True}
  79. ):
  80. await self.subject.async_turn_on()
  81. async def test_turn_off(self):
  82. async with assert_device_properties_set(
  83. self.subject._device, {HVACMODE_DPS: False}
  84. ):
  85. await self.subject.async_turn_off()
  86. def test_preset_mode(self):
  87. self.dps[PRESET_DPS] = "normal"
  88. self.assertEqual(self.climate.preset_mode, "normal")
  89. self.assertEqual(self.subject.preset_mode, "normal")
  90. self.dps[PRESET_DPS] = "nature"
  91. self.assertEqual(self.climate.preset_mode, PRESET_ECO)
  92. self.assertEqual(self.subject.preset_mode, "nature")
  93. self.dps[PRESET_DPS] = PRESET_SLEEP
  94. self.assertEqual(self.climate.preset_mode, PRESET_SLEEP)
  95. self.assertEqual(self.subject.preset_mode, PRESET_SLEEP)
  96. self.dps[PRESET_DPS] = None
  97. self.assertIs(self.climate.preset_mode, None)
  98. def test_preset_modes(self):
  99. self.assertCountEqual(
  100. self.climate.preset_modes, ["normal", PRESET_ECO, PRESET_SLEEP]
  101. )
  102. self.assertCountEqual(self.subject.preset_modes, ["normal", "nature", "sleep"])
  103. async def test_set_climate_preset_mode_to_normal(self):
  104. async with assert_device_properties_set(
  105. self.climate._device,
  106. {PRESET_DPS: "normal"},
  107. ):
  108. await self.climate.async_set_preset_mode("normal")
  109. async def test_set_climate_preset_mode_to_eco(self):
  110. async with assert_device_properties_set(
  111. self.climate._device,
  112. {PRESET_DPS: "nature"},
  113. ):
  114. await self.climate.async_set_preset_mode(PRESET_ECO)
  115. async def test_set_climate_preset_mode_to_sleep(self):
  116. async with assert_device_properties_set(
  117. self.climate._device,
  118. {PRESET_DPS: PRESET_SLEEP},
  119. ):
  120. await self.climate.async_set_preset_mode(PRESET_SLEEP)
  121. async def test_set_preset_mode_to_normal(self):
  122. async with assert_device_properties_set(
  123. self.subject._device,
  124. {PRESET_DPS: "normal"},
  125. ):
  126. await self.subject.async_set_preset_mode("normal")
  127. async def test_set_preset_mode_to_nature(self):
  128. async with assert_device_properties_set(
  129. self.subject._device,
  130. {PRESET_DPS: "nature"},
  131. ):
  132. await self.subject.async_set_preset_mode("nature")
  133. async def test_set_preset_mode_to_sleep(self):
  134. async with assert_device_properties_set(
  135. self.subject._device,
  136. {PRESET_DPS: "sleep"},
  137. ):
  138. await self.subject.async_set_preset_mode("sleep")
  139. def test_swing_mode(self):
  140. self.dps[SWING_DPS] = False
  141. self.assertEqual(self.climate.swing_mode, SWING_OFF)
  142. self.assertFalse(self.subject.oscillating)
  143. self.dps[SWING_DPS] = True
  144. self.assertEqual(self.climate.swing_mode, SWING_HORIZONTAL)
  145. self.assertTrue(self.subject.oscillating)
  146. self.dps[SWING_DPS] = None
  147. self.assertIs(self.climate.swing_mode, None)
  148. self.assertFalse(self.subject.oscillating)
  149. def test_swing_modes(self):
  150. self.assertCountEqual(self.climate.swing_modes, [SWING_OFF, SWING_HORIZONTAL])
  151. async def test_climate_set_swing_mode_to_off(self):
  152. async with assert_device_properties_set(
  153. self.climate._device,
  154. {SWING_DPS: False},
  155. ):
  156. await self.climate.async_set_swing_mode(SWING_OFF)
  157. async def test_climate_set_swing_mode_to_horizontal(self):
  158. async with assert_device_properties_set(
  159. self.climate._device,
  160. {SWING_DPS: True},
  161. ):
  162. await self.climate.async_set_swing_mode(SWING_HORIZONTAL)
  163. async def test_oscillate_off(self):
  164. async with assert_device_properties_set(
  165. self.subject._device, {SWING_DPS: False}
  166. ):
  167. await self.subject.async_oscillate(False)
  168. async def test_oscillate_on(self):
  169. async with assert_device_properties_set(
  170. self.subject._device, {SWING_DPS: True}
  171. ):
  172. await self.subject.async_oscillate(True)
  173. def test_speed(self):
  174. self.dps[PRESET_DPS] = "normal"
  175. self.dps[FANMODE_DPS] = 6
  176. self.assertEqual(self.subject.percentage, 50)
  177. async def test_set_speed_in_normal_mode(self):
  178. self.dps[PRESET_DPS] = "normal"
  179. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 3}):
  180. await self.subject.async_set_percentage(25)
  181. async def test_set_speed_in_normal_mode_snaps(self):
  182. self.dps[PRESET_DPS] = "normal"
  183. async with assert_device_properties_set(
  184. self.subject._device, {FANMODE_DPS: 10}
  185. ):
  186. await self.subject.async_set_percentage(80)
  187. async def test_set_speed_in_sleep_mode_snaps(self):
  188. self.dps[PRESET_DPS] = "sleep"
  189. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 8}):
  190. await self.subject.async_set_percentage(75)
  191. def test_climate_fan_modes(self):
  192. self.dps[PRESET_DPS] = "normal"
  193. self.assertCountEqual(self.climate.fan_modes, list(range(1, 13)))
  194. self.dps[PRESET_DPS] = "nature"
  195. self.assertCountEqual(self.climate.fan_modes, ["low", "medium", "high"])
  196. self.dps[PRESET_DPS] = PRESET_SLEEP
  197. self.assertCountEqual(self.climate.fan_modes, ["low", "medium", "high"])
  198. self.dps[PRESET_DPS] = None
  199. self.assertEqual(self.climate.fan_modes, None)
  200. def test_climate_fan_mode_for_normal_preset(self):
  201. self.dps[PRESET_DPS] = "normal"
  202. self.dps[FANMODE_DPS] = 1
  203. self.assertEqual(self.climate.fan_mode, 1)
  204. self.dps[FANMODE_DPS] = 6
  205. self.assertEqual(self.climate.fan_mode, 6)
  206. self.dps[FANMODE_DPS] = 12
  207. self.assertEqual(self.climate.fan_mode, 12)
  208. self.dps[FANMODE_DPS] = None
  209. self.assertEqual(self.climate.fan_mode, None)
  210. async def test_climate_set_fan_mode_for_normal_preset(self):
  211. self.dps[PRESET_DPS] = "normal"
  212. async with assert_device_properties_set(
  213. self.climate._device,
  214. {FANMODE_DPS: 6},
  215. ):
  216. await self.climate.async_set_fan_mode(6)
  217. def test_climate_fan_mode_for_eco_preset(self):
  218. self.dps[PRESET_DPS] = "nature"
  219. self.dps[FANMODE_DPS] = 4
  220. self.assertEqual(self.climate.fan_mode, "low")
  221. self.dps[FANMODE_DPS] = 8
  222. self.assertEqual(self.climate.fan_mode, "medium")
  223. self.dps[FANMODE_DPS] = 12
  224. self.assertEqual(self.climate.fan_mode, "high")
  225. self.dps[FANMODE_DPS] = None
  226. self.assertEqual(self.climate.fan_mode, None)
  227. async def test_climate_set_fan_mode_for_eco_preset(self):
  228. self.dps[PRESET_DPS] = "nature"
  229. async with assert_device_properties_set(
  230. self.climate._device,
  231. {FANMODE_DPS: 4},
  232. ):
  233. await self.climate.async_set_fan_mode("low")
  234. def test_climate_fan_mode_for_sleep_preset(self):
  235. self.dps[PRESET_DPS] = PRESET_SLEEP
  236. self.dps[FANMODE_DPS] = 4
  237. self.assertEqual(self.climate.fan_mode, "low")
  238. self.dps[FANMODE_DPS] = 8
  239. self.assertEqual(self.climate.fan_mode, "medium")
  240. self.dps[FANMODE_DPS] = 12
  241. self.assertEqual(self.climate.fan_mode, "high")
  242. self.dps[FANMODE_DPS] = None
  243. self.assertEqual(self.climate.fan_mode, None)
  244. async def test_climate_set_fan_mode_for_sleep_preset(self):
  245. self.dps[PRESET_DPS] = PRESET_SLEEP
  246. async with assert_device_properties_set(
  247. self.climate._device,
  248. {FANMODE_DPS: 8},
  249. ):
  250. await self.climate.async_set_fan_mode("medium")
  251. def test_device_state_attributes(self):
  252. self.dps[TIMER_DPS] = "5"
  253. self.assertEqual(self.climate.device_state_attributes, {"timer": "5"})
  254. self.assertEqual(self.subject.device_state_attributes, {"timer": "5"})
  255. def test_light_icon(self):
  256. self.dps[LIGHT_DPS] = True
  257. self.assertEqual(self.light.icon, "mdi:led-on")
  258. self.dps[LIGHT_DPS] = False
  259. self.assertEqual(self.light.icon, "mdi:led-off")
  260. def test_light_is_on(self):
  261. self.dps[LIGHT_DPS] = True
  262. self.assertEqual(self.light.is_on, True)
  263. self.dps[LIGHT_DPS] = False
  264. self.assertEqual(self.light.is_on, False)
  265. def test_light_state_attributes(self):
  266. self.assertEqual(self.light.device_state_attributes, {})
  267. async def test_light_turn_on(self):
  268. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  269. await self.light.async_turn_on()
  270. async def test_light_turn_off(self):
  271. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  272. await self.light.async_turn_off()
  273. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  274. self.dps[LIGHT_DPS] = False
  275. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  276. await self.light.async_toggle()
  277. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  278. self.dps[LIGHT_DPS] = True
  279. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  280. await self.light.async_toggle()