test_goldair_gpph_heater.py 11 KB

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