base_device_tests.py 6.0 KB

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