test_electriq_dehumidifier.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.fan import SUPPORT_PRESET_MODE
  4. from homeassistant.components.humidifier import SUPPORT_MODES
  5. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  6. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  7. from homeassistant.const import STATE_UNAVAILABLE
  8. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  9. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  10. from custom_components.tuya_local.generic.light import TuyaLocalLight
  11. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  12. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  13. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  14. from ..const import ELECTRIQ_DEHUMIDIFIER_PAYLOAD
  15. from ..helpers import assert_device_properties_set
  16. SWITCH_DPS = "1"
  17. MODE_DPS = "2"
  18. CURRENTHUMID_DPS = "3"
  19. HUMIDITY_DPS = "4"
  20. LOCK_DPS = "7"
  21. LIGHT_DPS = "10"
  22. PRESET_DPS = "102"
  23. CURRENTTEMP_DPS = "103"
  24. IONIZER_DPS = "104"
  25. class TestElectriqDehumidifier(IsolatedAsyncioTestCase):
  26. def setUp(self):
  27. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  28. self.addCleanup(device_patcher.stop)
  29. self.mock_device = device_patcher.start()
  30. cfg = TuyaDeviceConfig("electriq_dehumidifier.yaml")
  31. entities = {}
  32. entities[cfg.primary_entity.entity] = cfg.primary_entity
  33. for e in cfg.secondary_entities():
  34. entities[e.entity] = e
  35. self.humidifier_name = (
  36. "missing" if "humidifier" not in entities else entities["humidifier"].name
  37. )
  38. self.fan_name = "missing" if "fan" not in entities else entities["fan"].name
  39. self.light_name = (
  40. "missing" if "light" not in entities else entities["light"].name
  41. )
  42. self.lock_name = "missing" if "lock" not in entities else entities["lock"].name
  43. self.switch_name = (
  44. "missing" if "switch" not in entities else entities["switch"].name
  45. )
  46. self.subject = TuyaLocalHumidifier(
  47. self.mock_device(), entities.get("humidifier")
  48. )
  49. self.fan = TuyaLocalFan(self.mock_device(), entities.get("fan"))
  50. self.light = TuyaLocalLight(self.mock_device(), entities.get("light"))
  51. self.lock = TuyaLocalLock(self.mock_device(), entities.get("lock"))
  52. self.switch = TuyaLocalSwitch(self.mock_device(), entities.get("switch"))
  53. self.dps = ELECTRIQ_DEHUMIDIFIER_PAYLOAD.copy()
  54. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  55. def test_supported_features(self):
  56. self.assertEqual(self.subject.supported_features, SUPPORT_MODES)
  57. self.assertEqual(self.fan.supported_features, SUPPORT_PRESET_MODE)
  58. def test_should_poll(self):
  59. self.assertTrue(self.subject.should_poll)
  60. self.assertTrue(self.fan.should_poll)
  61. self.assertTrue(self.light.should_poll)
  62. self.assertTrue(self.lock.should_poll)
  63. self.assertTrue(self.switch.should_poll)
  64. def test_name_returns_device_name(self):
  65. self.assertEqual(self.subject.name, self.subject._device.name)
  66. self.assertEqual(self.fan.name, self.subject._device.name)
  67. self.assertEqual(self.light.name, self.subject._device.name)
  68. self.assertEqual(self.lock.name, self.subject._device.name)
  69. self.assertEqual(self.switch.name, self.subject._device.name)
  70. def test_friendly_name_returns_config_name(self):
  71. self.assertEqual(self.subject.friendly_name, self.humidifier_name)
  72. self.assertEqual(self.fan.friendly_name, self.fan_name)
  73. self.assertEqual(self.light.friendly_name, self.light_name)
  74. self.assertEqual(self.lock.friendly_name, self.lock_name)
  75. self.assertEqual(self.switch.friendly_name, self.switch_name)
  76. def test_unique_id_returns_device_unique_id(self):
  77. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  78. self.assertEqual(self.fan.unique_id, self.subject._device.unique_id)
  79. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  80. self.assertEqual(self.lock.unique_id, self.subject._device.unique_id)
  81. self.assertEqual(self.switch.unique_id, self.subject._device.unique_id)
  82. def test_device_info_returns_device_info_from_device(self):
  83. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  84. self.assertEqual(self.fan.device_info, self.subject._device.device_info)
  85. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  86. self.assertEqual(self.lock.device_info, self.subject._device.device_info)
  87. self.assertEqual(self.switch.device_info, self.subject._device.device_info)
  88. def test_entities_created(self):
  89. self.assertIsInstance(self.subject, TuyaLocalHumidifier)
  90. self.assertIsInstance(self.fan, TuyaLocalFan)
  91. self.assertIsInstance(self.light, TuyaLocalLight)
  92. self.assertIsInstance(self.lock, TuyaLocalLock)
  93. self.assertIsInstance(self.switch, TuyaLocalSwitch)
  94. def test_entities_are_same_device(self):
  95. self.assertEqual(self.fan._device, self.subject._device)
  96. self.assertEqual(self.light._device, self.subject._device)
  97. self.assertEqual(self.lock._device, self.subject._device)
  98. self.assertEqual(self.switch._device, self.subject._device)
  99. def test_icon(self):
  100. """Test that the icon is as expected."""
  101. self.dps[SWITCH_DPS] = True
  102. self.dps[MODE_DPS] = "auto"
  103. self.assertEqual(self.subject.icon, "mdi:air-humidifier")
  104. self.dps[SWITCH_DPS] = False
  105. self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
  106. self.dps[MODE_DPS] = "fan"
  107. self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
  108. self.dps[SWITCH_DPS] = True
  109. self.assertEqual(self.subject.icon, "mdi:air-purifier")
  110. self.dps[MODE_DPS] = "high"
  111. self.assertEqual(self.subject.icon, "mdi:tshirt-crew-outline")
  112. self.assertEqual(self.light.icon, "mdi:solar-power")
  113. self.assertEqual(self.switch.icon, "mdi:creation")
  114. def test_min_target_humidity(self):
  115. self.assertEqual(self.subject.min_humidity, 35)
  116. def test_max_target_humidity(self):
  117. self.assertEqual(self.subject.max_humidity, 80)
  118. def test_target_humidity(self):
  119. self.dps[HUMIDITY_DPS] = 55
  120. self.assertEqual(self.subject.target_humidity, 55)
  121. async def test_set_target_humidity_rounds_up_to_5_percent(self):
  122. async with assert_device_properties_set(
  123. self.subject._device,
  124. {HUMIDITY_DPS: 55},
  125. ):
  126. await self.subject.async_set_humidity(53)
  127. async def test_set_target_humidity_rounds_down_to_5_percent(self):
  128. async with assert_device_properties_set(
  129. self.subject._device,
  130. {HUMIDITY_DPS: 50},
  131. ):
  132. await self.subject.async_set_humidity(52)
  133. def test_is_on(self):
  134. self.dps[SWITCH_DPS] = True
  135. self.assertTrue(self.subject.is_on)
  136. self.assertTrue(self.fan.is_on)
  137. self.dps[SWITCH_DPS] = False
  138. self.assertFalse(self.subject.is_on)
  139. self.assertFalse(self.fan.is_on)
  140. self.dps[SWITCH_DPS] = None
  141. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  142. self.assertEqual(self.fan.is_on, STATE_UNAVAILABLE)
  143. async def test_turn_on(self):
  144. async with assert_device_properties_set(
  145. self.subject._device, {SWITCH_DPS: True}
  146. ):
  147. await self.subject.async_turn_on()
  148. async def test_turn_on(self):
  149. async with assert_device_properties_set(
  150. self.subject._device, {SWITCH_DPS: False}
  151. ):
  152. await self.subject.async_turn_off()
  153. async def test_fan_turn_on(self):
  154. async with assert_device_properties_set(
  155. self.subject._device, {SWITCH_DPS: True}
  156. ):
  157. await self.fan.async_turn_on()
  158. async def test_fan_turn_on(self):
  159. async with assert_device_properties_set(
  160. self.subject._device, {SWITCH_DPS: False}
  161. ):
  162. await self.fan.async_turn_off()
  163. def test_mode(self):
  164. self.dps[MODE_DPS] = "low"
  165. self.assertEqual(self.subject.mode, "Low")
  166. self.dps[MODE_DPS] = "high"
  167. self.assertEqual(self.subject.mode, "High")
  168. self.dps[MODE_DPS] = "auto"
  169. self.assertEqual(self.subject.mode, "Auto")
  170. self.dps[MODE_DPS] = "fan"
  171. self.assertEqual(self.subject.mode, "Air clean")
  172. async def test_set_mode_to_auto(self):
  173. async with assert_device_properties_set(
  174. self.subject._device,
  175. {
  176. MODE_DPS: "auto",
  177. },
  178. ):
  179. await self.subject.async_set_mode("Auto")
  180. self.subject._device.anticipate_property_value.assert_not_called()
  181. async def test_set_mode_to_low(self):
  182. async with assert_device_properties_set(
  183. self.subject._device,
  184. {
  185. MODE_DPS: "low",
  186. },
  187. ):
  188. await self.subject.async_set_mode("Low")
  189. self.subject._device.anticipate_property_value.assert_not_called()
  190. async def test_set_mode_to_high(self):
  191. async with assert_device_properties_set(
  192. self.subject._device,
  193. {
  194. MODE_DPS: "high",
  195. },
  196. ):
  197. await self.subject.async_set_mode("High")
  198. self.subject._device.anticipate_property_value.assert_not_called()
  199. async def test_set_mode_to_fan(self):
  200. async with assert_device_properties_set(
  201. self.subject._device,
  202. {
  203. MODE_DPS: "fan",
  204. },
  205. ):
  206. await self.subject.async_set_mode("Air clean")
  207. self.subject._device.anticipate_property_value.assert_not_called()
  208. def test_fan_preset_mode(self):
  209. self.dps[PRESET_DPS] = "45"
  210. self.assertEqual(self.fan.preset_mode, "Half open")
  211. self.dps[PRESET_DPS] = "90"
  212. self.assertEqual(self.fan.preset_mode, "Fully open")
  213. self.dps[PRESET_DPS] = "0_90"
  214. self.assertEqual(self.fan.preset_mode, "Oscillate")
  215. async def test_set_fan_to_half_open(self):
  216. async with assert_device_properties_set(
  217. self.subject._device,
  218. {
  219. PRESET_DPS: "45",
  220. },
  221. ):
  222. await self.fan.async_set_preset_mode("Half open")
  223. self.subject._device.anticipate_property_value.assert_not_called()
  224. async def test_set_fan_to_fully_open(self):
  225. async with assert_device_properties_set(
  226. self.subject._device,
  227. {
  228. PRESET_DPS: "90",
  229. },
  230. ):
  231. await self.fan.async_set_preset_mode("Fully open")
  232. self.subject._device.anticipate_property_value.assert_not_called()
  233. async def test_set_fan_to_oscillate(self):
  234. async with assert_device_properties_set(
  235. self.subject._device,
  236. {
  237. PRESET_DPS: "0_90",
  238. },
  239. ):
  240. await self.fan.async_set_preset_mode("Oscillate")
  241. self.subject._device.anticipate_property_value.assert_not_called()
  242. def test_lock_is_locked(self):
  243. self.dps[LOCK_DPS] = True
  244. self.assertTrue(self.lock.is_locked)
  245. self.dps[LOCK_DPS] = False
  246. self.assertFalse(self.lock.is_locked)
  247. self.dps[LOCK_DPS] = None
  248. self.assertFalse(self.lock.is_locked)
  249. async def test_lock_locks(self):
  250. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  251. await self.lock.async_lock()
  252. async def test_lock_unlocks(self):
  253. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  254. await self.lock.async_unlock()
  255. async def test_lock_update(self):
  256. result = AsyncMock()
  257. self.lock._device.async_refresh.return_value = result()
  258. await self.lock.async_update()
  259. self.lock._device.async_refresh.assert_called_once()
  260. result.assert_awaited()
  261. def test_light_is_on(self):
  262. self.dps[LIGHT_DPS] = True
  263. self.assertTrue(self.light.is_on)
  264. self.dps[LIGHT_DPS] = False
  265. self.assertFalse(self.light.is_on)
  266. async def test_light_turn_on(self):
  267. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  268. await self.light.async_turn_on()
  269. async def test_light_turn_off(self):
  270. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  271. await self.light.async_turn_off()
  272. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  273. self.dps[LIGHT_DPS] = False
  274. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  275. await self.light.async_toggle()
  276. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  277. self.dps[LIGHT_DPS] = True
  278. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  279. await self.light.async_toggle()
  280. async def test_light_update(self):
  281. result = AsyncMock()
  282. self.light._device.async_refresh.return_value = result()
  283. await self.light.async_update()
  284. self.light._device.async_refresh.assert_called_once()
  285. result.assert_awaited()
  286. def test_switch_is_on(self):
  287. self.dps[IONIZER_DPS] = True
  288. self.assertTrue(self.switch.is_on)
  289. self.dps[IONIZER_DPS] = False
  290. self.assertFalse(self.switch.is_on)
  291. async def test_switch_turn_on(self):
  292. async with assert_device_properties_set(
  293. self.switch._device, {IONIZER_DPS: True}
  294. ):
  295. await self.switch.async_turn_on()
  296. async def test_switch_turn_off(self):
  297. async with assert_device_properties_set(
  298. self.switch._device, {IONIZER_DPS: False}
  299. ):
  300. await self.switch.async_turn_off()
  301. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  302. self.dps[IONIZER_DPS] = False
  303. async with assert_device_properties_set(
  304. self.switch._device, {IONIZER_DPS: True}
  305. ):
  306. await self.switch.async_toggle()
  307. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  308. self.dps[IONIZER_DPS] = True
  309. async with assert_device_properties_set(
  310. self.switch._device, {IONIZER_DPS: False}
  311. ):
  312. await self.switch.async_toggle()
  313. async def test_switch_update(self):
  314. result = AsyncMock()
  315. self.switch._device.async_refresh.return_value = result()
  316. await self.switch.async_update()
  317. self.switch._device.async_refresh.assert_called_once()
  318. result.assert_awaited()
  319. def test_state_attributes(self):
  320. self.dps[CURRENTHUMID_DPS] = 50
  321. self.dps[CURRENTTEMP_DPS] = 21
  322. self.assertCountEqual(
  323. self.subject.device_state_attributes,
  324. {"current_humidity": 50, "current_temperature": 21},
  325. )
  326. self.assertEqual(self.fan.device_state_attributes, {})
  327. self.assertEqual(self.light.device_state_attributes, {})
  328. self.assertEqual(self.lock.device_state_attributes, {})
  329. self.assertEqual(self.switch.device_state_attributes, {})