test_goldair_dehumidifier.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. from unittest.mock import ANY
  2. from homeassistant.components.binary_sensor import (
  3. DEVICE_CLASS_COLD,
  4. DEVICE_CLASS_PROBLEM,
  5. )
  6. from homeassistant.components.climate.const import (
  7. FAN_HIGH,
  8. FAN_LOW,
  9. HVAC_MODE_DRY,
  10. HVAC_MODE_OFF,
  11. SUPPORT_FAN_MODE,
  12. SUPPORT_PRESET_MODE,
  13. SUPPORT_TARGET_HUMIDITY,
  14. )
  15. from homeassistant.components.light import COLOR_MODE_ONOFF
  16. from homeassistant.const import (
  17. DEVICE_CLASS_HUMIDITY,
  18. DEVICE_CLASS_TEMPERATURE,
  19. STATE_UNAVAILABLE,
  20. TEMP_CELSIUS,
  21. )
  22. from ..const import DEHUMIDIFIER_PAYLOAD
  23. from ..helpers import assert_device_properties_set
  24. from ..mixins.binary_sensor import MultiBinarySensorTests
  25. from ..mixins.lock import BasicLockTests
  26. from ..mixins.number import BasicNumberTests
  27. from ..mixins.sensor import MultiSensorTests
  28. from ..mixins.switch import BasicSwitchTests, SwitchableTests
  29. from .base_device_tests import TuyaDeviceTestCase
  30. HVACMODE_DPS = "1"
  31. PRESET_DPS = "2"
  32. HUMIDITY_DPS = "4"
  33. AIRCLEAN_DPS = "5"
  34. FANMODE_DPS = "6"
  35. LOCK_DPS = "7"
  36. ERROR_DPS = "11"
  37. TIMER_DPS = "12"
  38. UNKNOWN101_DPS = "101"
  39. LIGHTOFF_DPS = "102"
  40. CURRENTTEMP_DPS = "103"
  41. CURRENTHUMID_DPS = "104"
  42. DEFROST_DPS = "105"
  43. PRESET_NORMAL = "0"
  44. PRESET_LOW = "1"
  45. PRESET_HIGH = "2"
  46. PRESET_DRY_CLOTHES = "3"
  47. ERROR_TANK = "Tank full or missing"
  48. class TestGoldairDehumidifier(
  49. BasicLockTests,
  50. BasicNumberTests,
  51. BasicSwitchTests,
  52. MultiBinarySensorTests,
  53. MultiSensorTests,
  54. SwitchableTests,
  55. TuyaDeviceTestCase,
  56. ):
  57. __test__ = True
  58. def setUp(self):
  59. self.setUpForConfig("goldair_dehumidifier.yaml", DEHUMIDIFIER_PAYLOAD)
  60. self.subject = self.entities.get("humidifier")
  61. self.setUpSwitchable(HVACMODE_DPS, self.subject)
  62. self.fan = self.entities.get("fan")
  63. self.climate = self.entities.get("climate_dehumidifier_as_climate")
  64. # BasicLightTests mixin is not used here because the switch is inverted
  65. self.light = self.entities.get("light_display")
  66. self.setUpBasicLock(LOCK_DPS, self.entities.get("lock_child_lock"))
  67. self.setUpBasicSwitch(AIRCLEAN_DPS, self.entities.get("switch_air_clean"))
  68. self.setUpBasicNumber(TIMER_DPS, self.entities.get("number_timer"), max=24)
  69. self.setUpMultiSensors(
  70. [
  71. {
  72. "name": "sensor_current_temperature",
  73. "dps": CURRENTTEMP_DPS,
  74. "unit": TEMP_CELSIUS,
  75. "device_class": DEVICE_CLASS_TEMPERATURE,
  76. "state_class": "measurement",
  77. },
  78. {
  79. "name": "sensor_current_humidity",
  80. "dps": CURRENTHUMID_DPS,
  81. "unit": "%",
  82. "device_class": DEVICE_CLASS_HUMIDITY,
  83. "state_class": "measurement",
  84. },
  85. ]
  86. )
  87. self.setUpMultiBinarySensors(
  88. [
  89. {
  90. "name": "binary_sensor_tank",
  91. "dps": ERROR_DPS,
  92. "device_class": DEVICE_CLASS_PROBLEM,
  93. "testdata": (8, 0),
  94. },
  95. {
  96. "name": "binary_sensor_defrost",
  97. "dps": DEFROST_DPS,
  98. "device_class": DEVICE_CLASS_COLD,
  99. },
  100. ]
  101. )
  102. def test_supported_features(self):
  103. self.assertEqual(
  104. self.climate.supported_features,
  105. SUPPORT_TARGET_HUMIDITY | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE,
  106. )
  107. def test_icon_is_always_standard_when_off_without_error(self):
  108. self.dps[ERROR_DPS] = None
  109. self.dps[HVACMODE_DPS] = False
  110. self.dps[AIRCLEAN_DPS] = False
  111. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  112. self.assertEqual(self.climate.icon, "mdi:air-humidifier-off")
  113. self.dps[AIRCLEAN_DPS] = True
  114. self.dps[PRESET_DPS] = PRESET_NORMAL
  115. self.assertEqual(self.climate.icon, "mdi:air-humidifier-off")
  116. def test_icon_is_purifier_when_air_clean_is_active(self):
  117. self.dps[ERROR_DPS] = None
  118. self.dps[HVACMODE_DPS] = True
  119. self.dps[AIRCLEAN_DPS] = True
  120. self.assertEqual(self.climate.icon, "mdi:air-purifier")
  121. def test_icon_is_tshirt_when_dry_clothes_is_active(self):
  122. self.dps[ERROR_DPS] = None
  123. self.dps[HVACMODE_DPS] = True
  124. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  125. self.assertEqual(self.climate.icon, "mdi:tshirt-crew-outline")
  126. def test_icon_is_always_melting_snowflake_when_defrosting_and_tank_not_full(self):
  127. self.dps[DEFROST_DPS] = True
  128. self.dps[HVACMODE_DPS] = False
  129. self.assertEqual(self.climate.icon, "mdi:snowflake-melt")
  130. self.dps[HVACMODE_DPS] = True
  131. self.assertEqual(self.climate.icon, "mdi:snowflake-melt")
  132. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  133. self.assertEqual(self.climate.icon, "mdi:snowflake-melt")
  134. self.dps[AIRCLEAN_DPS] = True
  135. self.dps[PRESET_DPS] = PRESET_NORMAL
  136. self.assertEqual(self.climate.icon, "mdi:snowflake-melt")
  137. def test_icon_is_always_tank_when_tank_full_error_is_present(self):
  138. self.dps[ERROR_DPS] = 8
  139. self.dps[HVACMODE_DPS] = False
  140. self.assertEqual(self.climate.icon, "mdi:cup-water")
  141. self.dps[HVACMODE_DPS] = True
  142. self.assertEqual(self.climate.icon, "mdi:cup-water")
  143. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  144. self.assertEqual(self.climate.icon, "mdi:cup-water")
  145. self.dps[AIRCLEAN_DPS] = True
  146. self.dps[PRESET_DPS] = PRESET_NORMAL
  147. self.assertEqual(self.climate.icon, "mdi:cup-water")
  148. self.dps[DEFROST_DPS] = True
  149. self.assertEqual(self.climate.icon, "mdi:cup-water")
  150. def test_current_humidity(self):
  151. self.dps[CURRENTHUMID_DPS] = 47
  152. self.assertEqual(self.climate.current_humidity, 47)
  153. def test_min_target_humidity(self):
  154. self.assertEqual(self.climate.min_humidity, 30)
  155. self.assertEqual(self.subject.min_humidity, 30)
  156. def test_max_target_humidity(self):
  157. self.assertEqual(self.climate.max_humidity, 80)
  158. self.assertEqual(self.subject.max_humidity, 80)
  159. def test_target_humidity_in_normal_preset(self):
  160. self.dps[PRESET_DPS] = PRESET_NORMAL
  161. self.dps[HUMIDITY_DPS] = 55
  162. self.assertEqual(self.climate.target_humidity, 55)
  163. def test_target_humidity_in_humidifier(self):
  164. self.dps[PRESET_DPS] = PRESET_NORMAL
  165. self.dps[HUMIDITY_DPS] = 45
  166. self.assertEqual(self.subject.target_humidity, 45)
  167. def test_target_humidity_outside_normal_preset(self):
  168. self.dps[HUMIDITY_DPS] = 55
  169. self.dps[PRESET_DPS] = PRESET_HIGH
  170. self.assertIs(self.climate.target_humidity, None)
  171. self.dps[PRESET_DPS] = PRESET_LOW
  172. self.assertIs(self.climate.target_humidity, None)
  173. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  174. self.assertIs(self.climate.target_humidity, None)
  175. async def test_set_target_humidity_in_normal_preset_rounds_up_to_5_percent(self):
  176. self.dps[PRESET_DPS] = PRESET_NORMAL
  177. async with assert_device_properties_set(
  178. self.climate._device,
  179. {HUMIDITY_DPS: 55},
  180. ):
  181. await self.climate.async_set_humidity(53)
  182. async def test_set_target_humidity_in_normal_preset_rounds_down_to_5_percent(self):
  183. self.dps[PRESET_DPS] = PRESET_NORMAL
  184. async with assert_device_properties_set(
  185. self.climate._device,
  186. {HUMIDITY_DPS: 50},
  187. ):
  188. await self.climate.async_set_humidity(52)
  189. async def test_set_humidity_in_humidifier_rounds_up_to_5_percent(self):
  190. self.dps[PRESET_DPS] = PRESET_NORMAL
  191. async with assert_device_properties_set(
  192. self.subject._device,
  193. {HUMIDITY_DPS: 45},
  194. ):
  195. await self.subject.async_set_humidity(43)
  196. async def test_set_humidity_in_humidifier_rounds_down_to_5_percent(self):
  197. self.dps[PRESET_DPS] = PRESET_NORMAL
  198. async with assert_device_properties_set(
  199. self.subject._device,
  200. {HUMIDITY_DPS: 40},
  201. ):
  202. await self.subject.async_set_humidity(42)
  203. async def test_set_target_humidity_raises_error_outside_of_normal_preset(self):
  204. self.dps[PRESET_DPS] = PRESET_LOW
  205. with self.assertRaisesRegex(
  206. AttributeError, "humidity cannot be set at this time"
  207. ):
  208. await self.climate.async_set_humidity(50)
  209. self.dps[PRESET_DPS] = PRESET_HIGH
  210. with self.assertRaisesRegex(
  211. AttributeError, "humidity cannot be set at this time"
  212. ):
  213. await self.climate.async_set_humidity(50)
  214. self.dps[PRESET_DPS] = PRESET_LOW
  215. with self.assertRaisesRegex(
  216. AttributeError, "humidity cannot be set at this time"
  217. ):
  218. await self.climate.async_set_humidity(50)
  219. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  220. with self.assertRaisesRegex(
  221. AttributeError, "humidity cannot be set at this time"
  222. ):
  223. await self.climate.async_set_humidity(50)
  224. def test_temperature_unit_returns_device_temperature_unit(self):
  225. self.assertEqual(
  226. self.climate.temperature_unit,
  227. self.climate._device.temperature_unit,
  228. )
  229. def test_minimum_target_temperature(self):
  230. self.assertIs(self.climate.min_temp, None)
  231. def test_maximum_target_temperature(self):
  232. self.assertIs(self.climate.max_temp, None)
  233. def test_current_temperature(self):
  234. self.dps[CURRENTTEMP_DPS] = 25
  235. self.assertEqual(self.climate.current_temperature, 25)
  236. def test_climate_hvac_mode(self):
  237. self.dps[HVACMODE_DPS] = True
  238. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_DRY)
  239. self.dps[HVACMODE_DPS] = False
  240. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
  241. self.dps[HVACMODE_DPS] = None
  242. self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
  243. def test_climate_hvac_modes(self):
  244. self.assertCountEqual(self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_DRY])
  245. async def test_climate_set_hvac_mode_to_dry(self):
  246. async with assert_device_properties_set(
  247. self.climate._device, {HVACMODE_DPS: True}
  248. ):
  249. await self.climate.async_set_hvac_mode(HVAC_MODE_DRY)
  250. async def test_climate_set_hvac_mode_to_off(self):
  251. async with assert_device_properties_set(
  252. self.climate._device, {HVACMODE_DPS: False}
  253. ):
  254. await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
  255. def test_preset_mode(self):
  256. self.dps[PRESET_DPS] = PRESET_NORMAL
  257. self.assertEqual(self.climate.preset_mode, "Normal")
  258. self.assertEqual(self.subject.mode, "Normal")
  259. self.dps[PRESET_DPS] = PRESET_LOW
  260. self.assertEqual(self.climate.preset_mode, "Low")
  261. self.assertEqual(self.subject.mode, "Low")
  262. self.dps[PRESET_DPS] = PRESET_HIGH
  263. self.assertEqual(self.climate.preset_mode, "High")
  264. self.assertEqual(self.subject.mode, "High")
  265. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  266. self.assertEqual(self.climate.preset_mode, "Dry clothes")
  267. self.assertEqual(self.subject.mode, "Dry clothes")
  268. self.dps[PRESET_DPS] = None
  269. self.assertEqual(self.climate.preset_mode, None)
  270. self.assertEqual(self.subject.mode, None)
  271. def test_air_clean_is_surfaced_in_preset_mode(self):
  272. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  273. self.dps[AIRCLEAN_DPS] = True
  274. self.assertEqual(self.climate.preset_mode, "Air clean")
  275. def test_preset_modes(self):
  276. self.assertCountEqual(
  277. self.climate.preset_modes,
  278. [
  279. "Normal",
  280. "Low",
  281. "High",
  282. "Dry clothes",
  283. "Air clean",
  284. ],
  285. )
  286. async def test_set_preset_mode_to_normal(self):
  287. async with assert_device_properties_set(
  288. self.climate._device,
  289. {
  290. PRESET_DPS: PRESET_NORMAL,
  291. AIRCLEAN_DPS: False,
  292. },
  293. ):
  294. await self.climate.async_set_preset_mode("Normal")
  295. self.climate._device.anticipate_property_value.assert_not_called()
  296. async def test_set_preset_mode_to_low(self):
  297. async with assert_device_properties_set(
  298. self.climate._device,
  299. {
  300. PRESET_DPS: PRESET_LOW,
  301. AIRCLEAN_DPS: False,
  302. },
  303. ):
  304. await self.climate.async_set_preset_mode("Low")
  305. async def test_set_preset_mode_to_high(self):
  306. async with assert_device_properties_set(
  307. self.climate._device,
  308. {
  309. PRESET_DPS: PRESET_HIGH,
  310. AIRCLEAN_DPS: False,
  311. },
  312. ):
  313. await self.climate.async_set_preset_mode("High")
  314. async def test_set_preset_mode_to_dry_clothes(self):
  315. async with assert_device_properties_set(
  316. self.climate._device,
  317. {
  318. PRESET_DPS: PRESET_DRY_CLOTHES,
  319. AIRCLEAN_DPS: False,
  320. },
  321. ):
  322. await self.climate.async_set_preset_mode("Dry clothes")
  323. async def test_set_preset_mode_to_air_clean(self):
  324. async with assert_device_properties_set(
  325. self.climate._device, {AIRCLEAN_DPS: True, PRESET_DPS: ANY}
  326. ):
  327. await self.climate.async_set_preset_mode("Air clean")
  328. def test_fan_mode_reflects_dps_mode_in_normal_preset(self):
  329. self.dps[PRESET_DPS] = PRESET_NORMAL
  330. self.dps[FANMODE_DPS] = "1"
  331. self.assertEqual(self.climate.fan_mode, FAN_LOW)
  332. self.assertEqual(self.fan.percentage, 50)
  333. self.dps[FANMODE_DPS] = "3"
  334. self.assertEqual(self.climate.fan_mode, FAN_HIGH)
  335. self.assertEqual(self.fan.percentage, 100)
  336. self.dps[FANMODE_DPS] = None
  337. self.assertEqual(self.climate.fan_mode, None)
  338. self.assertEqual(self.fan.percentage, None)
  339. async def test_set_fan_mode_to_low_succeeds_in_normal_preset(self):
  340. self.dps[PRESET_DPS] = PRESET_NORMAL
  341. async with assert_device_properties_set(
  342. self.climate._device,
  343. {FANMODE_DPS: "1"},
  344. ):
  345. await self.climate.async_set_fan_mode(FAN_LOW)
  346. async def test_set_fan_mode_to_high_succeeds_in_normal_preset(self):
  347. self.dps[PRESET_DPS] = PRESET_NORMAL
  348. async with assert_device_properties_set(
  349. self.climate._device,
  350. {FANMODE_DPS: "3"},
  351. ):
  352. await self.climate.async_set_fan_mode(FAN_HIGH)
  353. async def test_set_fan_50_succeeds_in_normal_preset(self):
  354. self.dps[PRESET_DPS] = PRESET_NORMAL
  355. async with assert_device_properties_set(
  356. self.fan._device,
  357. {FANMODE_DPS: "1"},
  358. ):
  359. await self.fan.async_set_percentage(50)
  360. async def test_set_fan_100_succeeds_in_normal_preset(self):
  361. self.dps[PRESET_DPS] = PRESET_NORMAL
  362. async with assert_device_properties_set(
  363. self.fan._device,
  364. {FANMODE_DPS: "3"},
  365. ):
  366. await self.fan.async_set_percentage(100)
  367. async def test_set_fan_30_snaps_to_50_in_normal_preset(self):
  368. self.dps[PRESET_DPS] = PRESET_NORMAL
  369. async with assert_device_properties_set(
  370. self.fan._device,
  371. {FANMODE_DPS: "1"},
  372. ):
  373. await self.fan.async_set_percentage(30)
  374. def test_device_state_attributes(self):
  375. self.dps[ERROR_DPS] = None
  376. self.dps[DEFROST_DPS] = False
  377. self.dps[AIRCLEAN_DPS] = False
  378. self.dps[UNKNOWN101_DPS] = False
  379. self.assertDictEqual(
  380. self.climate.device_state_attributes,
  381. {
  382. "error": None,
  383. "defrosting": False,
  384. "air_clean_on": False,
  385. "unknown_101": False,
  386. },
  387. )
  388. self.dps[ERROR_DPS] = 8
  389. self.dps[DEFROST_DPS] = True
  390. self.dps[AIRCLEAN_DPS] = True
  391. self.dps[UNKNOWN101_DPS] = True
  392. self.assertDictEqual(
  393. self.climate.device_state_attributes,
  394. {
  395. "error": ERROR_TANK,
  396. "defrosting": True,
  397. "air_clean_on": True,
  398. "unknown_101": True,
  399. },
  400. )
  401. def test_light_supported_color_modes(self):
  402. self.assertCountEqual(
  403. self.light.supported_color_modes,
  404. [COLOR_MODE_ONOFF],
  405. )
  406. def test_light_color_mode(self):
  407. self.assertEqual(self.light.color_mode, COLOR_MODE_ONOFF)
  408. def test_light_icon(self):
  409. self.dps[LIGHTOFF_DPS] = False
  410. self.assertEqual(self.light.icon, "mdi:led-on")
  411. self.dps[LIGHTOFF_DPS] = True
  412. self.assertEqual(self.light.icon, "mdi:led-off")
  413. def test_light_is_on(self):
  414. self.dps[LIGHTOFF_DPS] = False
  415. self.assertEqual(self.light.is_on, True)
  416. self.dps[LIGHTOFF_DPS] = True
  417. self.assertEqual(self.light.is_on, False)
  418. def test_light_state_attributes(self):
  419. self.assertEqual(self.light.device_state_attributes, {})
  420. async def test_light_turn_on(self):
  421. async with assert_device_properties_set(
  422. self.light._device, {LIGHTOFF_DPS: False}
  423. ):
  424. await self.light.async_turn_on()
  425. async def test_light_turn_off(self):
  426. async with assert_device_properties_set(
  427. self.light._device, {LIGHTOFF_DPS: True}
  428. ):
  429. await self.light.async_turn_off()
  430. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  431. self.dps[LIGHTOFF_DPS] = True
  432. async with assert_device_properties_set(
  433. self.light._device, {LIGHTOFF_DPS: False}
  434. ):
  435. await self.light.async_toggle()
  436. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  437. self.dps[LIGHTOFF_DPS] = False
  438. async with assert_device_properties_set(
  439. self.light._device, {LIGHTOFF_DPS: True}
  440. ):
  441. await self.light.async_toggle()
  442. def test_switch_icon(self):
  443. self.assertEqual(self.basicSwitch.icon, "mdi:air-purifier")