4
0

base_device_tests.py 6.4 KB

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