test_goldair_gpph_heater.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. from homeassistant.components.binary_sensor import BinarySensorDeviceClass
  2. from homeassistant.components.climate.const import (
  3. ClimateEntityFeature,
  4. HVACMode,
  5. )
  6. from homeassistant.components.sensor import SensorDeviceClass
  7. from homeassistant.const import (
  8. PERCENTAGE,
  9. PRECISION_WHOLE,
  10. UnitOfTime,
  11. UnitOfTemperature,
  12. )
  13. from ..const import GPPH_HEATER_PAYLOAD
  14. from ..helpers import assert_device_properties_set
  15. from ..mixins.binary_sensor import BasicBinarySensorTests
  16. from ..mixins.climate import TargetTemperatureTests
  17. from ..mixins.light import BasicLightTests
  18. from ..mixins.lock import BasicLockTests
  19. from ..mixins.number import BasicNumberTests
  20. from ..mixins.sensor import BasicSensorTests
  21. from .base_device_tests import TuyaDeviceTestCase
  22. HVACMODE_DPS = "1"
  23. TEMPERATURE_DPS = "2"
  24. CURRENTTEMP_DPS = "3"
  25. PRESET_DPS = "4"
  26. LOCK_DPS = "6"
  27. ERROR_DPS = "12"
  28. POWERLEVEL_DPS = "101"
  29. TIMER_DPS = "102"
  30. TIMERACT_DPS = "103"
  31. LIGHT_DPS = "104"
  32. SWING_DPS = "105"
  33. ECOTEMP_DPS = "106"
  34. class TestGoldairHeater(
  35. BasicBinarySensorTests,
  36. BasicLightTests,
  37. BasicLockTests,
  38. BasicNumberTests,
  39. BasicSensorTests,
  40. TargetTemperatureTests,
  41. TuyaDeviceTestCase,
  42. ):
  43. __test__ = True
  44. def setUp(self):
  45. self.setUpForConfig("goldair_gpph_heater.yaml", GPPH_HEATER_PAYLOAD)
  46. self.subject = self.entities.get("climate")
  47. self.setUpTargetTemperature(
  48. TEMPERATURE_DPS,
  49. self.subject,
  50. min=5,
  51. max=35,
  52. )
  53. self.setUpBasicLight(LIGHT_DPS, self.entities.get("light_display"))
  54. self.setUpBasicLock(LOCK_DPS, self.entities.get("lock_child_lock"))
  55. self.setUpBasicNumber(
  56. TIMER_DPS,
  57. self.entities.get("number_timer"),
  58. max=1440,
  59. step=60,
  60. unit=UnitOfTime.MINUTES,
  61. )
  62. self.setUpBasicSensor(
  63. POWERLEVEL_DPS,
  64. self.entities.get("sensor_power_level"),
  65. unit=PERCENTAGE,
  66. device_class=SensorDeviceClass.POWER_FACTOR,
  67. testdata=("2", 40),
  68. )
  69. self.setUpBasicBinarySensor(
  70. ERROR_DPS,
  71. self.entities.get("binary_sensor_error"),
  72. device_class=BinarySensorDeviceClass.PROBLEM,
  73. testdata=(1, 0),
  74. )
  75. self.mark_secondary(
  76. [
  77. "light_display",
  78. "lock_child_lock",
  79. "number_timer",
  80. "sensor_power_level",
  81. "binary_sensor_error",
  82. ]
  83. )
  84. def test_supported_features(self):
  85. self.assertEqual(
  86. self.subject.supported_features,
  87. (
  88. ClimateEntityFeature.TARGET_TEMPERATURE
  89. | ClimateEntityFeature.PRESET_MODE
  90. | ClimateEntityFeature.SWING_MODE
  91. ),
  92. )
  93. def test_icon(self):
  94. self.dps[HVACMODE_DPS] = True
  95. self.assertEqual(self.subject.icon, "mdi:radiator")
  96. self.dps[HVACMODE_DPS] = False
  97. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  98. self.dps[HVACMODE_DPS] = True
  99. self.dps[POWERLEVEL_DPS] = "stop"
  100. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  101. def test_temperature_unit_returns_celsius(self):
  102. self.assertEqual(self.subject.temperature_unit, UnitOfTemperature.CELSIUS)
  103. def test_precision(self):
  104. self.assertEqual(self.subject.precision, PRECISION_WHOLE)
  105. def test_target_temperature_in_eco_and_af_modes(self):
  106. self.dps[TEMPERATURE_DPS] = 25
  107. self.dps[ECOTEMP_DPS] = 15
  108. self.dps[PRESET_DPS] = "ECO"
  109. self.assertEqual(self.subject.target_temperature, 15)
  110. self.dps[PRESET_DPS] = "AF"
  111. self.assertIs(self.subject.target_temperature, None)
  112. def test_minimum_temperature(self):
  113. self.dps[PRESET_DPS] = "C"
  114. self.assertEqual(self.subject.min_temp, 5)
  115. self.dps[PRESET_DPS] = "ECO"
  116. self.assertEqual(self.subject.min_temp, 5)
  117. self.dps[PRESET_DPS] = "AF"
  118. self.assertIs(self.subject.min_temp, 5)
  119. def test_maximum_target_temperature(self):
  120. self.dps[PRESET_DPS] = "C"
  121. self.assertEqual(self.subject.max_temp, 35)
  122. self.dps[PRESET_DPS] = "ECO"
  123. self.assertEqual(self.subject.max_temp, 21)
  124. self.dps[PRESET_DPS] = "AF"
  125. self.assertIs(self.subject.max_temp, 5)
  126. async def test_legacy_set_temperature_with_preset_mode(self):
  127. async with assert_device_properties_set(
  128. self.subject._device, {PRESET_DPS: "C"}
  129. ):
  130. await self.subject.async_set_temperature(preset_mode="comfort")
  131. async def test_legacy_set_temperature_with_both_properties(self):
  132. async with assert_device_properties_set(
  133. self.subject._device,
  134. {
  135. TEMPERATURE_DPS: 25,
  136. PRESET_DPS: "C",
  137. },
  138. ):
  139. await self.subject.async_set_temperature(
  140. temperature=25, preset_mode="comfort"
  141. )
  142. async def test_set_target_temperature_in_eco_mode(self):
  143. self.dps[PRESET_DPS] = "ECO"
  144. async with assert_device_properties_set(
  145. self.subject._device, {ECOTEMP_DPS: 15}
  146. ):
  147. await self.subject.async_set_target_temperature(15)
  148. async def test_set_target_temperature_fails_outside_valid_range_in_eco(self):
  149. self.dps[PRESET_DPS] = "ECO"
  150. with self.assertRaisesRegex(
  151. ValueError, "eco_temperature \\(4\\) must be between 5 and 21"
  152. ):
  153. await self.subject.async_set_target_temperature(4)
  154. with self.assertRaisesRegex(
  155. ValueError, "eco_temperature \\(22\\) must be between 5 and 21"
  156. ):
  157. await self.subject.async_set_target_temperature(22)
  158. async def test_set_target_temperature_fails_in_anti_freeze(self):
  159. self.dps[PRESET_DPS] = "AF"
  160. with self.assertRaisesRegex(
  161. AttributeError, "temperature cannot be set at this time"
  162. ):
  163. await self.subject.async_set_target_temperature(25)
  164. def test_current_temperature(self):
  165. self.dps[CURRENTTEMP_DPS] = 25
  166. self.assertEqual(self.subject.current_temperature, 25)
  167. def test_humidity_unsupported(self):
  168. self.assertIsNone(self.subject.min_humidity)
  169. self.assertIsNone(self.subject.max_humidity)
  170. self.assertIsNone(self.subject.current_humidity)
  171. with self.assertRaises(NotImplementedError):
  172. self.subject.target_humidity
  173. async def test_set_humidity_unsupported(self):
  174. with self.assertRaises(NotImplementedError):
  175. await self.subject.async_set_humidity(50)
  176. def test_hvac_mode(self):
  177. self.dps[HVACMODE_DPS] = True
  178. self.assertEqual(self.subject.hvac_mode, HVACMode.HEAT)
  179. self.dps[HVACMODE_DPS] = False
  180. self.assertEqual(self.subject.hvac_mode, HVACMode.OFF)
  181. def test_hvac_modes(self):
  182. self.assertCountEqual(self.subject.hvac_modes, [HVACMode.OFF, HVACMode.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(HVACMode.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(HVACMode.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")