test_goldair_fan.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. async def test_set_speed_in_sleep_mode_snaps(self):
  228. self.dps[PRESET_DPS] = "sleep"
  229. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 8}):
  230. await self.subject.async_set_percentage(75)
  231. @skip("Fan modes does not work without mapping")
  232. def test_climate_fan_modes(self):
  233. self.dps[PRESET_DPS] = "normal"
  234. self.assertCountEqual(self.climate.fan_modes, list(range(1, 13)))
  235. self.dps[PRESET_DPS] = "nature"
  236. self.assertCountEqual(self.climate.fan_modes, [1, 2, 3])
  237. self.dps[PRESET_DPS] = PRESET_SLEEP
  238. self.assertCountEqual(self.climate.fan_modes, [1, 2, 3])
  239. self.dps[PRESET_DPS] = None
  240. self.assertEqual(self.climate.fan_modes, [])
  241. def test_climate_fan_mode_for_normal_preset(self):
  242. self.dps[PRESET_DPS] = "normal"
  243. self.dps[FANMODE_DPS] = 1
  244. self.assertEqual(self.climate.fan_mode, 1)
  245. self.dps[FANMODE_DPS] = 6
  246. self.assertEqual(self.climate.fan_mode, 6)
  247. self.dps[FANMODE_DPS] = 12
  248. self.assertEqual(self.climate.fan_mode, 12)
  249. self.dps[FANMODE_DPS] = None
  250. self.assertEqual(self.climate.fan_mode, None)
  251. async def test_climate_set_fan_mode_for_normal_preset(self):
  252. self.dps[PRESET_DPS] = "normal"
  253. async with assert_device_properties_set(
  254. self.climate._device,
  255. {FANMODE_DPS: 6},
  256. ):
  257. await self.climate.async_set_fan_mode(6)
  258. def test_climate_fan_mode_for_eco_preset(self):
  259. self.dps[PRESET_DPS] = "nature"
  260. self.dps[FANMODE_DPS] = 4
  261. self.assertEqual(self.climate.fan_mode, "low")
  262. self.dps[FANMODE_DPS] = 8
  263. self.assertEqual(self.climate.fan_mode, "medium")
  264. self.dps[FANMODE_DPS] = 12
  265. self.assertEqual(self.climate.fan_mode, "high")
  266. self.dps[FANMODE_DPS] = None
  267. self.assertEqual(self.climate.fan_mode, None)
  268. @skip("Complex conditions not yet supported for setting")
  269. async def test_climate_set_fan_mode_for_eco_preset(self):
  270. self.dps[PRESET_DPS] = "nature"
  271. async with assert_device_properties_set(
  272. self.climate._device,
  273. {FANMODE_DPS: "4"},
  274. ):
  275. await self.climate.async_set_fan_mode("low")
  276. def test_climate_fan_mode_for_sleep_preset(self):
  277. self.dps[PRESET_DPS] = PRESET_SLEEP
  278. self.dps[FANMODE_DPS] = "4"
  279. self.assertEqual(self.climate.fan_mode, "low")
  280. self.dps[FANMODE_DPS] = "8"
  281. self.assertEqual(self.climate.fan_mode, "medium")
  282. self.dps[FANMODE_DPS] = "12"
  283. self.assertEqual(self.climate.fan_mode, "high")
  284. self.dps[FANMODE_DPS] = None
  285. self.assertEqual(self.climate.fan_mode, None)
  286. @skip("Complex conditions not yet supported for setting")
  287. async def test_climate_set_fan_mode_for_sleep_preset(self):
  288. self.dps[PRESET_DPS] = PRESET_SLEEP
  289. async with assert_device_properties_set(
  290. self.climate._device,
  291. {FANMODE_DPS: "8"},
  292. ):
  293. await self.climate.async_set_fan_mode(2)
  294. @skip("Complex conditions not yet supported for setting")
  295. async def test_climate_set_fan_mode_does_nothing_when_preset_mode_is_not_set(self):
  296. self.dps[PRESET_DPS] = None
  297. with self.assertRaises(
  298. ValueError, msg="Fan mode can only be set when a preset mode is set"
  299. ):
  300. await self.climate.async_set_fan_mode(2)
  301. def test_device_state_attributes(self):
  302. self.dps[TIMER_DPS] = "5"
  303. self.assertEqual(self.climate.device_state_attributes, {"timer": "5"})
  304. self.assertEqual(self.subject.device_state_attributes, {"timer": "5"})
  305. async def test_update(self):
  306. result = AsyncMock()
  307. self.subject._device.async_refresh.return_value = result()
  308. await self.subject.async_update()
  309. self.subject._device.async_refresh.assert_called_once()
  310. result.assert_awaited()
  311. def test_light_was_created(self):
  312. self.assertIsInstance(self.light, TuyaLocalLight)
  313. def test_light_is_same_device(self):
  314. self.assertEqual(self.light._device, self.subject._device)
  315. def test_light_icon(self):
  316. self.dps[LIGHT_DPS] = True
  317. self.assertEqual(self.light.icon, "mdi:led-on")
  318. self.dps[LIGHT_DPS] = False
  319. self.assertEqual(self.light.icon, "mdi:led-off")
  320. def test_light_is_on(self):
  321. self.dps[LIGHT_DPS] = True
  322. self.assertEqual(self.light.is_on, True)
  323. self.dps[LIGHT_DPS] = False
  324. self.assertEqual(self.light.is_on, False)
  325. def test_light_state_attributes(self):
  326. self.assertEqual(self.light.device_state_attributes, {})
  327. async def test_light_turn_on(self):
  328. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  329. await self.light.async_turn_on()
  330. async def test_light_turn_off(self):
  331. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  332. await self.light.async_turn_off()
  333. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  334. self.dps[LIGHT_DPS] = False
  335. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  336. await self.light.async_toggle()
  337. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  338. self.dps[LIGHT_DPS] = True
  339. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  340. await self.light.async_toggle()
  341. async def test_light_update(self):
  342. result = AsyncMock()
  343. self.light._device.async_refresh.return_value = result()
  344. await self.light.async_update()
  345. self.light._device.async_refresh.assert_called_once()
  346. result.assert_awaited()