test_goldair_gpph_heater.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. HVAC_MODE_HEAT,
  5. HVAC_MODE_OFF,
  6. SUPPORT_PRESET_MODE,
  7. SUPPORT_SWING_MODE,
  8. SUPPORT_TARGET_TEMPERATURE,
  9. )
  10. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  11. from homeassistant.const import STATE_UNAVAILABLE
  12. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  13. from custom_components.tuya_local.generic.light import TuyaLocalLight
  14. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  15. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  16. from ..const import GPPH_HEATER_PAYLOAD
  17. from ..helpers import assert_device_properties_set
  18. HVACMODE_DPS = "1"
  19. TEMPERATURE_DPS = "2"
  20. CURRENTTEMP_DPS = "3"
  21. PRESET_DPS = "4"
  22. LOCK_DPS = "6"
  23. ERROR_DPS = "12"
  24. POWERLEVEL_DPS = "101"
  25. TIMER_DPS = "102"
  26. TIMERACT_DPS = "103"
  27. LIGHT_DPS = "104"
  28. SWING_DPS = "105"
  29. ECOTEMP_DPS = "106"
  30. class TestGoldairHeater(IsolatedAsyncioTestCase):
  31. def setUp(self):
  32. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  33. self.addCleanup(device_patcher.stop)
  34. self.mock_device = device_patcher.start()
  35. cfg = TuyaDeviceConfig("goldair_gpph_heater.yaml")
  36. climate = cfg.primary_entity
  37. light = None
  38. lock = None
  39. for e in cfg.secondary_entities():
  40. if e.entity == "light":
  41. light = e
  42. elif e.entity == "lock":
  43. lock = e
  44. self.climate_name = climate.name
  45. self.light_name = "missing" if light is None else light.name
  46. self.lock_name = "missing" if lock is None else lock.name
  47. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  48. self.light = TuyaLocalLight(self.mock_device(), light)
  49. self.lock = TuyaLocalLock(self.mock_device(), lock)
  50. self.dps = GPPH_HEATER_PAYLOAD.copy()
  51. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  52. def test_supported_features(self):
  53. self.assertEqual(
  54. self.subject.supported_features,
  55. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE,
  56. )
  57. def test_should_poll(self):
  58. self.assertTrue(self.subject.should_poll)
  59. self.assertTrue(self.light.should_poll)
  60. self.assertTrue(self.lock.should_poll)
  61. def test_name_returns_device_name(self):
  62. self.assertEqual(self.subject.name, self.subject._device.name)
  63. self.assertEqual(self.light.name, self.subject._device.name)
  64. self.assertEqual(self.lock.name, self.subject._device.name)
  65. def test_friendly_name_returns_config_name(self):
  66. self.assertEqual(self.subject.friendly_name, self.climate_name)
  67. self.assertEqual(self.light.friendly_name, self.light_name)
  68. self.assertEqual(self.lock.friendly_name, self.lock_name)
  69. def test_unique_id_returns_device_unique_id(self):
  70. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  71. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  72. self.assertEqual(self.lock.unique_id, self.subject._device.unique_id)
  73. def test_device_info_returns_device_info_from_device(self):
  74. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  75. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  76. self.assertEqual(self.lock.device_info, self.subject._device.device_info)
  77. @skip("Icon customisation not yet supported")
  78. def test_icon(self):
  79. self.dps[HVACMODE_DPS] = True
  80. self.assertEqual(self.subject.icon, "mdi:radiator")
  81. self.dps[HVACMODE_DPS] = False
  82. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  83. self.dps[HVACMODE_DPS] = True
  84. self.dps[POWERLEVEL_DPS] = "stop"
  85. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  86. def test_temperature_unit_returns_device_temperature_unit(self):
  87. self.assertEqual(
  88. self.subject.temperature_unit, self.subject._device.temperature_unit
  89. )
  90. def test_target_temperature(self):
  91. self.dps[TEMPERATURE_DPS] = 25
  92. self.dps[PRESET_DPS] = "C"
  93. self.assertEqual(self.subject.target_temperature, 25)
  94. @skip("Conditional redirection not supported.")
  95. def test_target_temperature_in_eco_and_af_modes(self):
  96. self.dps[TEMPERATURE_DPS] = 25
  97. self.dps[ECOTEMP_DPS] = 15
  98. self.dps[PRESET_DPS] = "ECO"
  99. self.assertEqual(self.subject.target_temperature, 15)
  100. self.dps[PRESET_DPS] = "AF"
  101. self.assertIs(self.subject.target_temperature, None)
  102. def test_target_temperature_step(self):
  103. self.assertEqual(self.subject.target_temperature_step, 1)
  104. @skip("Conditional ranges not yet implemented")
  105. def test_minimum_temperature(self):
  106. self.dps[PRESET_DPS] = "C"
  107. self.assertEqual(self.subject.min_temp, 5)
  108. self.dps[PRESET_DPS] = "ECO"
  109. self.assertEqual(self.subject.min_temp, 5)
  110. self.dps[PRESET_DPS] = "AF"
  111. self.assertIs(self.subject.min_temp, None)
  112. @skip("Conditional ranges not yet implemented")
  113. def test_maximum_target_temperature(self):
  114. self.dps[PRESET_DPS] = "C"
  115. self.assertEqual(self.subject.max_temp, 35)
  116. self.dps[PRESET_DPS] = "ECO"
  117. self.assertEqual(self.subject.max_temp, 21)
  118. self.dps[PRESET_DPS] = "AF"
  119. self.assertIs(self.subject.max_temp, None)
  120. async def test_legacy_set_temperature_with_temperature(self):
  121. async with assert_device_properties_set(
  122. self.subject._device, {TEMPERATURE_DPS: 25}
  123. ):
  124. await self.subject.async_set_temperature(temperature=25)
  125. async def test_legacy_set_temperature_with_preset_mode(self):
  126. async with assert_device_properties_set(
  127. self.subject._device, {PRESET_DPS: "C"}
  128. ):
  129. await self.subject.async_set_temperature(preset_mode="comfort")
  130. async def test_legacy_set_temperature_with_both_properties(self):
  131. async with assert_device_properties_set(
  132. self.subject._device,
  133. {
  134. TEMPERATURE_DPS: 25,
  135. PRESET_DPS: "C",
  136. },
  137. ):
  138. await self.subject.async_set_temperature(
  139. temperature=25, preset_mode="comfort"
  140. )
  141. async def test_legacy_set_temperature_with_no_valid_properties(self):
  142. await self.subject.async_set_temperature(something="else")
  143. self.subject._device.async_set_property.assert_not_called
  144. async def test_set_target_temperature_in_comfort_mode(self):
  145. self.dps[PRESET_DPS] = "C"
  146. async with assert_device_properties_set(
  147. self.subject._device, {TEMPERATURE_DPS: 25}
  148. ):
  149. await self.subject.async_set_target_temperature(25)
  150. @skip("Redirection not yet supported")
  151. async def test_set_target_temperature_in_eco_mode(self):
  152. self.dps[PRESET_DPS] = "ECO"
  153. async with assert_device_properties_set(
  154. self.subject._device, {ECOTEMP_DPS: 15}
  155. ):
  156. await self.subject.async_set_target_temperature(15)
  157. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  158. async with assert_device_properties_set(
  159. self.subject._device,
  160. {TEMPERATURE_DPS: 25},
  161. ):
  162. await self.subject.async_set_target_temperature(24.6)
  163. @skip("Conditional ranges not supported yet")
  164. async def test_set_target_temperature_fails_outside_valid_range_in_comfort(self):
  165. self.dps[PRESET_DPS] = "C"
  166. with self.assertRaisesRegex(
  167. ValueError, "Target temperature \\(4\\) must be between 5 and 35"
  168. ):
  169. await self.subject.async_set_target_temperature(4)
  170. with self.assertRaisesRegex(
  171. ValueError, "Target temperature \\(36\\) must be between 5 and 35"
  172. ):
  173. await self.subject.async_set_target_temperature(36)
  174. @skip("Conditional ranges not supported yet")
  175. async def test_set_target_temperature_fails_outside_valid_range_in_eco(self):
  176. self.dps[PRESET_DPS] = "ECO"
  177. with self.assertRaisesRegex(
  178. ValueError, "Target temperature \\(4\\) must be between 5 and 21"
  179. ):
  180. await self.subject.async_set_target_temperature(4)
  181. with self.assertRaisesRegex(
  182. ValueError, "Target temperature \\(22\\) must be between 5 and 21"
  183. ):
  184. await self.subject.async_set_target_temperature(22)
  185. @skip("Conditional ranges not supported yet")
  186. async def test_set_target_temperature_fails_in_anti_freeze(self):
  187. self.dps[PRESET_DPS] = "AF"
  188. with self.assertRaisesRegex(
  189. ValueError, "You cannot set the temperature in Anti-freeze mode"
  190. ):
  191. await self.subject.async_set_target_temperature(25)
  192. def test_current_temperature(self):
  193. self.dps[CURRENTTEMP_DPS] = 25
  194. self.assertEqual(self.subject.current_temperature, 25)
  195. def test_hvac_mode(self):
  196. self.dps[HVACMODE_DPS] = True
  197. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  198. self.dps[HVACMODE_DPS] = False
  199. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  200. self.dps[HVACMODE_DPS] = None
  201. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  202. def test_hvac_modes(self):
  203. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  204. async def test_turn_on(self):
  205. async with assert_device_properties_set(
  206. self.subject._device, {HVACMODE_DPS: True}
  207. ):
  208. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  209. async def test_turn_off(self):
  210. async with assert_device_properties_set(
  211. self.subject._device, {HVACMODE_DPS: False}
  212. ):
  213. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  214. def test_preset_mode(self):
  215. self.dps[PRESET_DPS] = "C"
  216. self.assertEqual(self.subject.preset_mode, "comfort")
  217. self.dps[PRESET_DPS] = "ECO"
  218. self.assertEqual(self.subject.preset_mode, "eco")
  219. self.dps[PRESET_DPS] = "AF"
  220. self.assertEqual(self.subject.preset_mode, "away")
  221. self.dps[PRESET_DPS] = None
  222. self.assertIs(self.subject.preset_mode, None)
  223. def test_preset_modes(self):
  224. self.assertCountEqual(self.subject.preset_modes, ["comfort", "eco", "away"])
  225. async def test_set_preset_mode_to_comfort(self):
  226. async with assert_device_properties_set(
  227. self.subject._device,
  228. {PRESET_DPS: "C"},
  229. ):
  230. await self.subject.async_set_preset_mode("comfort")
  231. async def test_set_preset_mode_to_eco(self):
  232. async with assert_device_properties_set(
  233. self.subject._device,
  234. {PRESET_DPS: "ECO"},
  235. ):
  236. await self.subject.async_set_preset_mode("eco")
  237. async def test_set_preset_mode_to_anti_freeze(self):
  238. async with assert_device_properties_set(
  239. self.subject._device,
  240. {PRESET_DPS: "AF"},
  241. ):
  242. await self.subject.async_set_preset_mode("away")
  243. @skip("Conditional redirection not yet supported")
  244. def test_power_level_returns_user_power_level(self):
  245. self.dps[SWING_DPS] = "user"
  246. self.dps[POWERLEVEL_DPS] = "stop"
  247. self.assertEqual(self.subject.swing_mode, "Stop")
  248. self.dps[POWERLEVEL_DPS] = "3"
  249. self.assertEqual(self.subject.swing_mode, "3")
  250. self.dps[POWERLEVEL_DPS] = None
  251. self.assertIs(self.subject.swing_mode, None)
  252. def test_non_user_swing_mode(self):
  253. self.dps[SWING_DPS] = "stop"
  254. self.assertEqual(self.subject.swing_mode, "Stop")
  255. self.dps[SWING_DPS] = "auto"
  256. self.assertEqual(self.subject.swing_mode, "Auto")
  257. self.dps[SWING_DPS] = None
  258. self.assertIs(self.subject.swing_mode, None)
  259. @skip("Conditional redirection not supported yet")
  260. def test_swing_modes(self):
  261. self.assertCountEqual(
  262. self.subject.swing_modes,
  263. ["Stop", "1", "2", "3", "4", "5", "Auto"],
  264. )
  265. @skip("Conditional redirection not supported yet")
  266. async def test_set_power_level_to_stop(self):
  267. async with assert_device_properties_set(
  268. self.subject._device,
  269. {POWERLEVEL_DPS: "stop"},
  270. ):
  271. await self.subject.async_set_swing_mode("Stop")
  272. @skip("Conditional redirection not supported yet")
  273. async def test_set_swing_mode_to_auto(self):
  274. async with assert_device_properties_set(
  275. self.subject._device,
  276. {SWING_DPS: "auto"},
  277. ):
  278. await self.subject.async_set_swing_mode("Auto")
  279. @skip("Conditional redirection not supported yet")
  280. async def test_set_power_level_to_numeric_value(self):
  281. async with assert_device_properties_set(
  282. self.subject._device,
  283. {POWERLEVEL_DPS: "3"},
  284. ):
  285. await self.subject.async_set_swing_mode("3")
  286. @skip("Conditional redirection not supported yet")
  287. async def test_set_power_level_to_invalid_value_raises_error(self):
  288. with self.assertRaisesRegex(ValueError, "Invalid power level: unknown"):
  289. await self.subject.async_set_swing_mode("unknown")
  290. @skip("Hidden dps not supported yet")
  291. def test_device_state_attributes(self):
  292. self.dps[ERROR_DPS] = "something"
  293. self.dps[TIMER_DPS] = 5
  294. self.dps[TIMERACT_DPS] = True
  295. self.dps[POWERLEVEL_DPS] = 4
  296. self.assertCountEqual(
  297. self.subject.device_state_attributes,
  298. {
  299. "error": "something",
  300. "timer": 5,
  301. "timer_mode": True,
  302. "power_level": 4,
  303. },
  304. )
  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_lock_was_created(self):
  312. self.assertIsInstance(self.lock, TuyaLocalLock)
  313. def test_lock_is_same_device(self):
  314. self.assertEqual(self.lock._device, self.subject._device)
  315. def test_lock_state(self):
  316. self.dps[LOCK_DPS] = True
  317. self.assertEqual(self.lock.state, STATE_LOCKED)
  318. self.dps[LOCK_DPS] = False
  319. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  320. self.dps[LOCK_DPS] = None
  321. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  322. def test_lock_is_locked(self):
  323. self.dps[LOCK_DPS] = True
  324. self.assertTrue(self.lock.is_locked)
  325. self.dps[LOCK_DPS] = False
  326. self.assertFalse(self.lock.is_locked)
  327. self.dps[LOCK_DPS] = None
  328. self.assertFalse(self.lock.is_locked)
  329. async def async_test_lock_locks(self):
  330. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  331. await self.subject.async_lock()
  332. async def async_test_lock_unlocks(self):
  333. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  334. await self.subject.async_unlock()
  335. async def async_test_lock_update(self):
  336. result = AsyncMock()
  337. self.lock._device.async_refresh.return_value = result()
  338. await self.lock.async_update()
  339. self.lock._device.async_refresh.assert_called_once()
  340. result.assert_awaited()
  341. def test_light_was_created(self):
  342. self.assertIsInstance(self.light, TuyaLocalLight)
  343. def test_light_is_same_device(self):
  344. self.assertEqual(self.light._device, self.subject._device)
  345. def test_light_icon(self):
  346. self.dps[LIGHT_DPS] = True
  347. self.assertEqual(self.light.icon, "mdi:led-on")
  348. self.dps[LIGHT_DPS] = False
  349. self.assertEqual(self.light.icon, "mdi:led-off")
  350. def test_light_is_on(self):
  351. self.dps[LIGHT_DPS] = True
  352. self.assertEqual(self.light.is_on, True)
  353. self.dps[LIGHT_DPS] = False
  354. self.assertEqual(self.light.is_on, False)
  355. def test_light_state_attributes(self):
  356. self.assertEqual(self.light.device_state_attributes, {})
  357. async def test_light_turn_on(self):
  358. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  359. await self.light.async_turn_on()
  360. async def test_light_turn_off(self):
  361. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  362. await self.light.async_turn_off()
  363. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  364. self.dps[LIGHT_DPS] = False
  365. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  366. await self.light.async_toggle()
  367. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  368. self.dps[LIGHT_DPS] = True
  369. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  370. await self.light.async_toggle()
  371. async def test_light_update(self):
  372. result = AsyncMock()
  373. self.light._device.async_refresh.return_value = result()
  374. await self.light.async_update()
  375. self.light._device.async_refresh.assert_called_once()
  376. result.assert_awaited()