test_goldair_gpph_heater.py 11 KB

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