test_goldair_dehumidifier.py 19 KB

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