base_device_tests.py 3.4 KB

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