test_eanons_humidifier.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.humidifier.const import (
  14. MODE_NORMAL,
  15. MODE_BOOST,
  16. MODE_SLEEP,
  17. )
  18. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  19. from homeassistant.const import STATE_UNAVAILABLE
  20. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  21. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  22. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  23. from ..const import EANONS_HUMIDIFIER_PAYLOAD
  24. from ..helpers import assert_device_properties_set
  25. FANMODE_DPS = "2"
  26. TIMERHR_DPS = "3"
  27. TIMER_DPS = "4"
  28. ERROR_DPS = "9"
  29. HVACMODE_DPS = "10"
  30. PRESET_DPS = "12"
  31. HUMIDITY_DPS = "15"
  32. CURRENTHUMID_DPS = "16"
  33. SWITCH_DPS = "22"
  34. class TestEanonsHumidifier(IsolatedAsyncioTestCase):
  35. def setUp(self):
  36. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  37. self.addCleanup(device_patcher.stop)
  38. self.mock_device = device_patcher.start()
  39. cfg = TuyaDeviceConfig("eanons_humidifier.yaml")
  40. climate = cfg.primary_entity
  41. switch = None
  42. for e in cfg.secondary_entities():
  43. if e.entity == "switch":
  44. switch = e
  45. self.climate_name = climate.name
  46. self.switch_name = "missing" if switch is None else switch.name
  47. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  48. self.switch = TuyaLocalSwitch(self.mock_device(), switch)
  49. self.dps = EANONS_HUMIDIFIER_PAYLOAD.copy()
  50. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  51. def test_supported_features(self):
  52. self.assertEqual(
  53. self.subject.supported_features,
  54. SUPPORT_TARGET_HUMIDITY | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE,
  55. )
  56. def test_shouldPoll(self):
  57. self.assertTrue(self.subject.should_poll)
  58. self.assertTrue(self.switch.should_poll)
  59. def test_name_returns_device_name(self):
  60. self.assertEqual(self.subject.name, self.subject._device.name)
  61. self.assertEqual(self.switch.name, self.subject._device.name)
  62. def test_friendly_name_returns_config_name(self):
  63. self.assertEqual(self.subject.friendly_name, self.climate_name)
  64. self.assertEqual(self.switch.friendly_name, self.switch_name)
  65. def test_unique_id_returns_device_unique_id(self):
  66. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  67. self.assertEqual(self.switch.unique_id, self.subject._device.unique_id)
  68. def test_device_info_returns_device_info_from_device(self):
  69. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  70. self.assertEqual(self.switch.device_info, self.subject._device.device_info)
  71. @skip("Icon customisation not supported yet")
  72. def test_icon_is_humidifier(self):
  73. """Test that the icon is as expected."""
  74. self.dps[HVACMODE_DPS] = True
  75. self.assertEqual(self.subject.icon, "mdi:air-humidifier")
  76. self.dps[HVACMODE_DPS] = False
  77. self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
  78. def test_current_humidity(self):
  79. self.dps[CURRENTHUMID_DPS] = 47
  80. self.assertEqual(self.subject.current_humidity, 47)
  81. def test_min_target_humidity(self):
  82. self.assertEqual(self.subject.min_humidity, 40)
  83. def test_max_target_humidity(self):
  84. self.assertEqual(self.subject.max_humidity, 90)
  85. def test_target_humidity(self):
  86. self.dps[HUMIDITY_DPS] = 55
  87. self.assertEqual(self.subject.target_humidity, 55)
  88. def test_hvac_mode(self):
  89. self.dps[HVACMODE_DPS] = True
  90. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_DRY)
  91. self.dps[HVACMODE_DPS] = False
  92. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  93. self.dps[HVACMODE_DPS] = None
  94. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  95. def test_hvac_modes(self):
  96. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_DRY])
  97. async def test_turn_on(self):
  98. async with assert_device_properties_set(
  99. self.subject._device, {HVACMODE_DPS: True}
  100. ):
  101. await self.subject.async_set_hvac_mode(HVAC_MODE_DRY)
  102. async def test_turn_off(self):
  103. async with assert_device_properties_set(
  104. self.subject._device, {HVACMODE_DPS: False}
  105. ):
  106. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  107. def test_preset_mode(self):
  108. self.dps[PRESET_DPS] = "sleep"
  109. self.assertEqual(self.subject.preset_mode, MODE_SLEEP)
  110. self.dps[PRESET_DPS] = "humidity"
  111. self.assertEqual(self.subject.preset_mode, MODE_NORMAL)
  112. self.dps[PRESET_DPS] = "work"
  113. self.assertEqual(self.subject.preset_mode, MODE_BOOST)
  114. self.dps[PRESET_DPS] = None
  115. self.assertEqual(self.subject.preset_mode, None)
  116. def test_preset_modes(self):
  117. self.assertCountEqual(
  118. self.subject.preset_modes,
  119. {MODE_NORMAL, MODE_SLEEP, MODE_BOOST},
  120. )
  121. async def test_set_preset_to_normal(self):
  122. async with assert_device_properties_set(
  123. self.subject._device,
  124. {
  125. PRESET_DPS: "humidity",
  126. },
  127. ):
  128. await self.subject.async_set_preset_mode(MODE_NORMAL)
  129. self.subject._device.anticipate_property_value.assert_not_called()
  130. async def test_set_preset_to_sleep(self):
  131. async with assert_device_properties_set(
  132. self.subject._device,
  133. {
  134. PRESET_DPS: "sleep",
  135. },
  136. ):
  137. await self.subject.async_set_preset_mode(MODE_SLEEP)
  138. self.subject._device.anticipate_property_value.assert_not_called()
  139. async def test_set_preset_to_boost(self):
  140. async with assert_device_properties_set(
  141. self.subject._device,
  142. {
  143. PRESET_DPS: "work",
  144. },
  145. ):
  146. await self.subject.async_set_preset_mode(MODE_BOOST)
  147. self.subject._device.anticipate_property_value.assert_not_called()
  148. def test_device_state_attributes(self):
  149. self.dps[ERROR_DPS] = 0
  150. self.dps[TIMERHR_DPS] = "cancel"
  151. self.dps[TIMER_DPS] = 0
  152. self.assertCountEqual(
  153. self.subject.device_state_attributes,
  154. {
  155. "error": "OK",
  156. "timer_hr": "cancel",
  157. "timer_min": 0,
  158. },
  159. )
  160. self.dps[ERROR_DPS] = 1
  161. self.dps[TIMERHR_DPS] = "1"
  162. self.dps[TIMER_DPS] = 60
  163. self.assertCountEqual(
  164. self.subject.device_state_attributes,
  165. {
  166. "error": 1,
  167. "timer_hr": "1",
  168. "timer_min": 60,
  169. },
  170. )
  171. def test_fan_mode(self):
  172. self.dps[FANMODE_DPS] = "small"
  173. self.assertEqual(self.subject.fan_mode, FAN_LOW)
  174. self.dps[FANMODE_DPS] = "middle"
  175. self.assertEqual(self.subject.fan_mode, FAN_MEDIUM)
  176. self.dps[FANMODE_DPS] = "large"
  177. self.assertEqual(self.subject.fan_mode, FAN_HIGH)
  178. self.dps[FANMODE_DPS] = None
  179. self.assertEqual(self.subject.fan_mode, None)
  180. def test_fan_modes(self):
  181. self.assertCountEqual(
  182. self.subject.fan_modes,
  183. {FAN_LOW, FAN_MEDIUM, FAN_HIGH},
  184. )
  185. async def test_set_fan_mode(self):
  186. async with assert_device_properties_set(
  187. self.subject._device,
  188. {FANMODE_DPS: "small"},
  189. ):
  190. await self.subject.async_set_fan_mode(FAN_LOW)
  191. def test_switch_was_created(self):
  192. self.assertIsInstance(self.switch, TuyaLocalSwitch)
  193. def test_switch_is_same_device(self):
  194. self.assertEqual(self.switch._device, self.subject._device)
  195. def test_switch_class_is_switch(self):
  196. self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
  197. def test_switch_is_on(self):
  198. self.dps[SWITCH_DPS] = True
  199. self.assertTrue(self.switch.is_on)
  200. self.dps[SWITCH_DPS] = False
  201. self.assertFalse(self.switch.is_on)
  202. def test_switch_is_on_when_unavailable(self):
  203. self.dps[SWITCH_DPS] = None
  204. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  205. async def test_switch_turn_on(self):
  206. async with assert_device_properties_set(
  207. self.switch._device, {SWITCH_DPS: True}
  208. ):
  209. await self.switch.async_turn_on()
  210. async def test_switch_turn_off(self):
  211. async with assert_device_properties_set(
  212. self.switch._device, {SWITCH_DPS: False}
  213. ):
  214. await self.switch.async_turn_off()
  215. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  216. self.dps[SWITCH_DPS] = False
  217. async with assert_device_properties_set(
  218. self.switch._device, {SWITCH_DPS: True}
  219. ):
  220. await self.switch.async_toggle()
  221. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  222. self.dps[SWITCH_DPS] = True
  223. async with assert_device_properties_set(
  224. self.switch._device, {SWITCH_DPS: False}
  225. ):
  226. await self.switch.async_toggle()
  227. def test_switch_state_attributes_set(self):
  228. self.assertEqual(self.switch.device_state_attributes, {})