test_goldair_gpph_heater.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. def test_target_temperature_in_eco_and_af_modes(self):
  95. self.dps[TEMPERATURE_DPS] = 25
  96. self.dps[ECOTEMP_DPS] = 15
  97. self.dps[PRESET_DPS] = "ECO"
  98. self.assertEqual(self.subject.target_temperature, 15)
  99. self.dps[PRESET_DPS] = "AF"
  100. self.assertIs(self.subject.target_temperature, None)
  101. def test_target_temperature_step(self):
  102. self.assertEqual(self.subject.target_temperature_step, 1)
  103. def test_minimum_temperature(self):
  104. self.dps[PRESET_DPS] = "C"
  105. self.assertEqual(self.subject.min_temp, 5)
  106. self.dps[PRESET_DPS] = "ECO"
  107. self.assertEqual(self.subject.min_temp, 5)
  108. self.dps[PRESET_DPS] = "AF"
  109. self.assertIs(self.subject.min_temp, 5)
  110. def test_maximum_target_temperature(self):
  111. self.dps[PRESET_DPS] = "C"
  112. self.assertEqual(self.subject.max_temp, 35)
  113. self.dps[PRESET_DPS] = "ECO"
  114. self.assertEqual(self.subject.max_temp, 21)
  115. self.dps[PRESET_DPS] = "AF"
  116. self.assertIs(self.subject.max_temp, 5)
  117. async def test_legacy_set_temperature_with_temperature(self):
  118. async with assert_device_properties_set(
  119. self.subject._device, {TEMPERATURE_DPS: 25}
  120. ):
  121. await self.subject.async_set_temperature(temperature=25)
  122. async def test_legacy_set_temperature_with_preset_mode(self):
  123. async with assert_device_properties_set(
  124. self.subject._device, {PRESET_DPS: "C"}
  125. ):
  126. await self.subject.async_set_temperature(preset_mode="comfort")
  127. async def test_legacy_set_temperature_with_both_properties(self):
  128. async with assert_device_properties_set(
  129. self.subject._device,
  130. {
  131. TEMPERATURE_DPS: 25,
  132. PRESET_DPS: "C",
  133. },
  134. ):
  135. await self.subject.async_set_temperature(
  136. temperature=25, preset_mode="comfort"
  137. )
  138. async def test_legacy_set_temperature_with_no_valid_properties(self):
  139. await self.subject.async_set_temperature(something="else")
  140. self.subject._device.async_set_property.assert_not_called
  141. async def test_set_target_temperature_in_comfort_mode(self):
  142. self.dps[PRESET_DPS] = "C"
  143. async with assert_device_properties_set(
  144. self.subject._device, {TEMPERATURE_DPS: 25}
  145. ):
  146. await self.subject.async_set_target_temperature(25)
  147. async def test_set_target_temperature_in_eco_mode(self):
  148. self.dps[PRESET_DPS] = "ECO"
  149. async with assert_device_properties_set(
  150. self.subject._device, {ECOTEMP_DPS: 15}
  151. ):
  152. await self.subject.async_set_target_temperature(15)
  153. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  154. async with assert_device_properties_set(
  155. self.subject._device,
  156. {TEMPERATURE_DPS: 25},
  157. ):
  158. await self.subject.async_set_target_temperature(24.6)
  159. async def test_set_target_temperature_fails_outside_valid_range_in_comfort(self):
  160. self.dps[PRESET_DPS] = "C"
  161. with self.assertRaisesRegex(
  162. ValueError, "temperature \\(4\\) must be between 5 and 35"
  163. ):
  164. await self.subject.async_set_target_temperature(4)
  165. with self.assertRaisesRegex(
  166. ValueError, "temperature \\(36\\) must be between 5 and 35"
  167. ):
  168. await self.subject.async_set_target_temperature(36)
  169. async def test_set_target_temperature_fails_outside_valid_range_in_eco(self):
  170. self.dps[PRESET_DPS] = "ECO"
  171. with self.assertRaisesRegex(
  172. ValueError, "eco_temperature \\(4\\) must be between 5 and 21"
  173. ):
  174. await self.subject.async_set_target_temperature(4)
  175. with self.assertRaisesRegex(
  176. ValueError, "eco_temperature \\(22\\) must be between 5 and 21"
  177. ):
  178. await self.subject.async_set_target_temperature(22)
  179. async def test_set_target_temperature_fails_in_anti_freeze(self):
  180. self.dps[PRESET_DPS] = "AF"
  181. with self.assertRaisesRegex(
  182. AttributeError, "temperature cannot be set at this time"
  183. ):
  184. await self.subject.async_set_target_temperature(25)
  185. def test_current_temperature(self):
  186. self.dps[CURRENTTEMP_DPS] = 25
  187. self.assertEqual(self.subject.current_temperature, 25)
  188. def test_hvac_mode(self):
  189. self.dps[HVACMODE_DPS] = True
  190. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  191. self.dps[HVACMODE_DPS] = False
  192. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  193. self.dps[HVACMODE_DPS] = None
  194. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  195. def test_hvac_modes(self):
  196. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  197. async def test_turn_on(self):
  198. async with assert_device_properties_set(
  199. self.subject._device, {HVACMODE_DPS: True}
  200. ):
  201. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  202. async def test_turn_off(self):
  203. async with assert_device_properties_set(
  204. self.subject._device, {HVACMODE_DPS: False}
  205. ):
  206. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  207. def test_preset_mode(self):
  208. self.dps[PRESET_DPS] = "C"
  209. self.assertEqual(self.subject.preset_mode, "comfort")
  210. self.dps[PRESET_DPS] = "ECO"
  211. self.assertEqual(self.subject.preset_mode, "eco")
  212. self.dps[PRESET_DPS] = "AF"
  213. self.assertEqual(self.subject.preset_mode, "away")
  214. self.dps[PRESET_DPS] = None
  215. self.assertIs(self.subject.preset_mode, None)
  216. def test_preset_modes(self):
  217. self.assertCountEqual(self.subject.preset_modes, ["comfort", "eco", "away"])
  218. async def test_set_preset_mode_to_comfort(self):
  219. async with assert_device_properties_set(
  220. self.subject._device,
  221. {PRESET_DPS: "C"},
  222. ):
  223. await self.subject.async_set_preset_mode("comfort")
  224. async def test_set_preset_mode_to_eco(self):
  225. async with assert_device_properties_set(
  226. self.subject._device,
  227. {PRESET_DPS: "ECO"},
  228. ):
  229. await self.subject.async_set_preset_mode("eco")
  230. async def test_set_preset_mode_to_anti_freeze(self):
  231. async with assert_device_properties_set(
  232. self.subject._device,
  233. {PRESET_DPS: "AF"},
  234. ):
  235. await self.subject.async_set_preset_mode("away")
  236. def test_power_level_returns_user_power_level(self):
  237. self.dps[SWING_DPS] = "user"
  238. self.dps[POWERLEVEL_DPS] = "stop"
  239. self.assertEqual(self.subject.swing_mode, "Stop")
  240. self.dps[POWERLEVEL_DPS] = "3"
  241. self.assertEqual(self.subject.swing_mode, "3")
  242. self.dps[POWERLEVEL_DPS] = None
  243. self.assertIs(self.subject.swing_mode, None)
  244. def test_non_user_swing_mode(self):
  245. self.dps[SWING_DPS] = "stop"
  246. self.assertEqual(self.subject.swing_mode, "Stop")
  247. self.dps[SWING_DPS] = "auto"
  248. self.assertEqual(self.subject.swing_mode, "Auto")
  249. self.dps[SWING_DPS] = None
  250. self.assertIs(self.subject.swing_mode, None)
  251. @skip("Conditional redirection not supported yet")
  252. def test_swing_modes(self):
  253. self.assertCountEqual(
  254. self.subject.swing_modes,
  255. ["Stop", "1", "2", "3", "4", "5", "Auto"],
  256. )
  257. @skip("Conditional redirection not supported yet")
  258. async def test_set_power_level_to_stop(self):
  259. async with assert_device_properties_set(
  260. self.subject._device,
  261. {POWERLEVEL_DPS: "stop"},
  262. ):
  263. await self.subject.async_set_swing_mode("Stop")
  264. async def test_set_swing_mode_to_auto(self):
  265. async with assert_device_properties_set(
  266. self.subject._device,
  267. {SWING_DPS: "auto"},
  268. ):
  269. await self.subject.async_set_swing_mode("Auto")
  270. @skip("Conditional redirection not supported yet")
  271. async def test_set_power_level_to_numeric_value(self):
  272. async with assert_device_properties_set(
  273. self.subject._device,
  274. {POWERLEVEL_DPS: "3"},
  275. ):
  276. await self.subject.async_set_swing_mode("3")
  277. @skip("Conditional redirection not supported yet")
  278. async def test_set_power_level_to_invalid_value_raises_error(self):
  279. with self.assertRaisesRegex(ValueError, "Invalid power level: unknown"):
  280. await self.subject.async_set_swing_mode("unknown")
  281. def test_device_state_attributes(self):
  282. self.dps[ERROR_DPS] = "something"
  283. self.dps[TIMER_DPS] = 5
  284. self.dps[TIMERACT_DPS] = True
  285. self.dps[POWERLEVEL_DPS] = 4
  286. self.assertCountEqual(
  287. self.subject.device_state_attributes,
  288. {
  289. "error": "something",
  290. "timer": 5,
  291. "timer_mode": True,
  292. "power_level": 4,
  293. },
  294. )
  295. async def test_update(self):
  296. result = AsyncMock()
  297. self.subject._device.async_refresh.return_value = result()
  298. await self.subject.async_update()
  299. self.subject._device.async_refresh.assert_called_once()
  300. result.assert_awaited()
  301. def test_lock_was_created(self):
  302. self.assertIsInstance(self.lock, TuyaLocalLock)
  303. def test_lock_is_same_device(self):
  304. self.assertEqual(self.lock._device, self.subject._device)
  305. def test_lock_state(self):
  306. self.dps[LOCK_DPS] = True
  307. self.assertEqual(self.lock.state, STATE_LOCKED)
  308. self.dps[LOCK_DPS] = False
  309. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  310. self.dps[LOCK_DPS] = None
  311. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  312. def test_lock_is_locked(self):
  313. self.dps[LOCK_DPS] = True
  314. self.assertTrue(self.lock.is_locked)
  315. self.dps[LOCK_DPS] = False
  316. self.assertFalse(self.lock.is_locked)
  317. self.dps[LOCK_DPS] = None
  318. self.assertFalse(self.lock.is_locked)
  319. async def async_test_lock_locks(self):
  320. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  321. await self.subject.async_lock()
  322. async def async_test_lock_unlocks(self):
  323. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  324. await self.subject.async_unlock()
  325. async def async_test_lock_update(self):
  326. result = AsyncMock()
  327. self.lock._device.async_refresh.return_value = result()
  328. await self.lock.async_update()
  329. self.lock._device.async_refresh.assert_called_once()
  330. result.assert_awaited()
  331. def test_light_was_created(self):
  332. self.assertIsInstance(self.light, TuyaLocalLight)
  333. def test_light_is_same_device(self):
  334. self.assertEqual(self.light._device, self.subject._device)
  335. def test_light_icon(self):
  336. self.dps[LIGHT_DPS] = True
  337. self.assertEqual(self.light.icon, "mdi:led-on")
  338. self.dps[LIGHT_DPS] = False
  339. self.assertEqual(self.light.icon, "mdi:led-off")
  340. def test_light_is_on(self):
  341. self.dps[LIGHT_DPS] = True
  342. self.assertEqual(self.light.is_on, True)
  343. self.dps[LIGHT_DPS] = False
  344. self.assertEqual(self.light.is_on, False)
  345. def test_light_state_attributes(self):
  346. self.assertEqual(self.light.device_state_attributes, {})
  347. async def test_light_turn_on(self):
  348. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  349. await self.light.async_turn_on()
  350. async def test_light_turn_off(self):
  351. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  352. await self.light.async_turn_off()
  353. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  354. self.dps[LIGHT_DPS] = False
  355. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  356. await self.light.async_toggle()
  357. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  358. self.dps[LIGHT_DPS] = True
  359. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  360. await self.light.async_toggle()
  361. async def test_light_update(self):
  362. result = AsyncMock()
  363. self.light._device.async_refresh.return_value = result()
  364. await self.light.async_update()
  365. self.light._device.async_refresh.assert_called_once()
  366. result.assert_awaited()