base_device_tests.py 5.9 KB

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