test_goldair_dehumidifier.py 19 KB

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