test_goldair_dehumidifier.py 18 KB

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