test_goldair_gpph_heater.py 12 KB

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