base_device_tests.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.climate import TuyaLocalClimate
  5. from custom_components.tuya_local.generic.fan import TuyaLocalFan
  6. from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
  7. from custom_components.tuya_local.generic.light import TuyaLocalLight
  8. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  9. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  10. from custom_components.tuya_local.helpers.device_config import (
  11. TuyaDeviceConfig,
  12. possible_matches,
  13. )
  14. DEVICE_TYPES = {
  15. "climate": TuyaLocalClimate,
  16. "fan": TuyaLocalFan,
  17. "humidifier": TuyaLocalHumidifier,
  18. "light": TuyaLocalLight,
  19. "lock": TuyaLocalLock,
  20. "switch": TuyaLocalSwitch,
  21. }
  22. class TuyaDeviceTestCase(IsolatedAsyncioTestCase):
  23. __test__ = False
  24. def setUpForConfig(self, config_file, payload):
  25. """Perform setup tasks for every test."""
  26. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  27. self.addCleanup(device_patcher.stop)
  28. self.mock_device = device_patcher.start()
  29. self.dps = payload.copy()
  30. self.mock_device.get_property.side_effect = lambda id: self.dps[id]
  31. cfg = TuyaDeviceConfig(config_file)
  32. self.conf_type = cfg.legacy_type
  33. type(self.mock_device).unique_id = PropertyMock(return_value=str(uuid4()))
  34. self.mock_device.name = cfg.name
  35. self.entities = {}
  36. self.entities[cfg.primary_entity.config_id] = self.create_entity(
  37. cfg.primary_entity
  38. )
  39. self.names = {}
  40. self.names[cfg.primary_entity.config_id] = cfg.primary_entity.name(cfg.name)
  41. for e in cfg.secondary_entities():
  42. self.entities[e.config_id] = self.create_entity(e)
  43. self.names[e.config_id] = e.name(cfg.name)
  44. def create_entity(self, config):
  45. """Create an entity to match the config"""
  46. dev_type = DEVICE_TYPES[config.entity]
  47. if dev_type:
  48. return dev_type(self.mock_device, config)
  49. def test_config_matched(self):
  50. for cfg in possible_matches(self.dps):
  51. if cfg.legacy_type == self.conf_type:
  52. self.assertEqual(cfg.match_quality(self.dps), 100.0)
  53. return
  54. self.fail()
  55. def test_should_poll(self):
  56. for e in self.entities.values():
  57. self.assertTrue(e.should_poll)
  58. def test_name_returns_device_name(self):
  59. for e in self.entities:
  60. self.assertEqual(self.entities[e].name, self.names[e])
  61. def test_unique_id_contains_device_unique_id(self):
  62. entities = {}
  63. for e in self.entities.values():
  64. self.assertIn(self.mock_device.unique_id, e.unique_id)
  65. if type(e) not in entities:
  66. entities[type(e)] = []
  67. entities[type(e)].append(e.unique_id)
  68. for e in entities.values():
  69. self.assertCountEqual(e, set(e))
  70. def test_device_info_returns_device_info_from_device(self):
  71. for e in self.entities.values():
  72. self.assertEqual(e.device_info, self.mock_device.device_info)
  73. async def test_update(self):
  74. for e in self.entities.values():
  75. result = AsyncMock()
  76. self.mock_device.async_refresh.return_value = result()
  77. self.mock_device.async_refresh.reset_mock()
  78. await e.async_update()
  79. self.mock_device.async_refresh.assert_called_once()
  80. result.assert_awaited()