test_goldair_fan.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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,
  10. SUPPORT_SWING_MODE,
  11. SWING_HORIZONTAL,
  12. SWING_OFF,
  13. )
  14. from homeassistant.const import STATE_UNAVAILABLE
  15. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  16. from custom_components.tuya_local.generic.light import TuyaLocalLight
  17. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  18. from ..const import FAN_PAYLOAD
  19. from ..helpers import assert_device_properties_set
  20. HVACMODE_DPS = "1"
  21. FANMODE_DPS = "2"
  22. PRESET_DPS = "3"
  23. SWING_DPS = "8"
  24. UNKNOWN_DPS = "11"
  25. LIGHT_DPS = "101"
  26. class TestGoldairFan(IsolatedAsyncioTestCase):
  27. def setUp(self):
  28. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  29. self.addCleanup(device_patcher.stop)
  30. self.mock_device = device_patcher.start()
  31. cfg = TuyaDeviceConfig("goldair_fan.yaml")
  32. climate = cfg.primary_entity
  33. light = None
  34. for e in cfg.secondary_entities():
  35. if e.entity == "light":
  36. light = e
  37. self.climate_name = climate.name
  38. self.light_name = light.name
  39. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  40. self.light = TuyaLocalLight(self.mock_device(), light)
  41. self.dps = FAN_PAYLOAD.copy()
  42. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  43. def test_supported_features(self):
  44. self.assertEqual(
  45. self.subject.supported_features,
  46. SUPPORT_FAN_MODE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE,
  47. )
  48. def test_should_poll(self):
  49. self.assertTrue(self.subject.should_poll)
  50. self.assertTrue(self.light.should_poll)
  51. def test_name_returns_device_name(self):
  52. self.assertEqual(self.subject.name, self.subject._device.name)
  53. self.assertEqual(self.light.name, self.subject._device.name)
  54. def test_friendly_name_returns_config_name(self):
  55. self.assertEqual(self.subject.friendly_name, self.climate_name)
  56. self.assertEqual(self.light.friendly_name, self.light_name)
  57. def test_unique_id_returns_device_unique_id(self):
  58. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  59. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  60. def test_device_info_returns_device_info_from_device(self):
  61. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  62. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  63. @skip("Icon customisation not supported yet")
  64. def test_icon_is_fan(self):
  65. self.assertEqual(self.subject.icon, "mdi:fan")
  66. def test_temperature_unit_returns_device_temperature_unit(self):
  67. self.assertEqual(
  68. self.subject.temperature_unit, self.subject._device.temperature_unit
  69. )
  70. def test_hvac_mode(self):
  71. self.dps[HVACMODE_DPS] = True
  72. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_FAN_ONLY)
  73. self.dps[HVACMODE_DPS] = False
  74. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  75. self.dps[HVACMODE_DPS] = None
  76. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  77. def test_hvac_modes(self):
  78. self.assertCountEqual(
  79. self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY]
  80. )
  81. async def test_turn_on(self):
  82. async with assert_device_properties_set(
  83. self.subject._device, {HVACMODE_DPS: True}
  84. ):
  85. await self.subject.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  86. async def test_turn_off(self):
  87. async with assert_device_properties_set(
  88. self.subject._device, {HVACMODE_DPS: False}
  89. ):
  90. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  91. def test_preset_mode(self):
  92. self.dps[PRESET_DPS] = "normal"
  93. self.assertEqual(self.subject.preset_mode, "normal")
  94. self.dps[PRESET_DPS] = "nature"
  95. self.assertEqual(self.subject.preset_mode, PRESET_ECO)
  96. self.dps[PRESET_DPS] = PRESET_SLEEP
  97. self.assertEqual(self.subject.preset_mode, PRESET_SLEEP)
  98. self.dps[PRESET_DPS] = None
  99. self.assertIs(self.subject.preset_mode, None)
  100. def test_preset_modes(self):
  101. self.assertCountEqual(
  102. self.subject.preset_modes, ["normal", PRESET_ECO, PRESET_SLEEP]
  103. )
  104. async def test_set_preset_mode_to_normal(self):
  105. async with assert_device_properties_set(
  106. self.subject._device,
  107. {PRESET_DPS: "normal"},
  108. ):
  109. await self.subject.async_set_preset_mode("normal")
  110. async def test_set_preset_mode_to_eco(self):
  111. async with assert_device_properties_set(
  112. self.subject._device,
  113. {PRESET_DPS: "nature"},
  114. ):
  115. await self.subject.async_set_preset_mode(PRESET_ECO)
  116. async def test_set_preset_mode_to_sleep(self):
  117. async with assert_device_properties_set(
  118. self.subject._device,
  119. {PRESET_DPS: PRESET_SLEEP},
  120. ):
  121. await self.subject.async_set_preset_mode(PRESET_SLEEP)
  122. def test_swing_mode(self):
  123. self.dps[SWING_DPS] = False
  124. self.assertEqual(self.subject.swing_mode, SWING_OFF)
  125. self.dps[SWING_DPS] = True
  126. self.assertEqual(self.subject.swing_mode, SWING_HORIZONTAL)
  127. self.dps[SWING_DPS] = None
  128. self.assertIs(self.subject.swing_mode, None)
  129. def test_swing_modes(self):
  130. self.assertCountEqual(self.subject.swing_modes, [SWING_OFF, SWING_HORIZONTAL])
  131. async def test_set_swing_mode_to_off(self):
  132. async with assert_device_properties_set(
  133. self.subject._device,
  134. {SWING_DPS: False},
  135. ):
  136. await self.subject.async_set_swing_mode(SWING_OFF)
  137. async def test_set_swing_mode_to_horizontal(self):
  138. async with assert_device_properties_set(
  139. self.subject._device,
  140. {SWING_DPS: True},
  141. ):
  142. await self.subject.async_set_swing_mode(SWING_HORIZONTAL)
  143. @skip("Complex conditions not supported yet")
  144. def test_fan_modes(self):
  145. self.dps[PRESET_DPS] = "normal"
  146. self.assertCountEqual(self.subject.fan_modes, list(range(1, 13)))
  147. self.dps[PRESET_DPS] = "nature"
  148. self.assertCountEqual(self.subject.fan_modes, [1, 2, 3])
  149. self.dps[PRESET_DPS] = PRESET_SLEEP
  150. self.assertCountEqual(self.subject.fan_modes, [1, 2, 3])
  151. self.dps[PRESET_DPS] = None
  152. self.assertEqual(self.subject.fan_modes, [])
  153. @skip("Complex conditions not supported yet")
  154. def test_fan_mode_for_normal_preset(self):
  155. self.dps[PRESET_DPS] = "normal"
  156. self.dps[FANMODE_DPS] = "1"
  157. self.assertEqual(self.subject.fan_mode, 1)
  158. self.dps[FANMODE_DPS] = "6"
  159. self.assertEqual(self.subject.fan_mode, 6)
  160. self.dps[FANMODE_DPS] = "12"
  161. self.assertEqual(self.subject.fan_mode, 12)
  162. self.dps[FANMODE_DPS] = None
  163. self.assertEqual(self.subject.fan_mode, None)
  164. @skip("Complex conditions not supported yet")
  165. async def test_set_fan_mode_for_normal_preset(self):
  166. self.dps[PRESET_DPS] = "normal"
  167. async with assert_device_properties_set(
  168. self.subject._device,
  169. {FANMODE_DPS: "6"},
  170. ):
  171. await self.subject.async_set_fan_mode(6)
  172. @skip("Complex conditions not supported yet")
  173. def test_fan_mode_for_eco_preset(self):
  174. self.dps[PRESET_DPS] = "nature"
  175. self.dps[FANMODE_DPS] = "4"
  176. self.assertEqual(self.subject.fan_mode, 1)
  177. self.dps[FANMODE_DPS] = "8"
  178. self.assertEqual(self.subject.fan_mode, 2)
  179. self.dps[FANMODE_DPS] = "12"
  180. self.assertEqual(self.subject.fan_mode, 3)
  181. self.dps[FANMODE_DPS] = None
  182. self.assertEqual(self.subject.fan_mode, None)
  183. @skip("Complex conditions not supported yet")
  184. async def test_set_fan_mode_for_eco_preset(self):
  185. self.dps[PRESET_DPS] = "nature"
  186. async with assert_device_properties_set(
  187. self.subject._device,
  188. {FANMODE_DPS: "4"},
  189. ):
  190. await self.subject.async_set_fan_mode(1)
  191. @skip("Complex conditions not supported yet")
  192. def test_fan_mode_for_sleep_preset(self):
  193. self.dps[PRESET_DPS] = PRESET_SLEEP
  194. self.dps[FANMODE_DPS] = "4"
  195. self.assertEqual(self.subject.fan_mode, 1)
  196. self.dps[FANMODE_DPS] = "8"
  197. self.assertEqual(self.subject.fan_mode, 2)
  198. self.dps[FANMODE_DPS] = "12"
  199. self.assertEqual(self.subject.fan_mode, 3)
  200. self.dps[FANMODE_DPS] = None
  201. self.assertEqual(self.subject.fan_mode, None)
  202. @skip("Complex conditions not supported yet")
  203. async def test_set_fan_mode_for_sleep_preset(self):
  204. self.dps[PRESET_DPS] = PRESET_SLEEP
  205. async with assert_device_properties_set(
  206. self.subject._device,
  207. {FANMODE_DPS: "8"},
  208. ):
  209. await self.subject.async_set_fan_mode(2)
  210. @skip("Complex conditions not supported yet")
  211. async def test_set_fan_mode_does_nothing_when_preset_mode_is_not_set(self):
  212. self.dps[PRESET_DPS] = None
  213. with self.assertRaises(
  214. ValueError, msg="Fan mode can only be set when a preset mode is set"
  215. ):
  216. await self.subject.async_set_fan_mode(2)
  217. def test_device_state_attributes(self):
  218. self.dps[UNKNOWN_DPS] = "something"
  219. self.assertEqual(
  220. self.subject.device_state_attributes, {"unknown_11": "something"}
  221. )
  222. async def test_update(self):
  223. result = AsyncMock()
  224. self.subject._device.async_refresh.return_value = result()
  225. await self.subject.async_update()
  226. self.subject._device.async_refresh.assert_called_once()
  227. result.assert_awaited()
  228. def test_light_was_created(self):
  229. self.assertIsInstance(self.light, TuyaLocalLight)
  230. def test_light_is_same_device(self):
  231. self.assertEqual(self.light._device, self.subject._device)
  232. def test_light_icon(self):
  233. self.dps[LIGHT_DPS] = True
  234. self.assertEqual(self.light.icon, "mdi:led-on")
  235. self.dps[LIGHT_DPS] = False
  236. self.assertEqual(self.light.icon, "mdi:led-off")
  237. def test_light_is_on(self):
  238. self.dps[LIGHT_DPS] = True
  239. self.assertEqual(self.light.is_on, True)
  240. self.dps[LIGHT_DPS] = False
  241. self.assertEqual(self.light.is_on, False)
  242. def test_light_state_attributes(self):
  243. self.assertEqual(self.light.device_state_attributes, {})
  244. async def test_light_turn_on(self):
  245. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  246. await self.light.async_turn_on()
  247. async def test_light_turn_off(self):
  248. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  249. await self.light.async_turn_off()
  250. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  251. self.dps[LIGHT_DPS] = False
  252. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  253. await self.light.async_toggle()
  254. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  255. self.dps[LIGHT_DPS] = True
  256. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  257. await self.light.async_toggle()
  258. async def test_light_update(self):
  259. result = AsyncMock()
  260. self.light._device.async_refresh.return_value = result()
  261. await self.light.async_update()
  262. self.light._device.async_refresh.assert_called_once()
  263. result.assert_awaited()