base_device_tests.py 6.6 KB

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