base_device_tests.py 3.7 KB

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