test_goldair_dehumidifier.py 26 KB

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