base_device_tests.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, Mock, PropertyMock, patch
  3. from uuid import uuid4
  4. from homeassistant.helpers.entity import EntityCategory
  5. from custom_components.tuya_local.alarm_control_panel import TuyaLocalAlarmControlPanel
  6. from custom_components.tuya_local.binary_sensor import TuyaLocalBinarySensor
  7. from custom_components.tuya_local.button import TuyaLocalButton
  8. from custom_components.tuya_local.camera import TuyaLocalCamera
  9. from custom_components.tuya_local.climate import TuyaLocalClimate
  10. from custom_components.tuya_local.cover import TuyaLocalCover
  11. from custom_components.tuya_local.event import TuyaLocalEvent
  12. from custom_components.tuya_local.fan import TuyaLocalFan
  13. from custom_components.tuya_local.helpers.device_config import (
  14. TuyaDeviceConfig,
  15. possible_matches,
  16. )
  17. from custom_components.tuya_local.humidifier import TuyaLocalHumidifier
  18. from custom_components.tuya_local.lawn_mower import TuyaLocalLawnMower
  19. from custom_components.tuya_local.light import TuyaLocalLight
  20. from custom_components.tuya_local.lock import TuyaLocalLock
  21. from custom_components.tuya_local.number import TuyaLocalNumber
  22. from custom_components.tuya_local.remote import TuyaLocalRemote
  23. from custom_components.tuya_local.select import TuyaLocalSelect
  24. from custom_components.tuya_local.sensor import TuyaLocalSensor
  25. from custom_components.tuya_local.siren import TuyaLocalSiren
  26. from custom_components.tuya_local.switch import TuyaLocalSwitch
  27. from custom_components.tuya_local.vacuum import TuyaLocalVacuum
  28. from custom_components.tuya_local.water_heater import TuyaLocalWaterHeater
  29. DEVICE_TYPES = {
  30. "alarm_control_panel": TuyaLocalAlarmControlPanel,
  31. "binary_sensor": TuyaLocalBinarySensor,
  32. "button": TuyaLocalButton,
  33. "camera": TuyaLocalCamera,
  34. "climate": TuyaLocalClimate,
  35. "cover": TuyaLocalCover,
  36. "event": TuyaLocalEvent,
  37. "fan": TuyaLocalFan,
  38. "humidifier": TuyaLocalHumidifier,
  39. "lawn_mower": TuyaLocalLawnMower,
  40. "light": TuyaLocalLight,
  41. "lock": TuyaLocalLock,
  42. "number": TuyaLocalNumber,
  43. "remote": TuyaLocalRemote,
  44. "switch": TuyaLocalSwitch,
  45. "select": TuyaLocalSelect,
  46. "sensor": TuyaLocalSensor,
  47. "siren": TuyaLocalSiren,
  48. "vacuum": TuyaLocalVacuum,
  49. "water_heater": TuyaLocalWaterHeater,
  50. }
  51. class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
  52. __test__ = False
  53. def setUpForConfig(self, config_file, payload):
  54. """Perform setup tasks for every test."""
  55. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  56. self.addCleanup(device_patcher.stop)
  57. self.mock_device = device_patcher.start()
  58. self.dps = payload.copy()
  59. self.mock_device.get_property.side_effect = lambda id: self.dps.get(id)
  60. cfg = TuyaDeviceConfig(config_file)
  61. self.conf_type = cfg.legacy_type
  62. type(self.mock_device).has_returned_state = PropertyMock(return_value=True)
  63. type(self.mock_device).unique_id = PropertyMock(return_value=str(uuid4()))
  64. self.mock_device.name = cfg.name
  65. self.entities = {}
  66. self.secondary_category = []
  67. self.primary_entity = cfg.primary_entity.config_id
  68. self.entities[self.primary_entity] = self.create_entity(cfg.primary_entity)
  69. self.names = {}
  70. self.names[cfg.primary_entity.config_id] = cfg.primary_entity.name
  71. for e in cfg.secondary_entities():
  72. self.entities[e.config_id] = self.create_entity(e)
  73. self.names[e.config_id] = e.name
  74. def create_entity(self, config):
  75. """Create an entity to match the config"""
  76. dev_type = DEVICE_TYPES[config.entity]
  77. if dev_type:
  78. entity = dev_type(self.mock_device, config)
  79. entity.platform = Mock()
  80. entity.platform.name = dev_type
  81. entity.platform.platform_translations = {}
  82. return entity
  83. def mark_secondary(self, entities):
  84. self.secondary_category = self.secondary_category + entities
  85. def test_config_matched(self):
  86. for cfg in possible_matches(self.dps):
  87. if cfg.legacy_type == self.conf_type:
  88. self.assertEqual(
  89. cfg.match_quality(self.dps),
  90. 100.0,
  91. msg=f"{self.conf_type} is an imperfect match",
  92. )
  93. return
  94. self.fail()
  95. def test_should_poll(self):
  96. for e in self.entities.values():
  97. self.assertFalse(e.should_poll)
  98. def test_available(self):
  99. for e in self.entities.values():
  100. self.assertTrue(e.available)
  101. def test_entity_category(self):
  102. for k, e in self.entities.items():
  103. if k in self.secondary_category:
  104. if type(e) in [TuyaLocalBinarySensor, TuyaLocalSensor]:
  105. self.assertEqual(
  106. e.entity_category,
  107. EntityCategory.DIAGNOSTIC,
  108. msg=f"{k} is {e.entity_category.value}, expected diagnostic",
  109. )
  110. else:
  111. self.assertEqual(
  112. e.entity_category,
  113. EntityCategory.CONFIG,
  114. msg=f"{k} is {e.entity_category.value}, expected config",
  115. )
  116. else:
  117. self.assertIsNone(
  118. e.entity_category,
  119. msg=f"{k} is {e.entity_category}, expected None",
  120. )
  121. # name has become more difficult to test with translation support, but it is working
  122. # in practice.
  123. # def test_name_returns_device_name(self):
  124. # for e in self.entities:
  125. # self.assertEqual(self.entities[e].name, self.names[e])
  126. def test_unique_id_contains_device_unique_id(self):
  127. entities = {}
  128. for e in self.entities.values():
  129. self.assertIn(self.mock_device.unique_id, e.unique_id)
  130. if type(e) not in entities:
  131. entities[type(e)] = []
  132. entities[type(e)].append(e.unique_id)
  133. for e in entities.values():
  134. self.assertCountEqual(e, set(e))
  135. def test_device_info_returns_device_info_from_device(self):
  136. for e in self.entities.values():
  137. self.assertEqual(e.device_info, self.mock_device.device_info)
  138. async def test_update(self):
  139. for e in self.entities.values():
  140. result = AsyncMock()
  141. self.mock_device.async_refresh.return_value = result()
  142. self.mock_device.async_refresh.reset_mock()
  143. await e.async_update()
  144. self.mock_device.async_refresh.assert_called_once()
  145. result.assert_awaited()