test_goldair_gpph_heater.py 16 KB

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