test_goldair_gpph_heater.py 12 KB

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