test_goldair_fan.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. def test_climate_icon_is_fan(self):
  86. self.dps[HVACMODE_DPS] = True
  87. self.assertEqual(self.climate.icon, "mdi:fan")
  88. self.dps[HVACMODE_DPS] = False
  89. self.assertEqual(self.climate.icon, "mdi:fan-off")
  90. def test_temperature_unit_returns_device_temperature_unit(self):
  91. self.assertEqual(
  92. self.climate.temperature_unit, self.climate._device.temperature_unit
  93. )
  94. def test_is_on(self):
  95. self.dps[HVACMODE_DPS] = True
  96. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_FAN_ONLY)
  97. self.assertTrue(self.subject.is_on)
  98. self.dps[HVACMODE_DPS] = False
  99. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
  100. self.assertFalse(self.subject.is_on)
  101. self.dps[HVACMODE_DPS] = None
  102. self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
  103. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  104. def test_climate_hvac_modes(self):
  105. self.assertCountEqual(
  106. self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY]
  107. )
  108. async def test_climate_turn_on(self):
  109. async with assert_device_properties_set(
  110. self.climate._device, {HVACMODE_DPS: True}
  111. ):
  112. await self.climate.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  113. async def test_climate_turn_off(self):
  114. async with assert_device_properties_set(
  115. self.climate._device, {HVACMODE_DPS: False}
  116. ):
  117. await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
  118. async def test_turn_on(self):
  119. async with assert_device_properties_set(
  120. self.subject._device, {HVACMODE_DPS: True}
  121. ):
  122. await self.subject.async_turn_on()
  123. async def test_turn_off(self):
  124. async with assert_device_properties_set(
  125. self.subject._device, {HVACMODE_DPS: False}
  126. ):
  127. await self.subject.async_turn_off()
  128. def test_preset_mode(self):
  129. self.dps[PRESET_DPS] = "normal"
  130. self.assertEqual(self.climate.preset_mode, "normal")
  131. self.assertEqual(self.subject.preset_mode, "normal")
  132. self.dps[PRESET_DPS] = "nature"
  133. self.assertEqual(self.climate.preset_mode, PRESET_ECO)
  134. self.assertEqual(self.subject.preset_mode, "nature")
  135. self.dps[PRESET_DPS] = PRESET_SLEEP
  136. self.assertEqual(self.climate.preset_mode, PRESET_SLEEP)
  137. self.assertEqual(self.subject.preset_mode, PRESET_SLEEP)
  138. self.dps[PRESET_DPS] = None
  139. self.assertIs(self.climate.preset_mode, None)
  140. def test_preset_modes(self):
  141. self.assertCountEqual(
  142. self.climate.preset_modes, ["normal", PRESET_ECO, PRESET_SLEEP]
  143. )
  144. self.assertCountEqual(self.subject.preset_modes, ["normal", "nature", "sleep"])
  145. async def test_set_climate_preset_mode_to_normal(self):
  146. async with assert_device_properties_set(
  147. self.climate._device,
  148. {PRESET_DPS: "normal"},
  149. ):
  150. await self.climate.async_set_preset_mode("normal")
  151. async def test_set_climate_preset_mode_to_eco(self):
  152. async with assert_device_properties_set(
  153. self.climate._device,
  154. {PRESET_DPS: "nature"},
  155. ):
  156. await self.climate.async_set_preset_mode(PRESET_ECO)
  157. async def test_set_climate_preset_mode_to_sleep(self):
  158. async with assert_device_properties_set(
  159. self.climate._device,
  160. {PRESET_DPS: PRESET_SLEEP},
  161. ):
  162. await self.climate.async_set_preset_mode(PRESET_SLEEP)
  163. async def test_set_preset_mode_to_normal(self):
  164. async with assert_device_properties_set(
  165. self.subject._device,
  166. {PRESET_DPS: "normal"},
  167. ):
  168. await self.subject.async_set_preset_mode("normal")
  169. async def test_set_preset_mode_to_nature(self):
  170. async with assert_device_properties_set(
  171. self.subject._device,
  172. {PRESET_DPS: "nature"},
  173. ):
  174. await self.subject.async_set_preset_mode("nature")
  175. async def test_set_preset_mode_to_sleep(self):
  176. async with assert_device_properties_set(
  177. self.subject._device,
  178. {PRESET_DPS: "sleep"},
  179. ):
  180. await self.subject.async_set_preset_mode("sleep")
  181. def test_swing_mode(self):
  182. self.dps[SWING_DPS] = False
  183. self.assertEqual(self.climate.swing_mode, SWING_OFF)
  184. self.assertFalse(self.subject.oscillating)
  185. self.dps[SWING_DPS] = True
  186. self.assertEqual(self.climate.swing_mode, SWING_HORIZONTAL)
  187. self.assertTrue(self.subject.oscillating)
  188. self.dps[SWING_DPS] = None
  189. self.assertIs(self.climate.swing_mode, None)
  190. self.assertFalse(self.subject.oscillating)
  191. def test_swing_modes(self):
  192. self.assertCountEqual(self.climate.swing_modes, [SWING_OFF, SWING_HORIZONTAL])
  193. async def test_climate_set_swing_mode_to_off(self):
  194. async with assert_device_properties_set(
  195. self.climate._device,
  196. {SWING_DPS: False},
  197. ):
  198. await self.climate.async_set_swing_mode(SWING_OFF)
  199. async def test_climate_set_swing_mode_to_horizontal(self):
  200. async with assert_device_properties_set(
  201. self.climate._device,
  202. {SWING_DPS: True},
  203. ):
  204. await self.climate.async_set_swing_mode(SWING_HORIZONTAL)
  205. async def test_oscillate_off(self):
  206. async with assert_device_properties_set(
  207. self.subject._device, {SWING_DPS: False}
  208. ):
  209. await self.subject.async_oscillate(False)
  210. async def test_oscillate_on(self):
  211. async with assert_device_properties_set(
  212. self.subject._device, {SWING_DPS: True}
  213. ):
  214. await self.subject.async_oscillate(True)
  215. def test_speed(self):
  216. self.dps[PRESET_DPS] = "normal"
  217. self.dps[FANMODE_DPS] = 6
  218. self.assertEqual(self.subject.percentage, 50)
  219. async def test_set_speed_in_normal_mode(self):
  220. self.dps[PRESET_DPS] = "normal"
  221. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 3}):
  222. await self.subject.async_set_percentage(25)
  223. async def test_set_speed_in_normal_mode_snaps(self):
  224. self.dps[PRESET_DPS] = "normal"
  225. async with assert_device_properties_set(
  226. self.subject._device, {FANMODE_DPS: 10}
  227. ):
  228. await self.subject.async_set_percentage(80)
  229. async def test_set_speed_in_sleep_mode_snaps(self):
  230. self.dps[PRESET_DPS] = "sleep"
  231. async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 8}):
  232. await self.subject.async_set_percentage(75)
  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, ["low", "medium", "high"])
  238. self.dps[PRESET_DPS] = PRESET_SLEEP
  239. self.assertCountEqual(self.climate.fan_modes, ["low", "medium", "high"])
  240. self.dps[PRESET_DPS] = None
  241. self.assertEqual(self.climate.fan_modes, None)
  242. def test_climate_fan_mode_for_normal_preset(self):
  243. self.dps[PRESET_DPS] = "normal"
  244. self.dps[FANMODE_DPS] = 1
  245. self.assertEqual(self.climate.fan_mode, 1)
  246. self.dps[FANMODE_DPS] = 6
  247. self.assertEqual(self.climate.fan_mode, 6)
  248. self.dps[FANMODE_DPS] = 12
  249. self.assertEqual(self.climate.fan_mode, 12)
  250. self.dps[FANMODE_DPS] = None
  251. self.assertEqual(self.climate.fan_mode, None)
  252. async def test_climate_set_fan_mode_for_normal_preset(self):
  253. self.dps[PRESET_DPS] = "normal"
  254. async with assert_device_properties_set(
  255. self.climate._device,
  256. {FANMODE_DPS: 6},
  257. ):
  258. await self.climate.async_set_fan_mode(6)
  259. def test_climate_fan_mode_for_eco_preset(self):
  260. self.dps[PRESET_DPS] = "nature"
  261. self.dps[FANMODE_DPS] = 4
  262. self.assertEqual(self.climate.fan_mode, "low")
  263. self.dps[FANMODE_DPS] = 8
  264. self.assertEqual(self.climate.fan_mode, "medium")
  265. self.dps[FANMODE_DPS] = 12
  266. self.assertEqual(self.climate.fan_mode, "high")
  267. self.dps[FANMODE_DPS] = None
  268. self.assertEqual(self.climate.fan_mode, None)
  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. async def test_climate_set_fan_mode_for_sleep_preset(self):
  287. self.dps[PRESET_DPS] = PRESET_SLEEP
  288. async with assert_device_properties_set(
  289. self.climate._device,
  290. {FANMODE_DPS: 8},
  291. ):
  292. await self.climate.async_set_fan_mode("medium")
  293. def test_device_state_attributes(self):
  294. self.dps[TIMER_DPS] = "5"
  295. self.assertEqual(self.climate.device_state_attributes, {"timer": "5"})
  296. self.assertEqual(self.subject.device_state_attributes, {"timer": "5"})
  297. async def test_update(self):
  298. result = AsyncMock()
  299. self.subject._device.async_refresh.return_value = result()
  300. await self.subject.async_update()
  301. self.subject._device.async_refresh.assert_called_once()
  302. result.assert_awaited()
  303. def test_light_was_created(self):
  304. self.assertIsInstance(self.light, TuyaLocalLight)
  305. def test_light_is_same_device(self):
  306. self.assertEqual(self.light._device, self.subject._device)
  307. def test_light_icon(self):
  308. self.dps[LIGHT_DPS] = True
  309. self.assertEqual(self.light.icon, "mdi:led-on")
  310. self.dps[LIGHT_DPS] = False
  311. self.assertEqual(self.light.icon, "mdi:led-off")
  312. def test_light_is_on(self):
  313. self.dps[LIGHT_DPS] = True
  314. self.assertEqual(self.light.is_on, True)
  315. self.dps[LIGHT_DPS] = False
  316. self.assertEqual(self.light.is_on, False)
  317. def test_light_state_attributes(self):
  318. self.assertEqual(self.light.device_state_attributes, {})
  319. async def test_light_turn_on(self):
  320. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  321. await self.light.async_turn_on()
  322. async def test_light_turn_off(self):
  323. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  324. await self.light.async_turn_off()
  325. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  326. self.dps[LIGHT_DPS] = False
  327. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  328. await self.light.async_toggle()
  329. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  330. self.dps[LIGHT_DPS] = True
  331. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  332. await self.light.async_toggle()
  333. async def test_light_update(self):
  334. result = AsyncMock()
  335. self.light._device.async_refresh.return_value = result()
  336. await self.light.async_update()
  337. self.light._device.async_refresh.assert_called_once()
  338. result.assert_awaited()