test_goldair_gpph_heater.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. from unittest import skip
  2. from homeassistant.components.climate.const import (
  3. HVAC_MODE_HEAT,
  4. HVAC_MODE_OFF,
  5. SUPPORT_PRESET_MODE,
  6. SUPPORT_SWING_MODE,
  7. SUPPORT_TARGET_TEMPERATURE,
  8. )
  9. from homeassistant.components.light import COLOR_MODE_ONOFF
  10. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  11. from homeassistant.const import STATE_UNAVAILABLE
  12. from ..const import GPPH_HEATER_PAYLOAD
  13. from ..helpers import assert_device_properties_set
  14. from .base_device_tests import TuyaDeviceTestCase
  15. HVACMODE_DPS = "1"
  16. TEMPERATURE_DPS = "2"
  17. CURRENTTEMP_DPS = "3"
  18. PRESET_DPS = "4"
  19. LOCK_DPS = "6"
  20. ERROR_DPS = "12"
  21. POWERLEVEL_DPS = "101"
  22. TIMER_DPS = "102"
  23. TIMERACT_DPS = "103"
  24. LIGHT_DPS = "104"
  25. SWING_DPS = "105"
  26. ECOTEMP_DPS = "106"
  27. class TestGoldairHeater(TuyaDeviceTestCase):
  28. __test__ = True
  29. def setUp(self):
  30. self.setUpForConfig("goldair_gpph_heater.yaml", GPPH_HEATER_PAYLOAD)
  31. self.subject = self.entities.get("climate")
  32. self.light = self.entities.get("light_display")
  33. self.lock = self.entities.get("lock_child_lock")
  34. def test_supported_features(self):
  35. self.assertEqual(
  36. self.subject.supported_features,
  37. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE,
  38. )
  39. def test_icon(self):
  40. self.dps[HVACMODE_DPS] = True
  41. self.assertEqual(self.subject.icon, "mdi:radiator")
  42. self.dps[HVACMODE_DPS] = False
  43. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  44. self.dps[HVACMODE_DPS] = True
  45. self.dps[POWERLEVEL_DPS] = "stop"
  46. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  47. def test_temperature_unit_returns_device_temperature_unit(self):
  48. self.assertEqual(
  49. self.subject.temperature_unit, self.subject._device.temperature_unit
  50. )
  51. def test_target_temperature(self):
  52. self.dps[TEMPERATURE_DPS] = 25
  53. self.dps[PRESET_DPS] = "C"
  54. self.assertEqual(self.subject.target_temperature, 25)
  55. def test_target_temperature_in_eco_and_af_modes(self):
  56. self.dps[TEMPERATURE_DPS] = 25
  57. self.dps[ECOTEMP_DPS] = 15
  58. self.dps[PRESET_DPS] = "ECO"
  59. self.assertEqual(self.subject.target_temperature, 15)
  60. self.dps[PRESET_DPS] = "AF"
  61. self.assertIs(self.subject.target_temperature, None)
  62. def test_target_temperature_step(self):
  63. self.assertEqual(self.subject.target_temperature_step, 1)
  64. def test_minimum_temperature(self):
  65. self.dps[PRESET_DPS] = "C"
  66. self.assertEqual(self.subject.min_temp, 5)
  67. self.dps[PRESET_DPS] = "ECO"
  68. self.assertEqual(self.subject.min_temp, 5)
  69. self.dps[PRESET_DPS] = "AF"
  70. self.assertIs(self.subject.min_temp, 5)
  71. def test_maximum_target_temperature(self):
  72. self.dps[PRESET_DPS] = "C"
  73. self.assertEqual(self.subject.max_temp, 35)
  74. self.dps[PRESET_DPS] = "ECO"
  75. self.assertEqual(self.subject.max_temp, 21)
  76. self.dps[PRESET_DPS] = "AF"
  77. self.assertIs(self.subject.max_temp, 5)
  78. async def test_legacy_set_temperature_with_temperature(self):
  79. async with assert_device_properties_set(
  80. self.subject._device, {TEMPERATURE_DPS: 25}
  81. ):
  82. await self.subject.async_set_temperature(temperature=25)
  83. async def test_legacy_set_temperature_with_preset_mode(self):
  84. async with assert_device_properties_set(
  85. self.subject._device, {PRESET_DPS: "C"}
  86. ):
  87. await self.subject.async_set_temperature(preset_mode="comfort")
  88. async def test_legacy_set_temperature_with_both_properties(self):
  89. async with assert_device_properties_set(
  90. self.subject._device,
  91. {
  92. TEMPERATURE_DPS: 25,
  93. PRESET_DPS: "C",
  94. },
  95. ):
  96. await self.subject.async_set_temperature(
  97. temperature=25, preset_mode="comfort"
  98. )
  99. async def test_legacy_set_temperature_with_no_valid_properties(self):
  100. await self.subject.async_set_temperature(something="else")
  101. self.subject._device.async_set_property.assert_not_called()
  102. async def test_set_target_temperature_in_comfort_mode(self):
  103. self.dps[PRESET_DPS] = "C"
  104. async with assert_device_properties_set(
  105. self.subject._device, {TEMPERATURE_DPS: 25}
  106. ):
  107. await self.subject.async_set_target_temperature(25)
  108. async def test_set_target_temperature_in_eco_mode(self):
  109. self.dps[PRESET_DPS] = "ECO"
  110. async with assert_device_properties_set(
  111. self.subject._device, {ECOTEMP_DPS: 15}
  112. ):
  113. await self.subject.async_set_target_temperature(15)
  114. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  115. async with assert_device_properties_set(
  116. self.subject._device,
  117. {TEMPERATURE_DPS: 25},
  118. ):
  119. await self.subject.async_set_target_temperature(24.6)
  120. async def test_set_target_temperature_fails_outside_valid_range_in_comfort(self):
  121. self.dps[PRESET_DPS] = "C"
  122. with self.assertRaisesRegex(
  123. ValueError, "temperature \\(4\\) must be between 5 and 35"
  124. ):
  125. await self.subject.async_set_target_temperature(4)
  126. with self.assertRaisesRegex(
  127. ValueError, "temperature \\(36\\) must be between 5 and 35"
  128. ):
  129. await self.subject.async_set_target_temperature(36)
  130. async def test_set_target_temperature_fails_outside_valid_range_in_eco(self):
  131. self.dps[PRESET_DPS] = "ECO"
  132. with self.assertRaisesRegex(
  133. ValueError, "eco_temperature \\(4\\) must be between 5 and 21"
  134. ):
  135. await self.subject.async_set_target_temperature(4)
  136. with self.assertRaisesRegex(
  137. ValueError, "eco_temperature \\(22\\) must be between 5 and 21"
  138. ):
  139. await self.subject.async_set_target_temperature(22)
  140. async def test_set_target_temperature_fails_in_anti_freeze(self):
  141. self.dps[PRESET_DPS] = "AF"
  142. with self.assertRaisesRegex(
  143. AttributeError, "temperature cannot be set at this time"
  144. ):
  145. await self.subject.async_set_target_temperature(25)
  146. def test_current_temperature(self):
  147. self.dps[CURRENTTEMP_DPS] = 25
  148. self.assertEqual(self.subject.current_temperature, 25)
  149. def test_humidity_unsupported(self):
  150. self.assertIsNone(self.subject.min_humidity)
  151. self.assertIsNone(self.subject.max_humidity)
  152. self.assertIsNone(self.subject.current_humidity)
  153. with self.assertRaises(NotImplementedError):
  154. self.subject.target_humidity
  155. async def test_set_humidity_unsupported(self):
  156. with self.assertRaises(NotImplementedError):
  157. await self.subject.async_set_humidity(50)
  158. def test_hvac_mode(self):
  159. self.dps[HVACMODE_DPS] = True
  160. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  161. self.dps[HVACMODE_DPS] = False
  162. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  163. self.dps[HVACMODE_DPS] = None
  164. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  165. def test_hvac_modes(self):
  166. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  167. async def test_turn_on(self):
  168. async with assert_device_properties_set(
  169. self.subject._device, {HVACMODE_DPS: True}
  170. ):
  171. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  172. async def test_turn_off(self):
  173. async with assert_device_properties_set(
  174. self.subject._device, {HVACMODE_DPS: False}
  175. ):
  176. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  177. def test_preset_mode(self):
  178. self.dps[PRESET_DPS] = "C"
  179. self.assertEqual(self.subject.preset_mode, "comfort")
  180. self.dps[PRESET_DPS] = "ECO"
  181. self.assertEqual(self.subject.preset_mode, "eco")
  182. self.dps[PRESET_DPS] = "AF"
  183. self.assertEqual(self.subject.preset_mode, "away")
  184. self.dps[PRESET_DPS] = None
  185. self.assertIs(self.subject.preset_mode, None)
  186. def test_preset_modes(self):
  187. self.assertCountEqual(self.subject.preset_modes, ["comfort", "eco", "away"])
  188. async def test_set_preset_mode_to_comfort(self):
  189. async with assert_device_properties_set(
  190. self.subject._device,
  191. {PRESET_DPS: "C"},
  192. ):
  193. await self.subject.async_set_preset_mode("comfort")
  194. async def test_set_preset_mode_to_eco(self):
  195. async with assert_device_properties_set(
  196. self.subject._device,
  197. {PRESET_DPS: "ECO"},
  198. ):
  199. await self.subject.async_set_preset_mode("eco")
  200. async def test_set_preset_mode_to_anti_freeze(self):
  201. async with assert_device_properties_set(
  202. self.subject._device,
  203. {PRESET_DPS: "AF"},
  204. ):
  205. await self.subject.async_set_preset_mode("away")
  206. def test_power_level_returns_user_power_level(self):
  207. self.dps[SWING_DPS] = "user"
  208. self.dps[POWERLEVEL_DPS] = "stop"
  209. self.assertEqual(self.subject.swing_mode, "Stop")
  210. self.dps[POWERLEVEL_DPS] = "3"
  211. self.assertEqual(self.subject.swing_mode, "3")
  212. def test_non_user_swing_mode(self):
  213. self.dps[SWING_DPS] = "stop"
  214. self.assertEqual(self.subject.swing_mode, "Stop")
  215. self.dps[SWING_DPS] = "auto"
  216. self.assertEqual(self.subject.swing_mode, "Auto")
  217. self.dps[SWING_DPS] = None
  218. self.assertIs(self.subject.swing_mode, None)
  219. def test_swing_modes(self):
  220. self.assertCountEqual(
  221. self.subject.swing_modes,
  222. ["Stop", "1", "2", "3", "4", "5", "Auto"],
  223. )
  224. @skip("Paired settings not supported yet")
  225. async def test_set_power_level_to_stop(self):
  226. async with assert_device_properties_set(
  227. self.subject._device,
  228. {POWERLEVEL_DPS: "stop"},
  229. ):
  230. await self.subject.async_set_swing_mode("Stop")
  231. async def test_set_swing_mode_to_auto(self):
  232. async with assert_device_properties_set(
  233. self.subject._device,
  234. {SWING_DPS: "auto"},
  235. ):
  236. await self.subject.async_set_swing_mode("Auto")
  237. async def test_set_power_level_to_numeric_value(self):
  238. async with assert_device_properties_set(
  239. self.subject._device,
  240. {SWING_DPS: "user", POWERLEVEL_DPS: "3"},
  241. ):
  242. await self.subject.async_set_swing_mode("3")
  243. @skip("Restriction to mapped values not supported yet")
  244. async def test_set_power_level_to_invalid_value_raises_error(self):
  245. with self.assertRaisesRegex(ValueError, "Invalid power level: unknown"):
  246. await self.subject.async_set_swing_mode("unknown")
  247. def test_device_state_attributes(self):
  248. self.dps[ERROR_DPS] = "something"
  249. self.dps[TIMER_DPS] = 5
  250. self.dps[TIMERACT_DPS] = True
  251. self.dps[POWERLEVEL_DPS] = 4
  252. self.assertDictEqual(
  253. self.subject.device_state_attributes,
  254. {
  255. "error": "something",
  256. "timer": 5,
  257. "timer_mode": True,
  258. "power_level": "4",
  259. },
  260. )
  261. def test_lock_state(self):
  262. self.dps[LOCK_DPS] = True
  263. self.assertEqual(self.lock.state, STATE_LOCKED)
  264. self.dps[LOCK_DPS] = False
  265. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  266. self.dps[LOCK_DPS] = None
  267. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  268. def test_lock_is_locked(self):
  269. self.dps[LOCK_DPS] = True
  270. self.assertTrue(self.lock.is_locked)
  271. self.dps[LOCK_DPS] = False
  272. self.assertFalse(self.lock.is_locked)
  273. self.dps[LOCK_DPS] = None
  274. self.assertFalse(self.lock.is_locked)
  275. async def test_lock_locks(self):
  276. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  277. await self.lock.async_lock()
  278. async def test_lock_unlocks(self):
  279. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  280. await self.lock.async_unlock()
  281. def test_light_supported_color_modes(self):
  282. self.assertCountEqual(
  283. self.light.supported_color_modes,
  284. [COLOR_MODE_ONOFF],
  285. )
  286. def test_light_color_mode(self):
  287. self.assertEqual(self.light.color_mode, COLOR_MODE_ONOFF)
  288. def test_light_has_no_brightness(self):
  289. self.assertIsNone(self.light.brightness)
  290. def test_light_has_no_effects(self):
  291. self.assertIsNone(self.light.effect_list)
  292. self.assertIsNone(self.light.effect)
  293. def test_light_icon(self):
  294. self.dps[LIGHT_DPS] = True
  295. self.assertEqual(self.light.icon, "mdi:led-on")
  296. self.dps[LIGHT_DPS] = False
  297. self.assertEqual(self.light.icon, "mdi:led-off")
  298. def test_light_is_on(self):
  299. self.dps[LIGHT_DPS] = True
  300. self.assertEqual(self.light.is_on, True)
  301. self.dps[LIGHT_DPS] = False
  302. self.assertEqual(self.light.is_on, False)
  303. def test_light_state_attributes(self):
  304. self.assertEqual(self.light.device_state_attributes, {})
  305. async def test_light_turn_on(self):
  306. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  307. await self.light.async_turn_on()
  308. async def test_light_turn_off(self):
  309. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  310. await self.light.async_turn_off()
  311. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  312. self.dps[LIGHT_DPS] = False
  313. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  314. await self.light.async_toggle()
  315. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  316. self.dps[LIGHT_DPS] = True
  317. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  318. await self.light.async_toggle()