base_device_tests.py 5.8 KB

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