test_goldair_dehumidifier.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  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.lock import STATE_LOCKED, STATE_UNLOCKED
  13. from homeassistant.const import STATE_UNAVAILABLE
  14. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  15. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  16. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  17. from custom_components.tuya_local.generic.light import TuyaLocalLight
  18. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  19. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  20. from ..const import DEHUMIDIFIER_PAYLOAD
  21. from ..helpers import assert_device_properties_set
  22. HVACMODE_DPS = "1"
  23. PRESET_DPS = "2"
  24. HUMIDITY_DPS = "4"
  25. AIRCLEAN_DPS = "5"
  26. FANMODE_DPS = "6"
  27. LOCK_DPS = "7"
  28. ERROR_DPS = "11"
  29. UNKNOWN12_DPS = "12"
  30. UNKNOWN101_DPS = "101"
  31. LIGHTOFF_DPS = "102"
  32. CURRENTTEMP_DPS = "103"
  33. CURRENTHUMID_DPS = "104"
  34. DEFROST_DPS = "105"
  35. PRESET_NORMAL = "0"
  36. PRESET_LOW = "1"
  37. PRESET_HIGH = "2"
  38. PRESET_DRY_CLOTHES = "3"
  39. ERROR_TANK = "Tank full or missing"
  40. class TestGoldairDehumidifier(IsolatedAsyncioTestCase):
  41. def setUp(self):
  42. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  43. self.addCleanup(device_patcher.stop)
  44. self.mock_device = device_patcher.start()
  45. cfg = TuyaDeviceConfig("goldair_dehumidifier.yaml")
  46. entities = {}
  47. entities[cfg.primary_entity.entity] = cfg.primary_entity
  48. for e in cfg.secondary_entities():
  49. entities[e.entity] = e
  50. self.climate_name = (
  51. "missing" if "climate" not in entities else entities["climate"].name
  52. )
  53. self.light_name = (
  54. "missing" if "light" not in entities else entities["light"].name
  55. )
  56. self.lock_name = "missing" if "lock" not in entities else entities["lock"].name
  57. self.humidifier_name = (
  58. "missing" if "humidifier" not in entities else entities["humidifier"].name
  59. )
  60. self.fan_name = "missing" if "fan" not in entities else entities["fan"].name
  61. self.subject = TuyaLocalClimate(self.mock_device(), entities.get("climate"))
  62. self.light = TuyaLocalLight(self.mock_device(), entities.get("light"))
  63. self.lock = TuyaLocalLock(self.mock_device(), entities.get("lock"))
  64. self.humidifier = TuyaLocalHumidifier(
  65. self.mock_device(), entities.get("humidifier")
  66. )
  67. self.fan = TuyaLocalFan(self.mock_device(), entities.get("fan"))
  68. self.dps = DEHUMIDIFIER_PAYLOAD.copy()
  69. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  70. def test_supported_features(self):
  71. self.assertEqual(
  72. self.subject.supported_features,
  73. SUPPORT_TARGET_HUMIDITY | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE,
  74. )
  75. def test_should_poll(self):
  76. self.assertTrue(self.subject.should_poll)
  77. self.assertTrue(self.fan.should_poll)
  78. self.assertTrue(self.light.should_poll)
  79. self.assertTrue(self.lock.should_poll)
  80. self.assertTrue(self.humidifier.should_poll)
  81. def test_name_returns_device_name(self):
  82. self.assertEqual(self.subject.name, self.subject._device.name)
  83. self.assertEqual(self.fan.name, self.subject._device.name)
  84. self.assertEqual(self.light.name, self.subject._device.name)
  85. self.assertEqual(self.lock.name, self.subject._device.name)
  86. self.assertEqual(self.humidifier.name, self.subject._device.name)
  87. def test_friendly_name_returns_config_name(self):
  88. self.assertEqual(self.subject.friendly_name, self.climate_name)
  89. self.assertEqual(self.fan.friendly_name, self.fan_name)
  90. self.assertEqual(self.light.friendly_name, self.light_name)
  91. self.assertEqual(self.lock.friendly_name, self.lock_name)
  92. self.assertEqual(self.humidifier.friendly_name, self.humidifier_name)
  93. def test_unique_id_returns_device_unique_id(self):
  94. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  95. self.assertEqual(self.fan.unique_id, self.subject._device.unique_id)
  96. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  97. self.assertEqual(self.lock.unique_id, self.subject._device.unique_id)
  98. self.assertEqual(self.humidifier.unique_id, self.subject._device.unique_id)
  99. def test_device_info_returns_device_info_from_device(self):
  100. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  101. self.assertEqual(self.fan.device_info, self.subject._device.device_info)
  102. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  103. self.assertEqual(self.lock.device_info, self.subject._device.device_info)
  104. self.assertEqual(self.humidifier.device_info, self.subject._device.device_info)
  105. @skip("Icon customisation not supported yet")
  106. def test_icon_is_always_standard_when_off_without_error(self):
  107. self.dps[ERROR_DPS] = None
  108. self.dps[HVACMODE_DPS] = False
  109. self.dps[AIRCLEAN_DPS] = False
  110. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  111. self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
  112. self.dps[AIRCLEAN_DPS] = True
  113. self.dps[PRESET_DPS] = PRESET_NORMAL
  114. self.assertEqual(self.subject.icon, "mdi:air-humidifier")
  115. @skip("Icon customisation not supported yet")
  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.subject.icon, "mdi:air-purifier")
  121. @skip("Icon customisation not supported yet")
  122. def test_icon_is_tshirt_when_dry_clothes_is_active(self):
  123. self.dps[ERROR_DPS] = None
  124. self.dps[HVACMODE_DPS] = True
  125. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  126. self.assertEqual(self.subject.icon, "mdi:tshirt-crew-outline")
  127. @skip("Icon customisation not supported yet")
  128. def test_icon_is_always_melting_snowflake_when_defrosting_and_tank_not_full(self):
  129. self.dps[DEFROST_DPS] = True
  130. self.dps[HVACMODE_DPS] = False
  131. self.assertEqual(self.subject.icon, "mdi:snowflake-melt")
  132. self.dps[HVACMODE_DPS] = True
  133. self.assertEqual(self.subject.icon, "mdi:snowflake-melt")
  134. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  135. self.assertEqual(self.subject.icon, "mdi:snowflake-melt")
  136. self.dps[AIRCLEAN_DPS] = True
  137. self.dps[PRESET_DPS] = PRESET_NORMAL
  138. self.assertEqual(self.subject.icon, "mdi:snowflake-melt")
  139. @skip("Icon customisation not supported yet")
  140. def test_icon_is_always_tank_when_tank_full_error_is_present(self):
  141. self.dps[ERROR_DPS] = 8
  142. self.dps[HVACMODE_DPS] = False
  143. self.assertEqual(self.subject.icon, "mdi:cup-water")
  144. self.dps[HVACMODE_DPS] = True
  145. self.assertEqual(self.subject.icon, "mdi:cup-water")
  146. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  147. self.assertEqual(self.subject.icon, "mdi:cup-water")
  148. self.dps[AIRCLEAN_DPS] = True
  149. self.dps[PRESET_DPS] = PRESET_NORMAL
  150. self.assertEqual(self.subject.icon, "mdi:cup-water")
  151. self.dps[DEFROST_DPS] = True
  152. self.assertEqual(self.subject.icon, "mdi:cup-water")
  153. def test_current_humidity(self):
  154. self.dps[CURRENTHUMID_DPS] = 47
  155. self.assertEqual(self.subject.current_humidity, 47)
  156. def test_min_target_humidity(self):
  157. self.assertEqual(self.subject.min_humidity, 30)
  158. self.assertEqual(self.humidifier.min_humidity, 30)
  159. def test_max_target_humidity(self):
  160. self.assertEqual(self.subject.max_humidity, 80)
  161. self.assertEqual(self.humidifier.max_humidity, 80)
  162. def test_target_humidity_in_normal_preset(self):
  163. self.dps[PRESET_DPS] = PRESET_NORMAL
  164. self.dps[HUMIDITY_DPS] = 55
  165. self.assertEqual(self.subject.target_humidity, 55)
  166. def test_target_humidity_in_humidifier(self):
  167. self.dps[PRESET_DPS] = PRESET_NORMAL
  168. self.dps[HUMIDITY_DPS] = 45
  169. self.assertEqual(self.humidifier.target_humidity, 45)
  170. def test_target_humidity_outside_normal_preset(self):
  171. self.dps[HUMIDITY_DPS] = 55
  172. self.dps[PRESET_DPS] = PRESET_HIGH
  173. self.assertIs(self.subject.target_humidity, None)
  174. self.dps[PRESET_DPS] = PRESET_LOW
  175. self.assertIs(self.subject.target_humidity, None)
  176. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  177. self.assertIs(self.subject.target_humidity, None)
  178. # self.dps[PRESET_DPS] = PRESET_NORMAL
  179. # self.dps[AIRCLEAN_DPS] = True
  180. # self.assertIs(self.subject.target_humidity, None)
  181. async def test_set_target_humidity_in_normal_preset_rounds_up_to_5_percent(self):
  182. self.dps[PRESET_DPS] = PRESET_NORMAL
  183. async with assert_device_properties_set(
  184. self.subject._device,
  185. {HUMIDITY_DPS: 55},
  186. ):
  187. await self.subject.async_set_humidity(53)
  188. async def test_set_target_humidity_in_normal_preset_rounds_down_to_5_percent(self):
  189. self.dps[PRESET_DPS] = PRESET_NORMAL
  190. async with assert_device_properties_set(
  191. self.subject._device,
  192. {HUMIDITY_DPS: 50},
  193. ):
  194. await self.subject.async_set_humidity(52)
  195. async def test_set_humidity_in_humidifier_rounds_up_to_5_percent(self):
  196. self.dps[PRESET_DPS] = PRESET_NORMAL
  197. async with assert_device_properties_set(
  198. self.humidifier._device,
  199. {HUMIDITY_DPS: 45},
  200. ):
  201. await self.humidifier.async_set_humidity(43)
  202. async def test_set_humidity_in_humidifier_rounds_down_to_5_percent(self):
  203. self.dps[PRESET_DPS] = PRESET_NORMAL
  204. async with assert_device_properties_set(
  205. self.humidifier._device,
  206. {HUMIDITY_DPS: 40},
  207. ):
  208. await self.humidifier.async_set_humidity(42)
  209. async def test_set_target_humidity_raises_error_outside_of_normal_preset(self):
  210. self.dps[PRESET_DPS] = PRESET_LOW
  211. with self.assertRaisesRegex(
  212. AttributeError, "humidity cannot be set at this time"
  213. ):
  214. await self.subject.async_set_humidity(50)
  215. self.dps[PRESET_DPS] = PRESET_HIGH
  216. with self.assertRaisesRegex(
  217. AttributeError, "humidity cannot be set at this time"
  218. ):
  219. await self.subject.async_set_humidity(50)
  220. self.dps[PRESET_DPS] = PRESET_LOW
  221. with self.assertRaisesRegex(
  222. AttributeError, "humidity cannot be set at this time"
  223. ):
  224. await self.subject.async_set_humidity(50)
  225. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  226. with self.assertRaisesRegex(
  227. AttributeError, "humidity cannot be set at this time"
  228. ):
  229. await self.subject.async_set_humidity(50)
  230. # self.dps[PRESET_DPS] = PRESET_NORMAL
  231. # self.dps[AIRCLEAN_DPS] = True
  232. # with self.assertRaisesRegex(
  233. # AttributeError, "humidity cannot be set at this time"
  234. # ):
  235. # await self.subject.async_set_humidity(50)
  236. def test_temperature_unit_returns_device_temperature_unit(self):
  237. self.assertEqual(
  238. self.subject.temperature_unit, self.subject._device.temperature_unit
  239. )
  240. def test_minimum_target_temperature(self):
  241. self.assertIs(self.subject.min_temp, None)
  242. def test_maximum_target_temperature(self):
  243. self.assertIs(self.subject.max_temp, None)
  244. def test_current_temperature(self):
  245. self.dps[CURRENTTEMP_DPS] = 25
  246. self.assertEqual(self.subject.current_temperature, 25)
  247. def test_hvac_mode(self):
  248. self.dps[HVACMODE_DPS] = True
  249. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_DRY)
  250. self.dps[HVACMODE_DPS] = False
  251. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  252. self.dps[HVACMODE_DPS] = None
  253. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  254. def test_hvac_modes(self):
  255. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_DRY])
  256. async def test_turn_on(self):
  257. async with assert_device_properties_set(
  258. self.subject._device, {HVACMODE_DPS: True}
  259. ):
  260. await self.subject.async_set_hvac_mode(HVAC_MODE_DRY)
  261. async def test_turn_off(self):
  262. async with assert_device_properties_set(
  263. self.subject._device, {HVACMODE_DPS: False}
  264. ):
  265. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  266. def test_humidifier_is_on(self):
  267. self.dps[HVACMODE_DPS] = True
  268. self.assertTrue(self.humidifier.is_on)
  269. self.dps[HVACMODE_DPS] = False
  270. self.assertFalse(self.humidifier.is_on)
  271. async def test_dehumidifier_turn_on(self):
  272. async with assert_device_properties_set(
  273. self.subject._device, {HVACMODE_DPS: True}
  274. ):
  275. await self.humidifier.async_turn_on()
  276. async def test_dehumidifier_turn_off(self):
  277. async with assert_device_properties_set(
  278. self.subject._device, {HVACMODE_DPS: False}
  279. ):
  280. await self.humidifier.async_turn_off()
  281. def test_preset_mode(self):
  282. self.dps[PRESET_DPS] = PRESET_NORMAL
  283. self.assertEqual(self.subject.preset_mode, "Normal")
  284. self.assertEqual(self.humidifier.mode, "Normal")
  285. self.dps[PRESET_DPS] = PRESET_LOW
  286. self.assertEqual(self.subject.preset_mode, "Low")
  287. self.assertEqual(self.humidifier.mode, "Low")
  288. self.dps[PRESET_DPS] = PRESET_HIGH
  289. self.assertEqual(self.subject.preset_mode, "High")
  290. self.assertEqual(self.humidifier.mode, "High")
  291. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  292. self.assertEqual(self.subject.preset_mode, "Dry clothes")
  293. self.assertEqual(self.humidifier.mode, "Dry clothes")
  294. self.dps[PRESET_DPS] = None
  295. self.assertEqual(self.subject.preset_mode, None)
  296. self.assertEqual(self.humidifier.mode, None)
  297. @skip("Conditions not included in config")
  298. def test_air_clean_is_surfaced_in_preset_mode(self):
  299. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  300. self.dps[AIRCLEAN_DPS] = True
  301. self.assertEqual(self.subject.preset_mode, "Air clean")
  302. def test_preset_modes(self):
  303. self.assertCountEqual(
  304. self.subject.preset_modes,
  305. [
  306. "Normal",
  307. "Low",
  308. "High",
  309. "Dry clothes",
  310. # "Air clean",
  311. ],
  312. )
  313. async def test_set_preset_mode_to_normal(self):
  314. async with assert_device_properties_set(
  315. self.subject._device,
  316. {
  317. PRESET_DPS: PRESET_NORMAL,
  318. },
  319. ):
  320. await self.subject.async_set_preset_mode("Normal")
  321. self.subject._device.anticipate_property_value.assert_not_called()
  322. @skip("Conditions not included in config")
  323. async def test_set_preset_mode_to_low(self):
  324. async with assert_device_properties_set(
  325. self.subject._device,
  326. {
  327. PRESET_DPS: PRESET_LOW,
  328. },
  329. ):
  330. await self.subject.async_set_preset_mode("Low")
  331. self.subject._device.anticipate_property_value.assert_called_once_with(
  332. FANMODE_DPS, "1"
  333. )
  334. @skip("Conditions not included in config")
  335. async def test_set_preset_mode_to_high(self):
  336. async with assert_device_properties_set(
  337. self.subject._device,
  338. {
  339. PRESET_DPS: PRESET_HIGH,
  340. },
  341. ):
  342. await self.subject.async_set_preset_mode("High")
  343. self.subject._device.anticipate_property_value.assert_called_once_with(
  344. FANMODE_DPS, "3"
  345. )
  346. @skip("Conditions not included in config")
  347. async def test_set_preset_mode_to_dry_clothes(self):
  348. async with assert_device_properties_set(
  349. self.subject._device,
  350. {
  351. PRESET_DPS: PRESET_DRY_CLOTHES,
  352. },
  353. ):
  354. await self.subject.async_set_preset_mode("Dry clothes")
  355. self.subject._device.anticipate_property_value.assert_called_once_with(
  356. FANMODE_DPS, "3"
  357. )
  358. @skip("Conditions not included in config")
  359. async def test_set_preset_mode_to_air_clean(self):
  360. async with assert_device_properties_set(
  361. self.subject._device, {AIRCLEAN_DPS: True}
  362. ):
  363. await self.subject.async_set_preset_mode("Air clean")
  364. self.subject._device.anticipate_property_value.assert_called_once_with(
  365. FANMODE_DPS, "1"
  366. )
  367. @skip("Conditions not included in config")
  368. def test_fan_mode_is_forced_to_high_in_high_dry_clothes_air_clean_presets(self):
  369. self.dps[FANMODE_DPS] = "1"
  370. self.dps[PRESET_DPS] = PRESET_HIGH
  371. self.assertEqual(self.subject.fan_mode, FAN_HIGH)
  372. self.assertEqual(self.fan.percentage, 100)
  373. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  374. self.assertEqual(self.subject.fan_mode, FAN_HIGH)
  375. self.assertEqual(self.fan.percentage, 100)
  376. self.dps[PRESET_DPS] = PRESET_NORMAL
  377. self.dps[AIRCLEAN_DPS] = True
  378. self.assertEqual(self.subject.fan_mode, FAN_HIGH)
  379. self.assertEqual(self.subject.percentage, 100)
  380. @skip("Conditions not included in config")
  381. def test_fan_mode_is_forced_to_low_in_low_preset(self):
  382. self.dps[FANMODE_DPS] = "3"
  383. self.dps[PRESET_DPS] = PRESET_LOW
  384. self.assertEqual(self.subject.fan_mode, FAN_LOW)
  385. self.assertEqual(self.fan.percentage, 50)
  386. def test_fan_mode_reflects_dps_mode_in_normal_preset(self):
  387. self.dps[PRESET_DPS] = PRESET_NORMAL
  388. self.dps[FANMODE_DPS] = "1"
  389. self.assertEqual(self.subject.fan_mode, FAN_LOW)
  390. self.assertEqual(self.fan.percentage, 50)
  391. self.dps[FANMODE_DPS] = "3"
  392. self.assertEqual(self.subject.fan_mode, FAN_HIGH)
  393. self.assertEqual(self.fan.percentage, 100)
  394. self.dps[FANMODE_DPS] = None
  395. self.assertEqual(self.subject.fan_mode, None)
  396. self.assertEqual(self.fan.percentage, None)
  397. @skip("Conditions not included in config")
  398. def test_fan_modes_reflect_preset_mode(self):
  399. self.dps[PRESET_DPS] = PRESET_NORMAL
  400. self.assertCountEqual(self.subject.fan_modes, [FAN_LOW, FAN_HIGH])
  401. self.assertEqual(self.fan.speed_count, 2)
  402. self.dps[PRESET_DPS] = PRESET_LOW
  403. self.assertEqual(self.subject.fan_modes, [FAN_LOW])
  404. self.assertEqual(self.fan.speed_count, 0)
  405. self.dps[PRESET_DPS] = PRESET_HIGH
  406. self.assertEqual(self.subject.fan_modes, [FAN_HIGH])
  407. self.assertEqual(self.fan.speed_count, 0)
  408. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  409. self.assertEqual(self.subject.fan_modes, [FAN_HIGH])
  410. self.assertEqual(self.fan.speed_count, 0)
  411. # self.dps[PRESET_DPS] = PRESET_NORMAL
  412. # self.dps[AIRCLEAN_DPS] = True
  413. # self.assertEqual(self.subject.fan_modes, [FAN_HIGH])
  414. # self.assertEqual(self.fan.speed_count, 0)
  415. async def test_set_fan_mode_to_low_succeeds_in_normal_preset(self):
  416. self.dps[PRESET_DPS] = PRESET_NORMAL
  417. async with assert_device_properties_set(
  418. self.subject._device,
  419. {FANMODE_DPS: "1"},
  420. ):
  421. await self.subject.async_set_fan_mode(FAN_LOW)
  422. async def test_set_fan_mode_to_high_succeeds_in_normal_preset(self):
  423. self.dps[PRESET_DPS] = PRESET_NORMAL
  424. async with assert_device_properties_set(
  425. self.subject._device,
  426. {FANMODE_DPS: "3"},
  427. ):
  428. await self.subject.async_set_fan_mode(FAN_HIGH)
  429. async def test_set_fan_50_succeeds_in_normal_preset(self):
  430. self.dps[PRESET_DPS] = PRESET_NORMAL
  431. async with assert_device_properties_set(
  432. self.fan._device,
  433. {FANMODE_DPS: "1"},
  434. ):
  435. await self.fan.async_set_percentage(50)
  436. async def test_set_fan_100_succeeds_in_normal_preset(self):
  437. self.dps[PRESET_DPS] = PRESET_NORMAL
  438. async with assert_device_properties_set(
  439. self.fan._device,
  440. {FANMODE_DPS: "3"},
  441. ):
  442. await self.fan.async_set_percentage(100)
  443. async def test_set_fan_30_snaps_to_50_in_normal_preset(self):
  444. self.dps[PRESET_DPS] = PRESET_NORMAL
  445. async with assert_device_properties_set(
  446. self.fan._device,
  447. {FANMODE_DPS: "1"},
  448. ):
  449. await self.fan.async_set_percentage(30)
  450. @skip("Restriction to listed options not supported yet")
  451. async def test_set_fan_mode_fails_with_invalid_mode(self):
  452. self.dps[PRESET_DPS] = PRESET_NORMAL
  453. with self.assertRaisesRegex(ValueError, "Invalid fan mode: something"):
  454. await self.subject.async_set_fan_mode("something")
  455. @skip("Conditions not yet supported for setting")
  456. async def test_set_fan_mode_fails_outside_normal_preset(self):
  457. self.dps[PRESET_DPS] = PRESET_LOW
  458. with self.assertRaisesRegex(
  459. AttributeError, "fan_mode cannot be set at this time"
  460. ):
  461. await self.subject.async_set_fan_mode(FAN_HIGH)
  462. self.dps[PRESET_DPS] = PRESET_HIGH
  463. with self.assertRaisesRegex(
  464. AttributeError, "fan_mode cannot be set at this time"
  465. ):
  466. await self.subject.async_set_fan_mode(FAN_HIGH)
  467. self.dps[PRESET_DPS] = PRESET_DRY_CLOTHES
  468. with self.assertRaisesRegex(
  469. AttributeError, "fan_mode cannot be set at this time"
  470. ):
  471. await self.subject.async_set_fan_mode(FAN_HIGH)
  472. # self.dps[PRESET_DPS] = PRESET_NORMAL
  473. # self.dps[AIRCLEAN_DPS] = True
  474. # with self.assertRaisesRegex(
  475. # ValueError, "Fan mode can only be changed while in Normal preset mode"
  476. # ):
  477. # await self.subject.async_set_fan_mode(FAN_HIGH)
  478. def test_device_state_attributes(self):
  479. self.dps[ERROR_DPS] = None
  480. self.dps[DEFROST_DPS] = False
  481. self.dps[AIRCLEAN_DPS] = False
  482. self.dps[UNKNOWN12_DPS] = "something"
  483. self.dps[UNKNOWN101_DPS] = False
  484. self.assertCountEqual(
  485. self.subject.device_state_attributes,
  486. {
  487. "error": STATE_UNAVAILABLE,
  488. "defrosting": False,
  489. "air_clean_on": False,
  490. "unknown_12": "something",
  491. "unknown_101": False,
  492. },
  493. )
  494. self.dps[ERROR_DPS] = 8
  495. self.dps[DEFROST_DPS] = True
  496. self.dps[AIRCLEAN_DPS] = True
  497. self.dps[UNKNOWN12_DPS] = "something else"
  498. self.dps[UNKNOWN101_DPS] = True
  499. self.assertCountEqual(
  500. self.subject.device_state_attributes,
  501. {
  502. "error": ERROR_TANK,
  503. "defrosting": True,
  504. "air_clean_on": True,
  505. "unknown_12": "something else",
  506. "unknown_101": True,
  507. },
  508. )
  509. async def test_update(self):
  510. result = AsyncMock()
  511. self.subject._device.async_refresh.return_value = result()
  512. await self.subject.async_update()
  513. self.subject._device.async_refresh.assert_called_once()
  514. result.assert_awaited()
  515. def test_lock_was_created(self):
  516. self.assertIsInstance(self.lock, TuyaLocalLock)
  517. def test_lock_is_same_device(self):
  518. self.assertEqual(self.lock._device, self.subject._device)
  519. def test_lock_state(self):
  520. self.dps[LOCK_DPS] = True
  521. self.assertEqual(self.lock.state, STATE_LOCKED)
  522. self.dps[LOCK_DPS] = False
  523. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  524. self.dps[LOCK_DPS] = None
  525. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  526. def test_lock_is_locked(self):
  527. self.dps[LOCK_DPS] = True
  528. self.assertTrue(self.lock.is_locked)
  529. self.dps[LOCK_DPS] = False
  530. self.assertFalse(self.lock.is_locked)
  531. self.dps[LOCK_DPS] = None
  532. self.assertFalse(self.lock.is_locked)
  533. async def async_test_lock_locks(self):
  534. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  535. await self.subject.async_lock()
  536. async def async_test_lock_unlocks(self):
  537. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  538. await self.subject.async_unlock()
  539. async def async_test_lock_update(self):
  540. result = AsyncMock()
  541. self.lock._device.async_refresh.return_value = result()
  542. await self.lock.async_update()
  543. self.lock._device.async_refresh.assert_called_once()
  544. result.assert_awaited()
  545. def test_light_was_created(self):
  546. self.assertIsInstance(self.light, TuyaLocalLight)
  547. def test_light_is_same_device(self):
  548. self.assertEqual(self.light._device, self.subject._device)
  549. def test_light_icon(self):
  550. self.dps[LIGHTOFF_DPS] = False
  551. self.assertEqual(self.light.icon, "mdi:led-on")
  552. self.dps[LIGHTOFF_DPS] = True
  553. self.assertEqual(self.light.icon, "mdi:led-off")
  554. def test_light_is_on(self):
  555. self.dps[LIGHTOFF_DPS] = False
  556. self.assertEqual(self.light.is_on, True)
  557. self.dps[LIGHTOFF_DPS] = True
  558. self.assertEqual(self.light.is_on, False)
  559. def test_light_state_attributes(self):
  560. self.assertEqual(self.light.device_state_attributes, {})
  561. async def test_light_turn_on(self):
  562. async with assert_device_properties_set(
  563. self.light._device, {LIGHTOFF_DPS: False}
  564. ):
  565. await self.light.async_turn_on()
  566. async def test_light_turn_off(self):
  567. async with assert_device_properties_set(
  568. self.light._device, {LIGHTOFF_DPS: True}
  569. ):
  570. await self.light.async_turn_off()
  571. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  572. self.dps[LIGHTOFF_DPS] = True
  573. async with assert_device_properties_set(
  574. self.light._device, {LIGHTOFF_DPS: False}
  575. ):
  576. await self.light.async_toggle()
  577. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  578. self.dps[LIGHTOFF_DPS] = False
  579. async with assert_device_properties_set(
  580. self.light._device, {LIGHTOFF_DPS: True}
  581. ):
  582. await self.light.async_toggle()
  583. async def test_light_update(self):
  584. result = AsyncMock()
  585. self.light._device.async_refresh.return_value = result()
  586. await self.light.async_update()
  587. self.light._device.async_refresh.assert_called_once()
  588. result.assert_awaited()