test_goldair_fan.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. HVAC_MODE_FAN_ONLY,
  5. HVAC_MODE_OFF,
  6. PRESET_ECO,
  7. PRESET_SLEEP,
  8. SUPPORT_FAN_MODE,
  9. SUPPORT_PRESET_MODE as SUPPORT_CLIMATE_PRESET,
  10. SUPPORT_SWING_MODE,
  11. SWING_HORIZONTAL,
  12. SWING_OFF,
  13. )
  14. from homeassistant.components.fan import (
  15. SUPPORT_OSCILLATE,
  16. SUPPORT_PRESET_MODE,
  17. SUPPORT_SET_SPEED,
  18. )
  19. from homeassistant.const import STATE_UNAVAILABLE
  20. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  21. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  22. from custom_components.tuya_local.generic.light import TuyaLocalLight
  23. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  24. from ..const import FAN_PAYLOAD
  25. from ..helpers import assert_device_properties_set
  26. HVACMODE_DPS = "1"
  27. FANMODE_DPS = "2"
  28. PRESET_DPS = "3"
  29. SWING_DPS = "8"
  30. TIMER_DPS = "11"
  31. LIGHT_DPS = "101"
  32. class TestGoldairFan(IsolatedAsyncioTestCase):
  33. def setUp(self):
  34. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  35. self.addCleanup(device_patcher.stop)
  36. self.mock_device = device_patcher.start()
  37. cfg = TuyaDeviceConfig("goldair_fan.yaml")
  38. entities = {}
  39. entities[cfg.primary_entity.entity] = cfg.primary_entity
  40. for e in cfg.secondary_entities():
  41. entities[e.entity] = e
  42. self.climate_name = (
  43. "missing" if "climate" not in entities.keys() else entities["climate"].name
  44. )
  45. self.fan_name = (
  46. "missing" if "fan" not in entities.keys() else entities["fan"].name
  47. )
  48. self.light_name = (
  49. "missing" if "light" not in entities.keys() else entities["light"].name
  50. )
  51. self.subject = TuyaLocalFan(self.mock_device(), entities.get("fan"))
  52. self.climate = TuyaLocalClimate(self.mock_device(), entities.get("climate"))
  53. self.light = TuyaLocalLight(self.mock_device(), entities.get("light"))
  54. self.dps = FAN_PAYLOAD.copy()
  55. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  56. def test_supported_features(self):
  57. self.assertEqual(
  58. self.subject.supported_features,
  59. SUPPORT_OSCILLATE | SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED,
  60. )
  61. self.assertEqual(
  62. self.climate.supported_features,
  63. SUPPORT_FAN_MODE | SUPPORT_CLIMATE_PRESET | SUPPORT_SWING_MODE,
  64. )
  65. def test_should_poll(self):
  66. self.assertTrue(self.subject.should_poll)
  67. self.assertTrue(self.climate.should_poll)
  68. self.assertTrue(self.light.should_poll)
  69. def test_name_returns_device_name(self):
  70. self.assertEqual(self.subject.name, self.subject._device.name)
  71. self.assertEqual(self.climate.name, self.subject._device.name)
  72. self.assertEqual(self.light.name, self.subject._device.name)
  73. def test_friendly_name_returns_config_name(self):
  74. self.assertEqual(self.subject.friendly_name, self.fan_name)
  75. self.assertEqual(self.climate.friendly_name, self.climate_name)
  76. self.assertEqual(self.light.friendly_name, self.light_name)
  77. def test_unique_id_returns_device_unique_id(self):
  78. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  79. self.assertEqual(self.climate.unique_id, self.subject._device.unique_id)
  80. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  81. def test_device_info_returns_device_info_from_device(self):
  82. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  83. self.assertEqual(self.climate.device_info, self.subject._device.device_info)
  84. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  85. @skip("Icon customisation not supported yet")
  86. def test_climate_icon_is_fan(self):
  87. self.assertEqual(self.climate.icon, "mdi:fan")
  88. def test_temperature_unit_returns_device_temperature_unit(self):
  89. self.assertEqual(
  90. self.climate.temperature_unit, self.climate._device.temperature_unit
  91. )
  92. def test_is_on(self):
  93. self.dps[HVACMODE_DPS] = True
  94. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_FAN_ONLY)
  95. self.assertTrue(self.subject.is_on)
  96. self.dps[HVACMODE_DPS] = False
  97. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
  98. self.assertFalse(self.subject.is_on)
  99. self.dps[HVACMODE_DPS] = None
  100. self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
  101. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  102. def test_climate_hvac_modes(self):
  103. self.assertCountEqual(
  104. self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY]
  105. )
  106. async def test_climate_turn_on(self):
  107. async with assert_device_properties_set(
  108. self.climate._device, {HVACMODE_DPS: True}
  109. ):
  110. await self.climate.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  111. async def test_climate_turn_off(self):
  112. async with assert_device_properties_set(
  113. self.climate._device, {HVACMODE_DPS: False}
  114. ):
  115. await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
  116. async def test_turn_on(self):
  117. async with assert_device_properties_set(
  118. self.subject._device, {HVACMODE_DPS: True}
  119. ):
  120. await self.subject.async_turn_on()
  121. async def test_turn_off(self):
  122. async with assert_device_properties_set(
  123. self.subject._device, {HVACMODE_DPS: False}
  124. ):
  125. await self.subject.async_turn_off()
  126. def test_preset_mode(self):
  127. self.dps[PRESET_DPS] = "normal"
  128. self.assertEqual(self.climate.preset_mode, "normal")
  129. self.assertEqual(self.subject.preset_mode, "normal")
  130. self.dps[PRESET_DPS] = "nature"
  131. self.assertEqual(self.climate.preset_mode, PRESET_ECO)
  132. self.assertEqual(self.subject.preset_mode, "nature")
  133. self.dps[PRESET_DPS] = PRESET_SLEEP
  134. self.assertEqual(self.climate.preset_mode, PRESET_SLEEP)
  135. self.assertEqual(self.subject.preset_mode, PRESET_SLEEP)
  136. self.dps[PRESET_DPS] = None
  137. self.assertIs(self.climate.preset_mode, None)
  138. def test_preset_modes(self):
  139. self.assertCountEqual(
  140. self.climate.preset_modes, ["normal", PRESET_ECO, PRESET_SLEEP]
  141. )
  142. self.assertCountEqual(self.subject.preset_modes, ["normal", "nature", "sleep"])
  143. async def test_set_climate_preset_mode_to_normal(self):
  144. async with assert_device_properties_set(
  145. self.climate._device,
  146. {PRESET_DPS: "normal"},
  147. ):
  148. await self.climate.async_set_preset_mode("normal")
  149. async def test_set_climate_preset_mode_to_eco(self):
  150. async with assert_device_properties_set(
  151. self.climate._device,
  152. {PRESET_DPS: "nature"},
  153. ):
  154. await self.climate.async_set_preset_mode(PRESET_ECO)
  155. async def test_set_climate_preset_mode_to_sleep(self):
  156. async with assert_device_properties_set(
  157. self.climate._device,
  158. {PRESET_DPS: PRESET_SLEEP},
  159. ):
  160. await self.climate.async_set_preset_mode(PRESET_SLEEP)
  161. async def test_set_preset_mode_to_normal(self):
  162. async with assert_device_properties_set(
  163. self.subject._device,
  164. {PRESET_DPS: "normal"},
  165. ):
  166. await self.subject.async_set_preset_mode("normal")
  167. async def test_set_preset_mode_to_nature(self):
  168. async with assert_device_properties_set(
  169. self.subject._device,
  170. {PRESET_DPS: "nature"},
  171. ):
  172. await self.subject.async_set_preset_mode("nature")
  173. async def test_set_preset_mode_to_sleep(self):
  174. async with assert_device_properties_set(
  175. self.subject._device,
  176. {PRESET_DPS: "sleep"},
  177. ):
  178. await self.subject.async_set_preset_mode("sleep")
  179. def test_swing_mode(self):
  180. self.dps[SWING_DPS] = False
  181. self.assertEqual(self.climate.swing_mode, SWING_OFF)
  182. self.assertFalse(self.subject.oscillating)
  183. self.dps[SWING_DPS] = True
  184. self.assertEqual(self.climate.swing_mode, SWING_HORIZONTAL)
  185. self.assertTrue(self.subject.oscillating)
  186. self.dps[SWING_DPS] = None
  187. self.assertIs(self.climate.swing_mode, None)
  188. self.assertFalse(self.subject.oscillating)
  189. def test_swing_modes(self):
  190. self.assertCountEqual(self.climate.swing_modes, [SWING_OFF, SWING_HORIZONTAL])
  191. async def test_climate_set_swing_mode_to_off(self):
  192. async with assert_device_properties_set(
  193. self.climate._device,
  194. {SWING_DPS: False},
  195. ):
  196. await self.climate.async_set_swing_mode(SWING_OFF)
  197. async def test_climate_set_swing_mode_to_horizontal(self):
  198. async with assert_device_properties_set(
  199. self.climate._device,
  200. {SWING_DPS: True},
  201. ):
  202. await self.climate.async_set_swing_mode(SWING_HORIZONTAL)
  203. async def test_oscillate_off(self):
  204. async with assert_device_properties_set(
  205. self.subject._device, {SWING_DPS: False}
  206. ):
  207. await self.subject.async_oscillate(False)
  208. async def test_oscillate_on(self):
  209. async with assert_device_properties_set(
  210. self.subject._device, {SWING_DPS: True}
  211. ):
  212. await self.subject.async_oscillate(True)
  213. def test_speed(self):
  214. self.dps[PRESET_DPS] = "normal"
  215. self.dps[FANMODE_DPS] = 6
  216. self.assertEqual(self.subject.percentage, 50)
  217. async def test_set_speed_in_normal_mode(self):
  218. self.dps[PRESET_DPS] = "normal"
  219. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 3}):
  220. await self.subject.async_set_percentage(25)
  221. async def test_set_speed_in_normal_mode_snaps(self):
  222. self.dps[PRESET_DPS] = "normal"
  223. async with assert_device_properties_set(
  224. self.subject._device, {FANMODE_DPS: 10}
  225. ):
  226. await self.subject.async_set_percentage(80)
  227. @skip("Complex conditions not supported yet")
  228. async def test_set_speed_in_sleep_mode_snaps(self):
  229. self.dps[PRESET_DPS] = "sleep"
  230. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 8}):
  231. await self.subject.async_set_percentage(75)
  232. @skip("Complex conditions not supported yet")
  233. def test_climate_fan_modes(self):
  234. self.dps[PRESET_DPS] = "normal"
  235. self.assertCountEqual(self.climate.fan_modes, list(range(1, 13)))
  236. self.dps[PRESET_DPS] = "nature"
  237. self.assertCountEqual(self.climate.fan_modes, [1, 2, 3])
  238. self.dps[PRESET_DPS] = PRESET_SLEEP
  239. self.assertCountEqual(self.climate.fan_modes, [1, 2, 3])
  240. self.dps[PRESET_DPS] = None
  241. self.assertEqual(self.climate.fan_modes, [])
  242. @skip("Complex conditions not supported yet")
  243. def test_climate_fan_mode_for_normal_preset(self):
  244. self.dps[PRESET_DPS] = "normal"
  245. self.dps[FANMODE_DPS] = "1"
  246. self.assertEqual(self.climate.fan_mode, 1)
  247. self.dps[FANMODE_DPS] = "6"
  248. self.assertEqual(self.climate.fan_mode, 6)
  249. self.dps[FANMODE_DPS] = "12"
  250. self.assertEqual(self.climate.fan_mode, 12)
  251. self.dps[FANMODE_DPS] = None
  252. self.assertEqual(self.climate.fan_mode, None)
  253. @skip("Complex conditions not supported yet")
  254. async def test_climate_set_fan_mode_for_normal_preset(self):
  255. self.dps[PRESET_DPS] = "normal"
  256. async with assert_device_properties_set(
  257. self.climate._device,
  258. {FANMODE_DPS: "6"},
  259. ):
  260. await self.climate.async_set_fan_mode(6)
  261. @skip("Complex conditions not supported yet")
  262. def test_climate_fan_mode_for_eco_preset(self):
  263. self.dps[PRESET_DPS] = "nature"
  264. self.dps[FANMODE_DPS] = "4"
  265. self.assertEqual(self.climate.fan_mode, 1)
  266. self.dps[FANMODE_DPS] = "8"
  267. self.assertEqual(self.climate.fan_mode, 2)
  268. self.dps[FANMODE_DPS] = "12"
  269. self.assertEqual(self.climate.fan_mode, 3)
  270. self.dps[FANMODE_DPS] = None
  271. self.assertEqual(self.climate.fan_mode, None)
  272. @skip("Complex conditions not supported yet")
  273. async def test_climate_set_fan_mode_for_eco_preset(self):
  274. self.dps[PRESET_DPS] = "nature"
  275. async with assert_device_properties_set(
  276. self.climate._device,
  277. {FANMODE_DPS: "4"},
  278. ):
  279. await self.climate.async_set_fan_mode(1)
  280. @skip("Complex conditions not supported yet")
  281. def test_climate_fan_mode_for_sleep_preset(self):
  282. self.dps[PRESET_DPS] = PRESET_SLEEP
  283. self.dps[FANMODE_DPS] = "4"
  284. self.assertEqual(self.climate.fan_mode, 1)
  285. self.dps[FANMODE_DPS] = "8"
  286. self.assertEqual(self.climate.fan_mode, 2)
  287. self.dps[FANMODE_DPS] = "12"
  288. self.assertEqual(self.climate.fan_mode, 3)
  289. self.dps[FANMODE_DPS] = None
  290. self.assertEqual(self.climate.fan_mode, None)
  291. @skip("Complex conditions not supported yet")
  292. async def test_climate_set_fan_mode_for_sleep_preset(self):
  293. self.dps[PRESET_DPS] = PRESET_SLEEP
  294. async with assert_device_properties_set(
  295. self.climate._device,
  296. {FANMODE_DPS: "8"},
  297. ):
  298. await self.climate.async_set_fan_mode(2)
  299. @skip("Complex conditions not supported yet")
  300. async def test_climate_set_fan_mode_does_nothing_when_preset_mode_is_not_set(self):
  301. self.dps[PRESET_DPS] = None
  302. with self.assertRaises(
  303. ValueError, msg="Fan mode can only be set when a preset mode is set"
  304. ):
  305. await self.climate.async_set_fan_mode(2)
  306. def test_device_state_attributes(self):
  307. self.dps[TIMER_DPS] = "5"
  308. self.assertEqual(self.climate.device_state_attributes, {"timer": "5"})
  309. self.assertEqual(self.climate.device_state_attributes, {"timer": "5"})
  310. async def test_update(self):
  311. result = AsyncMock()
  312. self.subject._device.async_refresh.return_value = result()
  313. await self.subject.async_update()
  314. self.subject._device.async_refresh.assert_called_once()
  315. result.assert_awaited()
  316. def test_light_was_created(self):
  317. self.assertIsInstance(self.light, TuyaLocalLight)
  318. def test_light_is_same_device(self):
  319. self.assertEqual(self.light._device, self.subject._device)
  320. def test_light_icon(self):
  321. self.dps[LIGHT_DPS] = True
  322. self.assertEqual(self.light.icon, "mdi:led-on")
  323. self.dps[LIGHT_DPS] = False
  324. self.assertEqual(self.light.icon, "mdi:led-off")
  325. def test_light_is_on(self):
  326. self.dps[LIGHT_DPS] = True
  327. self.assertEqual(self.light.is_on, True)
  328. self.dps[LIGHT_DPS] = False
  329. self.assertEqual(self.light.is_on, False)
  330. def test_light_state_attributes(self):
  331. self.assertEqual(self.light.device_state_attributes, {})
  332. async def test_light_turn_on(self):
  333. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  334. await self.light.async_turn_on()
  335. async def test_light_turn_off(self):
  336. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  337. await self.light.async_turn_off()
  338. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  339. self.dps[LIGHT_DPS] = False
  340. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  341. await self.light.async_toggle()
  342. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  343. self.dps[LIGHT_DPS] = True
  344. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  345. await self.light.async_toggle()
  346. async def test_light_update(self):
  347. result = AsyncMock()
  348. self.light._device.async_refresh.return_value = result()
  349. await self.light.async_update()
  350. self.light._device.async_refresh.assert_called_once()
  351. result.assert_awaited()