test_goldair_fan.py 12 KB

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