test_goldair_gpph_heater.py 13 KB

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