base_device_tests.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch, PropertyMock
  3. from uuid import uuid4
  4. from homeassistant.components.light import COLOR_MODE_ONOFF
  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.climate import TuyaLocalClimate
  9. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  10. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  11. from custom_components.tuya_local.generic.light import TuyaLocalLight
  12. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  13. from custom_components.tuya_local.generic.number import TuyaLocalNumber
  14. from custom_components.tuya_local.generic.select import TuyaLocalSelect
  15. from custom_components.tuya_local.generic.sensor import TuyaLocalSensor
  16. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  17. from custom_components.tuya_local.helpers.device_config import (
  18. TuyaDeviceConfig,
  19. possible_matches,
  20. )
  21. from ..helpers import assert_device_properties_set
  22. DEVICE_TYPES = {
  23. "climate": TuyaLocalClimate,
  24. "fan": TuyaLocalFan,
  25. "humidifier": TuyaLocalHumidifier,
  26. "light": TuyaLocalLight,
  27. "lock": TuyaLocalLock,
  28. "number": TuyaLocalNumber,
  29. "switch": TuyaLocalSwitch,
  30. "select": TuyaLocalSelect,
  31. "sensor": TuyaLocalSensor,
  32. }
  33. class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
  34. __test__ = False
  35. def setUpForConfig(self, config_file, payload):
  36. """Perform setup tasks for every test."""
  37. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  38. self.addCleanup(device_patcher.stop)
  39. self.mock_device = device_patcher.start()
  40. self.dps = payload.copy()
  41. self.mock_device.get_property.side_effect = lambda id: self.dps[id]
  42. cfg = TuyaDeviceConfig(config_file)
  43. self.conf_type = cfg.legacy_type
  44. type(self.mock_device).has_returned_state = PropertyMock(return_value=True)
  45. type(self.mock_device).unique_id = PropertyMock(return_value=str(uuid4()))
  46. self.mock_device.name = cfg.name
  47. self.entities = {}
  48. self.entities[cfg.primary_entity.config_id] = self.create_entity(
  49. cfg.primary_entity
  50. )
  51. self.names = {}
  52. self.names[cfg.primary_entity.config_id] = cfg.primary_entity.name(cfg.name)
  53. for e in cfg.secondary_entities():
  54. self.entities[e.config_id] = self.create_entity(e)
  55. self.names[e.config_id] = e.name(cfg.name)
  56. def create_entity(self, config):
  57. """Create an entity to match the config"""
  58. dev_type = DEVICE_TYPES[config.entity]
  59. if dev_type:
  60. return dev_type(self.mock_device, config)
  61. def test_config_matched(self):
  62. for cfg in possible_matches(self.dps):
  63. if cfg.legacy_type == self.conf_type:
  64. self.assertEqual(cfg.match_quality(self.dps), 100.0)
  65. return
  66. self.fail()
  67. def test_should_poll(self):
  68. for e in self.entities.values():
  69. self.assertTrue(e.should_poll)
  70. def test_available(self):
  71. for e in self.entities.values():
  72. self.assertTrue(e.available)
  73. def test_name_returns_device_name(self):
  74. for e in self.entities:
  75. self.assertEqual(self.entities[e].name, self.names[e])
  76. def test_unique_id_contains_device_unique_id(self):
  77. entities = {}
  78. for e in self.entities.values():
  79. self.assertIn(self.mock_device.unique_id, e.unique_id)
  80. if type(e) not in entities:
  81. entities[type(e)] = []
  82. entities[type(e)].append(e.unique_id)
  83. for e in entities.values():
  84. self.assertCountEqual(e, set(e))
  85. def test_device_info_returns_device_info_from_device(self):
  86. for e in self.entities.values():
  87. self.assertEqual(e.device_info, self.mock_device.device_info)
  88. async def test_update(self):
  89. for e in self.entities.values():
  90. result = AsyncMock()
  91. self.mock_device.async_refresh.return_value = result()
  92. self.mock_device.async_refresh.reset_mock()
  93. await e.async_update()
  94. self.mock_device.async_refresh.assert_called_once()
  95. result.assert_awaited()
  96. # Mixins for common test functions
  97. class SwitchableTests:
  98. def setUpSwitchable(self, dps, subject):
  99. self.switch_dps = dps
  100. self.switch_subject = subject
  101. def test_switchable_is_on(self):
  102. self.dps[self.switch_dps] = True
  103. self.assertTrue(self.switch_subject.is_on)
  104. self.dps[self.switch_dps] = False
  105. self.assertFalse(self.switch_subject.is_on)
  106. self.dps[self.switch_dps] = None
  107. self.assertIsNone(self.switch_subject.is_on)
  108. async def test_switchable_turn_on(self):
  109. async with assert_device_properties_set(
  110. self.switch_subject._device, {self.switch_dps: True}
  111. ):
  112. await self.switch_subject.async_turn_on()
  113. async def test_switchable_turn_off(self):
  114. async with assert_device_properties_set(
  115. self.switch_subject._device, {self.switch_dps: False}
  116. ):
  117. await self.switch_subject.async_turn_off()
  118. async def test_switchable_toggle(self):
  119. self.dps[self.switch_dps] = False
  120. async with assert_device_properties_set(
  121. self.switch_subject._device, {self.switch_dps: True}
  122. ):
  123. await self.switch_subject.async_toggle()
  124. self.dps[self.switch_dps] = True
  125. async with assert_device_properties_set(
  126. self.switch_subject._device, {self.switch_dps: False}
  127. ):
  128. await self.switch_subject.async_toggle()
  129. class BasicLightTests:
  130. def setUpBasicLight(self, dps, subject):
  131. self.basicLight = subject
  132. self.basicLightDps = dps
  133. def test_basic_light_supported_color_modes(self):
  134. self.assertCountEqual(
  135. self.basicLight.supported_color_modes,
  136. [COLOR_MODE_ONOFF],
  137. )
  138. def test_basic_light_color_mode(self):
  139. self.assertEqual(self.basicLight.color_mode, COLOR_MODE_ONOFF)
  140. def test_light_has_no_brightness(self):
  141. self.assertIsNone(self.basicLight.brightness)
  142. def test_light_has_no_effects(self):
  143. self.assertIsNone(self.basicLight.effect_list)
  144. self.assertIsNone(self.basicLight.effect)
  145. def test_basic_light_is_on(self):
  146. self.dps[self.basicLightDps] = True
  147. self.assertTrue(self.basicLight.is_on)
  148. self.dps[self.basicLightDps] = False
  149. self.assertFalse(self.basicLight.is_on)
  150. async def test_basic_light_turn_on(self):
  151. async with assert_device_properties_set(
  152. self.basicLight._device, {self.basicLightDps: True}
  153. ):
  154. await self.basicLight.async_turn_on()
  155. async def test_basic_light_turn_off(self):
  156. async with assert_device_properties_set(
  157. self.basicLight._device, {self.basicLightDps: False}
  158. ):
  159. await self.basicLight.async_turn_off()
  160. async def test_basic_light_toggle_turns_on_when_it_was_off(self):
  161. self.dps[self.basicLightDps] = False
  162. async with assert_device_properties_set(
  163. self.basicLight._device,
  164. {self.basicLightDps: True},
  165. ):
  166. await self.basicLight.async_toggle()
  167. async def test_basic_light_toggle_turns_off_when_it_was_on(self):
  168. self.dps[self.basicLightDps] = True
  169. async with assert_device_properties_set(
  170. self.basicLight._device,
  171. {self.basicLightDps: False},
  172. ):
  173. await self.basicLight.async_toggle()
  174. def test_basic_light_state_attributes(self):
  175. self.assertEqual(self.basicLight.device_state_attributes, {})
  176. class BasicLockTests:
  177. def setUpBasicLock(self, dps, subject):
  178. self.basicLock = subject
  179. self.basicLockDps = dps
  180. def test_basic_lock_state(self):
  181. self.dps[self.basicLockDps] = True
  182. self.assertEqual(self.basicLock.state, STATE_LOCKED)
  183. self.dps[self.basicLockDps] = False
  184. self.assertEqual(self.basicLock.state, STATE_UNLOCKED)
  185. self.dps[self.basicLockDps] = None
  186. self.assertEqual(self.basicLock.state, STATE_UNAVAILABLE)
  187. def test_basic_lock_is_locked(self):
  188. self.dps[self.basicLockDps] = True
  189. self.assertTrue(self.basicLock.is_locked)
  190. self.dps[self.basicLockDps] = False
  191. self.assertFalse(self.basicLock.is_locked)
  192. self.dps[self.basicLockDps] = None
  193. self.assertFalse(self.basicLock.is_locked)
  194. async def test_basic_lock_locks(self):
  195. async with assert_device_properties_set(
  196. self.basicLock._device,
  197. {self.basicLockDps: True},
  198. ):
  199. await self.basicLock.async_lock()
  200. async def test_basic_lock_unlocks(self):
  201. async with assert_device_properties_set(
  202. self.basicLock._device,
  203. {self.basicLockDps: False},
  204. ):
  205. await self.basicLock.async_unlock()
  206. def test_basic_lock_state_attributes(self):
  207. self.assertEqual(self.basicLock.device_state_attributes, {})
  208. class BasicSwitchTests:
  209. def setUpBasicSwitch(self, dps, subject):
  210. self.basicSwitch = subject
  211. self.basicSwitchDps = dps
  212. def test_basic_switch_is_on(self):
  213. self.dps[self.basicSwitchDps] = True
  214. self.assertEqual(self.basicSwitch.is_on, True)
  215. self.dps[self.basicSwitchDps] = False
  216. self.assertEqual(self.basicSwitch.is_on, False)
  217. async def test_basic_switch_turn_on(self):
  218. async with assert_device_properties_set(
  219. self.basicSwitch._device, {self.basicSwitchDps: True}
  220. ):
  221. await self.basicSwitch.async_turn_on()
  222. async def test_basic_switch_turn_off(self):
  223. async with assert_device_properties_set(
  224. self.basicSwitch._device, {self.basicSwitchDps: False}
  225. ):
  226. await self.basicSwitch.async_turn_off()
  227. async def test_basic_switch_toggle_turns_on_when_it_was_off(self):
  228. self.dps[self.basicSwitchDps] = False
  229. async with assert_device_properties_set(
  230. self.basicSwitch._device, {self.basicSwitchDps: True}
  231. ):
  232. await self.basicSwitch.async_toggle()
  233. async def test_basic_switch_toggle_turns_off_when_it_was_on(self):
  234. self.dps[self.basicSwitchDps] = True
  235. async with assert_device_properties_set(
  236. self.basicSwitch._device, {self.basicSwitchDps: False}
  237. ):
  238. await self.basicSwitch.async_toggle()
  239. def test_basic_switch_class_is_switch(self):
  240. self.assertEqual(self.basicSwitch.device_class, DEVICE_CLASS_SWITCH)
  241. def test_basic_switch_has_no_power_monitoring(self):
  242. self.assertIsNone(self.basicSwitch.current_power_w)
  243. def test_basic_switch_state_attributes(self):
  244. self.assertEqual(self.basicSwitch.device_state_attributes, {})