test_eanons_humidifier.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_MEDIUM,
  6. FAN_LOW,
  7. HVAC_MODE_DRY,
  8. HVAC_MODE_OFF,
  9. SUPPORT_FAN_MODE,
  10. SUPPORT_PRESET_MODE,
  11. SUPPORT_TARGET_HUMIDITY,
  12. )
  13. from homeassistant.components.fan import (
  14. SUPPORT_SET_SPEED,
  15. )
  16. from homeassistant.components.humidifier.const import (
  17. MODE_NORMAL,
  18. MODE_AUTO,
  19. MODE_SLEEP,
  20. SUPPORT_MODES,
  21. )
  22. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  23. from homeassistant.const import STATE_UNAVAILABLE
  24. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  25. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  26. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  27. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  28. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  29. from ..const import EANONS_HUMIDIFIER_PAYLOAD
  30. from ..helpers import assert_device_properties_set
  31. FANMODE_DPS = "2"
  32. TIMERHR_DPS = "3"
  33. TIMER_DPS = "4"
  34. ERROR_DPS = "9"
  35. HVACMODE_DPS = "10"
  36. PRESET_DPS = "12"
  37. HUMIDITY_DPS = "15"
  38. CURRENTHUMID_DPS = "16"
  39. SWITCH_DPS = "22"
  40. class TestEanonsHumidifier(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("eanons_humidifier.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.switch_name = (
  54. "missing" if "switch" not in entities else entities["switch"].name
  55. )
  56. self.humidifier_name = (
  57. "missing" if "humidifier" not in entities else entities["humidifier"].name
  58. )
  59. self.fan_name = "missing" if "fan" not in entities else entities["fan"].name
  60. self.subject = TuyaLocalHumidifier(
  61. self.mock_device(), entities.get("humidifier")
  62. )
  63. self.climate = TuyaLocalClimate(self.mock_device(), entities.get("climate"))
  64. self.switch = TuyaLocalSwitch(self.mock_device(), entities.get("switch"))
  65. self.fan = TuyaLocalFan(self.mock_device(), entities.get("fan"))
  66. self.dps = EANONS_HUMIDIFIER_PAYLOAD.copy()
  67. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  68. def test_supported_features(self):
  69. self.assertEqual(
  70. self.climate.supported_features,
  71. SUPPORT_TARGET_HUMIDITY | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE,
  72. )
  73. self.assertEqual(self.subject.supported_features, SUPPORT_MODES)
  74. self.assertEqual(self.fan.supported_features, SUPPORT_SET_SPEED)
  75. def test_shouldPoll(self):
  76. self.assertTrue(self.subject.should_poll)
  77. self.assertTrue(self.climate.should_poll)
  78. self.assertTrue(self.fan.should_poll)
  79. self.assertTrue(self.switch.should_poll)
  80. def test_name_returns_device_name(self):
  81. self.assertEqual(self.subject.name, self.subject._device.name)
  82. self.assertEqual(self.climate.name, self.subject._device.name)
  83. self.assertEqual(self.fan.name, self.subject._device.name)
  84. self.assertEqual(self.switch.name, self.subject._device.name)
  85. def test_friendly_name_returns_config_name(self):
  86. self.assertEqual(self.subject.friendly_name, self.humidifier_name)
  87. self.assertEqual(self.climate.friendly_name, self.climate_name)
  88. self.assertEqual(self.fan.friendly_name, self.fan_name)
  89. self.assertEqual(self.switch.friendly_name, self.switch_name)
  90. def test_unique_id_returns_device_unique_id(self):
  91. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  92. self.assertEqual(self.climate.unique_id, self.subject._device.unique_id)
  93. self.assertEqual(self.fan.unique_id, self.subject._device.unique_id)
  94. self.assertEqual(self.switch.unique_id, self.subject._device.unique_id)
  95. def test_device_info_returns_device_info_from_device(self):
  96. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  97. self.assertEqual(self.climate.device_info, self.subject._device.device_info)
  98. self.assertEqual(self.fan.device_info, self.subject._device.device_info)
  99. self.assertEqual(self.switch.device_info, self.subject._device.device_info)
  100. def test_climate_icon_is_humidifier(self):
  101. """Test that the icon is as expected."""
  102. self.dps[HVACMODE_DPS] = True
  103. self.assertEqual(self.climate.icon, "mdi:air-humidifier")
  104. self.dps[HVACMODE_DPS] = False
  105. self.assertEqual(self.climate.icon, "mdi:air-humidifier-off")
  106. def test_icon_is_humidifier(self):
  107. """Test that the icon is as expected."""
  108. self.dps[HVACMODE_DPS] = True
  109. self.assertEqual(self.subject.icon, "mdi:air-humidifier")
  110. self.dps[HVACMODE_DPS] = False
  111. self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
  112. def test_current_humidity(self):
  113. self.dps[CURRENTHUMID_DPS] = 47
  114. self.assertEqual(self.climate.current_humidity, 47)
  115. def test_min_target_humidity(self):
  116. self.assertEqual(self.climate.min_humidity, 40)
  117. self.assertEqual(self.subject.min_humidity, 40)
  118. def test_max_target_humidity(self):
  119. self.assertEqual(self.climate.max_humidity, 90)
  120. self.assertEqual(self.subject.max_humidity, 90)
  121. def test_target_humidity(self):
  122. self.dps[HUMIDITY_DPS] = 55
  123. self.assertEqual(self.climate.target_humidity, 55)
  124. self.assertEqual(self.subject.target_humidity, 55)
  125. def test_climate_hvac_mode(self):
  126. self.dps[HVACMODE_DPS] = True
  127. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_DRY)
  128. self.dps[HVACMODE_DPS] = False
  129. self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
  130. self.dps[HVACMODE_DPS] = None
  131. self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
  132. def test_climate_hvac_modes(self):
  133. self.assertCountEqual(self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_DRY])
  134. def test_is_on(self):
  135. self.dps[HVACMODE_DPS] = True
  136. self.assertTrue(self.subject.is_on)
  137. self.assertTrue(self.fan.is_on)
  138. self.dps[HVACMODE_DPS] = False
  139. self.assertFalse(self.subject.is_on)
  140. self.assertFalse(self.fan.is_on)
  141. self.dps[HVACMODE_DPS] = None
  142. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  143. self.assertEqual(self.fan.is_on, STATE_UNAVAILABLE)
  144. async def test_climate_turn_on(self):
  145. async with assert_device_properties_set(
  146. self.climate._device, {HVACMODE_DPS: True}
  147. ):
  148. await self.climate.async_set_hvac_mode(HVAC_MODE_DRY)
  149. async def test_climate_turn_off(self):
  150. async with assert_device_properties_set(
  151. self.climate._device, {HVACMODE_DPS: False}
  152. ):
  153. await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
  154. async def test_turn_on(self):
  155. async with assert_device_properties_set(
  156. self.subject._device, {HVACMODE_DPS: True}
  157. ):
  158. await self.subject.async_turn_on()
  159. async def test_turn_off(self):
  160. async with assert_device_properties_set(
  161. self.subject._device, {HVACMODE_DPS: False}
  162. ):
  163. await self.subject.async_turn_off()
  164. async def test_fan_turn_on(self):
  165. async with assert_device_properties_set(
  166. self.subject._device, {HVACMODE_DPS: True}
  167. ):
  168. await self.fan.async_turn_on()
  169. async def test_fan_turn_off(self):
  170. async with assert_device_properties_set(
  171. self.subject._device, {HVACMODE_DPS: False}
  172. ):
  173. await self.fan.async_turn_off()
  174. def test_preset_mode(self):
  175. self.dps[PRESET_DPS] = "sleep"
  176. self.assertEqual(self.climate.preset_mode, MODE_SLEEP)
  177. self.assertEqual(self.subject.mode, MODE_SLEEP)
  178. self.dps[PRESET_DPS] = "humidity"
  179. self.assertEqual(self.climate.preset_mode, MODE_AUTO)
  180. self.assertEqual(self.subject.mode, MODE_AUTO)
  181. self.dps[PRESET_DPS] = "work"
  182. self.assertEqual(self.climate.preset_mode, MODE_NORMAL)
  183. self.assertEqual(self.subject.mode, MODE_NORMAL)
  184. self.dps[PRESET_DPS] = None
  185. self.assertEqual(self.climate.preset_mode, None)
  186. self.assertEqual(self.subject.mode, None)
  187. def test_preset_modes(self):
  188. self.assertCountEqual(
  189. self.climate.preset_modes,
  190. {MODE_NORMAL, MODE_SLEEP, MODE_AUTO},
  191. )
  192. self.assertCountEqual(
  193. self.subject.available_modes,
  194. {MODE_NORMAL, MODE_SLEEP, MODE_AUTO},
  195. )
  196. async def test_set_climate_preset_to_auto(self):
  197. async with assert_device_properties_set(
  198. self.climate._device,
  199. {
  200. PRESET_DPS: "humidity",
  201. },
  202. ):
  203. await self.climate.async_set_preset_mode(MODE_AUTO)
  204. self.climate._device.anticipate_property_value.assert_not_called()
  205. async def test_set_climate_preset_to_sleep(self):
  206. async with assert_device_properties_set(
  207. self.climate._device,
  208. {
  209. PRESET_DPS: "sleep",
  210. },
  211. ):
  212. await self.climate.async_set_preset_mode(MODE_SLEEP)
  213. self.climate._device.anticipate_property_value.assert_not_called()
  214. async def test_set_climate_preset_to_normal(self):
  215. async with assert_device_properties_set(
  216. self.climate._device,
  217. {
  218. PRESET_DPS: "work",
  219. },
  220. ):
  221. await self.climate.async_set_preset_mode(MODE_NORMAL)
  222. self.climate._device.anticipate_property_value.assert_not_called()
  223. async def test_set_mode_to_auto(self):
  224. async with assert_device_properties_set(
  225. self.subject._device,
  226. {
  227. PRESET_DPS: "humidity",
  228. },
  229. ):
  230. await self.subject.async_set_mode(MODE_AUTO)
  231. self.subject._device.anticipate_property_value.assert_not_called()
  232. async def test_set_mode_to_sleep(self):
  233. async with assert_device_properties_set(
  234. self.subject._device,
  235. {
  236. PRESET_DPS: "sleep",
  237. },
  238. ):
  239. await self.subject.async_set_mode(MODE_SLEEP)
  240. self.subject._device.anticipate_property_value.assert_not_called()
  241. async def test_set_mode_to_normal(self):
  242. async with assert_device_properties_set(
  243. self.subject._device,
  244. {
  245. PRESET_DPS: "work",
  246. },
  247. ):
  248. await self.subject.async_set_mode(MODE_NORMAL)
  249. self.subject._device.anticipate_property_value.assert_not_called()
  250. def test_climate_device_state_attributes(self):
  251. self.dps[ERROR_DPS] = 0
  252. self.dps[TIMERHR_DPS] = "cancel"
  253. self.dps[TIMER_DPS] = 0
  254. self.assertCountEqual(
  255. self.climate.device_state_attributes,
  256. {
  257. "error": "OK",
  258. "timer_hr": "cancel",
  259. "timer_min": 0,
  260. },
  261. )
  262. self.dps[ERROR_DPS] = 1
  263. self.dps[TIMERHR_DPS] = "1"
  264. self.dps[TIMER_DPS] = 60
  265. self.assertCountEqual(
  266. self.climate.device_state_attributes,
  267. {
  268. "error": 1,
  269. "timer_hr": "1",
  270. "timer_min": 60,
  271. },
  272. )
  273. def test_device_state_attributes(self):
  274. self.dps[ERROR_DPS] = 0
  275. self.dps[TIMERHR_DPS] = "cancel"
  276. self.dps[TIMER_DPS] = 0
  277. self.dps[CURRENTHUMID_DPS] = 50
  278. self.dps[FANMODE_DPS] = "middle"
  279. self.assertCountEqual(
  280. self.subject.device_state_attributes,
  281. {
  282. "error": "OK",
  283. "timer_hr": "cancel",
  284. "timer_min": 0,
  285. "current_humidity": 50,
  286. },
  287. )
  288. def test_fan_speed(self):
  289. self.dps[FANMODE_DPS] = "small"
  290. self.assertEqual(self.fan.percentage, 33)
  291. self.dps[FANMODE_DPS] = "middle"
  292. self.assertEqual(self.fan.percentage, 67)
  293. self.dps[FANMODE_DPS] = "large"
  294. self.assertEqual(self.fan.percentage, 100)
  295. def test_climate_fan_mode(self):
  296. self.dps[FANMODE_DPS] = "small"
  297. self.assertEqual(self.climate.fan_mode, FAN_LOW)
  298. self.dps[FANMODE_DPS] = "middle"
  299. self.assertEqual(self.climate.fan_mode, FAN_MEDIUM)
  300. self.dps[FANMODE_DPS] = "large"
  301. self.assertEqual(self.climate.fan_mode, FAN_HIGH)
  302. self.dps[FANMODE_DPS] = None
  303. self.assertEqual(self.climate.fan_mode, None)
  304. def test_fan_speed_count(self):
  305. self.assertEqual(self.fan.speed_count, 3)
  306. def test_fan_percentage_step(self):
  307. self.assertAlmostEqual(self.fan.percentage_step, 33, 0)
  308. def test_climate_fan_modes(self):
  309. self.assertCountEqual(
  310. self.climate.fan_modes,
  311. {FAN_LOW, FAN_MEDIUM, FAN_HIGH},
  312. )
  313. async def test_fan_set_speed(self):
  314. async with assert_device_properties_set(
  315. self.fan._device,
  316. {FANMODE_DPS: "small"},
  317. ):
  318. await self.fan.async_set_percentage(33)
  319. async def test_fan_set_speed_snaps(self):
  320. async with assert_device_properties_set(
  321. self.fan._device,
  322. {FANMODE_DPS: "middle"},
  323. ):
  324. await self.fan.async_set_percentage(60)
  325. async def test_climate_set_fan_mode(self):
  326. async with assert_device_properties_set(
  327. self.climate._device,
  328. {FANMODE_DPS: "small"},
  329. ):
  330. await self.climate.async_set_fan_mode(FAN_LOW)
  331. def test_switch_was_created(self):
  332. self.assertIsInstance(self.switch, TuyaLocalSwitch)
  333. def test_switch_is_same_device(self):
  334. self.assertEqual(self.switch._device, self.climate._device)
  335. def test_switch_class_is_switch(self):
  336. self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
  337. def test_switch_is_on(self):
  338. self.dps[SWITCH_DPS] = True
  339. self.assertTrue(self.switch.is_on)
  340. self.dps[SWITCH_DPS] = False
  341. self.assertFalse(self.switch.is_on)
  342. def test_switch_is_on_when_unavailable(self):
  343. self.dps[SWITCH_DPS] = None
  344. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  345. async def test_switch_turn_on(self):
  346. async with assert_device_properties_set(
  347. self.switch._device, {SWITCH_DPS: True}
  348. ):
  349. await self.switch.async_turn_on()
  350. async def test_switch_turn_off(self):
  351. async with assert_device_properties_set(
  352. self.switch._device, {SWITCH_DPS: False}
  353. ):
  354. await self.switch.async_turn_off()
  355. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  356. self.dps[SWITCH_DPS] = False
  357. async with assert_device_properties_set(
  358. self.switch._device, {SWITCH_DPS: True}
  359. ):
  360. await self.switch.async_toggle()
  361. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  362. self.dps[SWITCH_DPS] = True
  363. async with assert_device_properties_set(
  364. self.switch._device, {SWITCH_DPS: False}
  365. ):
  366. await self.switch.async_toggle()
  367. def test_switch_returns_none_for_power(self):
  368. self.assertIsNone(self.switch.current_power_w)
  369. def test_switch_state_attributes_set(self):
  370. self.assertEqual(self.switch.device_state_attributes, {})