test_goldair_gpph_heater.py 11 KB

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