base_device_tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch, PropertyMock
  3. from uuid import uuid4
  4. from custom_components.tuya_local.generic.binary_sensor import TuyaLocalBinarySensor
  5. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  6. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  7. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  8. from custom_components.tuya_local.generic.light import TuyaLocalLight
  9. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  10. from custom_components.tuya_local.generic.number import TuyaLocalNumber
  11. from custom_components.tuya_local.generic.select import TuyaLocalSelect
  12. from custom_components.tuya_local.generic.sensor import TuyaLocalSensor
  13. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  14. from custom_components.tuya_local.helpers.device_config import (
  15. TuyaDeviceConfig,
  16. possible_matches,
  17. )
  18. from ..helpers import assert_device_properties_set
  19. DEVICE_TYPES = {
  20. "binary_sensor": TuyaLocalBinarySensor,
  21. "climate": TuyaLocalClimate,
  22. "fan": TuyaLocalFan,
  23. "humidifier": TuyaLocalHumidifier,
  24. "light": TuyaLocalLight,
  25. "lock": TuyaLocalLock,
  26. "number": TuyaLocalNumber,
  27. "switch": TuyaLocalSwitch,
  28. "select": TuyaLocalSelect,
  29. "sensor": TuyaLocalSensor,
  30. }
  31. class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
  32. __test__ = False
  33. def setUpForConfig(self, config_file, payload):
  34. """Perform setup tasks for every test."""
  35. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  36. self.addCleanup(device_patcher.stop)
  37. self.mock_device = device_patcher.start()
  38. self.dps = payload.copy()
  39. self.mock_device.get_property.side_effect = lambda id: self.dps[id]
  40. cfg = TuyaDeviceConfig(config_file)
  41. self.conf_type = cfg.legacy_type
  42. type(self.mock_device).has_returned_state = PropertyMock(return_value=True)
  43. type(self.mock_device).unique_id = PropertyMock(return_value=str(uuid4()))
  44. self.mock_device.name = cfg.name
  45. self.entities = {}
  46. self.primary_entity = cfg.primary_entity.config_id
  47. self.entities[self.primary_entity] = self.create_entity(cfg.primary_entity)
  48. self.names = {}
  49. self.names[cfg.primary_entity.config_id] = cfg.primary_entity.name(cfg.name)
  50. for e in cfg.secondary_entities():
  51. self.entities[e.config_id] = self.create_entity(e)
  52. self.names[e.config_id] = e.name(cfg.name)
  53. def create_entity(self, config):
  54. """Create an entity to match the config"""
  55. dev_type = DEVICE_TYPES[config.entity]
  56. if dev_type:
  57. return dev_type(self.mock_device, config)
  58. def test_config_matched(self):
  59. for cfg in possible_matches(self.dps):
  60. if cfg.legacy_type == self.conf_type:
  61. self.assertEqual(cfg.match_quality(self.dps), 100.0)
  62. return
  63. self.fail()
  64. def test_should_poll(self):
  65. for e in self.entities.values():
  66. self.assertTrue(e.should_poll)
  67. def test_available(self):
  68. for e in self.entities.values():
  69. self.assertTrue(e.available)
  70. def test_entity_category(self):
  71. for k in self.entities:
  72. e = self.entities[k]
  73. if k == self.primary_entity:
  74. self.assertIsNone(e.entity_category)
  75. elif type(e) in [TuyaLocalBinarySensor, TuyaLocalSensor]:
  76. self.assertEqual(e.entity_category, "diagnostic")
  77. else:
  78. self.assertEqual(e.entity_category, "config")
  79. def test_name_returns_device_name(self):
  80. for e in self.entities:
  81. self.assertEqual(self.entities[e].name, self.names[e])
  82. def test_unique_id_contains_device_unique_id(self):
  83. entities = {}
  84. for e in self.entities.values():
  85. self.assertIn(self.mock_device.unique_id, e.unique_id)
  86. if type(e) not in entities:
  87. entities[type(e)] = []
  88. entities[type(e)].append(e.unique_id)
  89. for e in entities.values():
  90. self.assertCountEqual(e, set(e))
  91. def test_device_info_returns_device_info_from_device(self):
  92. for e in self.entities.values():
  93. self.assertEqual(e.device_info, self.mock_device.device_info)
  94. async def test_update(self):
  95. for e in self.entities.values():
  96. result = AsyncMock()
  97. self.mock_device.async_refresh.return_value = result()
  98. self.mock_device.async_refresh.reset_mock()
  99. await e.async_update()
  100. self.mock_device.async_refresh.assert_called_once()
  101. result.assert_awaited()