test_goldair_fan.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.assertEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY])
  79. async def test_turn_on(self):
  80. async with assert_device_properties_set(
  81. self.subject._device, {HVACMODE_DPS: True}
  82. ):
  83. await self.subject.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  84. async def test_turn_off(self):
  85. async with assert_device_properties_set(
  86. self.subject._device, {HVACMODE_DPS: False}
  87. ):
  88. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  89. def test_preset_mode(self):
  90. self.dps[PRESET_DPS] = "normal"
  91. self.assertEqual(self.subject.preset_mode, "normal")
  92. self.dps[PRESET_DPS] = "nature"
  93. self.assertEqual(self.subject.preset_mode, PRESET_ECO)
  94. self.dps[PRESET_DPS] = PRESET_SLEEP
  95. self.assertEqual(self.subject.preset_mode, PRESET_SLEEP)
  96. self.dps[PRESET_DPS] = None
  97. self.assertIs(self.subject.preset_mode, None)
  98. def test_preset_modes(self):
  99. self.assertCountEqual(
  100. self.subject.preset_modes, ["normal", PRESET_ECO, PRESET_SLEEP]
  101. )
  102. async def test_set_preset_mode_to_normal(self):
  103. async with assert_device_properties_set(
  104. self.subject._device,
  105. {PRESET_DPS: "normal"},
  106. ):
  107. await self.subject.async_set_preset_mode("normal")
  108. async def test_set_preset_mode_to_eco(self):
  109. async with assert_device_properties_set(
  110. self.subject._device,
  111. {PRESET_DPS: "nature"},
  112. ):
  113. await self.subject.async_set_preset_mode(PRESET_ECO)
  114. async def test_set_preset_mode_to_sleep(self):
  115. async with assert_device_properties_set(
  116. self.subject._device,
  117. {PRESET_DPS: PRESET_SLEEP},
  118. ):
  119. await self.subject.async_set_preset_mode(PRESET_SLEEP)
  120. def test_swing_mode(self):
  121. self.dps[SWING_DPS] = False
  122. self.assertEqual(self.subject.swing_mode, SWING_OFF)
  123. self.dps[SWING_DPS] = True
  124. self.assertEqual(self.subject.swing_mode, SWING_HORIZONTAL)
  125. self.dps[SWING_DPS] = None
  126. self.assertIs(self.subject.swing_mode, None)
  127. def test_swing_modes(self):
  128. self.assertCountEqual(self.subject.swing_modes, [SWING_OFF, SWING_HORIZONTAL])
  129. async def test_set_swing_mode_to_off(self):
  130. async with assert_device_properties_set(
  131. self.subject._device,
  132. {SWING_DPS: False},
  133. ):
  134. await self.subject.async_set_swing_mode(SWING_OFF)
  135. async def test_set_swing_mode_to_horizontal(self):
  136. async with assert_device_properties_set(
  137. self.subject._device,
  138. {SWING_DPS: True},
  139. ):
  140. await self.subject.async_set_swing_mode(SWING_HORIZONTAL)
  141. @skip("Conditions not supported yet")
  142. def test_fan_modes(self):
  143. self.dps[PRESET_DPS] = "normal"
  144. self.assertCountEqual(self.subject.fan_modes, list(range(1, 13)))
  145. self.dps[PRESET_DPS] = "nature"
  146. self.assertCountEqual(self.subject.fan_modes, [1, 2, 3])
  147. self.dps[PRESET_DPS] = PRESET_SLEEP
  148. self.assertCountEqual(self.subject.fan_modes, [1, 2, 3])
  149. self.dps[PRESET_DPS] = None
  150. self.assertEqual(self.subject.fan_modes, [])
  151. @skip("Conditions not supported yet")
  152. def test_fan_mode_for_normal_preset(self):
  153. self.dps[PRESET_DPS] = "normal"
  154. self.dps[FANMODE_DPS] = "1"
  155. self.assertEqual(self.subject.fan_mode, 1)
  156. self.dps[FANMODE_DPS] = "6"
  157. self.assertEqual(self.subject.fan_mode, 6)
  158. self.dps[FANMODE_DPS] = "12"
  159. self.assertEqual(self.subject.fan_mode, 12)
  160. self.dps[FANMODE_DPS] = None
  161. self.assertEqual(self.subject.fan_mode, None)
  162. @skip("Conditions not supported yet")
  163. async def test_set_fan_mode_for_normal_preset(self):
  164. self.dps[PRESET_DPS] = "normal"
  165. async with assert_device_properties_set(
  166. self.subject._device,
  167. {FANMODE_DPS: "6"},
  168. ):
  169. await self.subject.async_set_fan_mode(6)
  170. @skip("Conditions not supported yet")
  171. def test_fan_mode_for_eco_preset(self):
  172. self.dps[PRESET_DPS] = "nature"
  173. self.dps[FANMODE_DPS] = "4"
  174. self.assertEqual(self.subject.fan_mode, 1)
  175. self.dps[FANMODE_DPS] = "8"
  176. self.assertEqual(self.subject.fan_mode, 2)
  177. self.dps[FANMODE_DPS] = "12"
  178. self.assertEqual(self.subject.fan_mode, 3)
  179. self.dps[FANMODE_DPS] = None
  180. self.assertEqual(self.subject.fan_mode, None)
  181. @skip("Conditions not supported yet")
  182. async def test_set_fan_mode_for_eco_preset(self):
  183. self.dps[PRESET_DPS] = "nature"
  184. async with assert_device_properties_set(
  185. self.subject._device,
  186. {FANMODE_DPS: "4"},
  187. ):
  188. await self.subject.async_set_fan_mode(1)
  189. @skip("Conditions not supported yet")
  190. def test_fan_mode_for_sleep_preset(self):
  191. self.dps[PRESET_DPS] = PRESET_SLEEP
  192. self.dps[FANMODE_DPS] = "4"
  193. self.assertEqual(self.subject.fan_mode, 1)
  194. self.dps[FANMODE_DPS] = "8"
  195. self.assertEqual(self.subject.fan_mode, 2)
  196. self.dps[FANMODE_DPS] = "12"
  197. self.assertEqual(self.subject.fan_mode, 3)
  198. self.dps[FANMODE_DPS] = None
  199. self.assertEqual(self.subject.fan_mode, None)
  200. @skip("Conditions not supported yet")
  201. async def test_set_fan_mode_for_sleep_preset(self):
  202. self.dps[PRESET_DPS] = PRESET_SLEEP
  203. async with assert_device_properties_set(
  204. self.subject._device,
  205. {FANMODE_DPS: "8"},
  206. ):
  207. await self.subject.async_set_fan_mode(2)
  208. @skip("Conditions not supported yet")
  209. async def test_set_fan_mode_does_nothing_when_preset_mode_is_not_set(self):
  210. self.dps[PRESET_DPS] = None
  211. with self.assertRaises(
  212. ValueError, msg="Fan mode can only be set when a preset mode is set"
  213. ):
  214. await self.subject.async_set_fan_mode(2)
  215. def test_device_state_attributes(self):
  216. self.dps[UNKNOWN_DPS] = "something"
  217. self.assertEqual(
  218. self.subject.device_state_attributes, {"unknown_11": "something"}
  219. )
  220. async def test_update(self):
  221. result = AsyncMock()
  222. self.subject._device.async_refresh.return_value = result()
  223. await self.subject.async_update()
  224. self.subject._device.async_refresh.assert_called_once()
  225. result.assert_awaited()
  226. def test_light_was_created(self):
  227. self.assertIsInstance(self.light, TuyaLocalLight)
  228. def test_light_is_same_device(self):
  229. self.assertEqual(self.light._device, self.subject._device)
  230. def test_light_icon(self):
  231. self.dps[LIGHT_DPS] = True
  232. self.assertEqual(self.light.icon, "mdi:led-on")
  233. self.dps[LIGHT_DPS] = False
  234. self.assertEqual(self.light.icon, "mdi:led-off")
  235. def test_light_is_on(self):
  236. self.dps[LIGHT_DPS] = True
  237. self.assertEqual(self.light.is_on, True)
  238. self.dps[LIGHT_DPS] = False
  239. self.assertEqual(self.light.is_on, False)
  240. def test_light_state_attributes(self):
  241. self.assertEqual(self.light.device_state_attributes, {})
  242. async def test_light_turn_on(self):
  243. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  244. await self.light.async_turn_on()
  245. async def test_light_turn_off(self):
  246. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  247. await self.light.async_turn_off()
  248. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  249. self.dps[LIGHT_DPS] = False
  250. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  251. await self.light.async_toggle()
  252. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  253. self.dps[LIGHT_DPS] = True
  254. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  255. await self.light.async_toggle()
  256. async def test_light_update(self):
  257. result = AsyncMock()
  258. self.light._device.async_refresh.return_value = result()
  259. await self.light.async_update()
  260. self.light._device.async_refresh.assert_called_once()
  261. result.assert_awaited()