base_device_tests.py 5.7 KB

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