test_device_config.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """Test the config parser"""
  2. from unittest import IsolatedAsyncioTestCase
  3. from unittest.mock import MagicMock
  4. from warnings import warn
  5. from custom_components.tuya_local.helpers.device_config import (
  6. available_configs,
  7. get_config,
  8. possible_matches,
  9. TuyaDeviceConfig,
  10. )
  11. from .const import (
  12. DEHUMIDIFIER_PAYLOAD,
  13. GPPH_HEATER_PAYLOAD,
  14. KOGAN_HEATER_PAYLOAD,
  15. )
  16. class TestDeviceConfig(IsolatedAsyncioTestCase):
  17. """Test the device config parser"""
  18. def test_can_find_config_files(self):
  19. """Test that the config files can be found by the parser."""
  20. found = False
  21. for cfg in available_configs():
  22. found = True
  23. break
  24. self.assertTrue(found)
  25. def test_config_files_parse(self):
  26. for cfg in available_configs():
  27. parsed = TuyaDeviceConfig(cfg)
  28. self.assertIsNotNone(parsed.name)
  29. def test_config_files_have_legacy_link(self):
  30. """
  31. Initially, we require a link between the new style config, and the old
  32. classes so we can transition over to the new config. When the
  33. transition is complete, we will drop the requirement, as new devices
  34. will only be added as config files.
  35. """
  36. for cfg in available_configs():
  37. parsed = TuyaDeviceConfig(cfg)
  38. self.assertIsNotNone(parsed.legacy_type)
  39. self.assertIsNotNone(parsed.primary_entity)
  40. # Most of the device_config functionality is exercised during testing of
  41. # the various supported devices. These tests concentrate only on the gaps.
  42. def test_match_quality(self):
  43. """Test the match_quality function."""
  44. cfg = get_config("deta_fan")
  45. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  46. self.assertEqual(q, 0)
  47. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  48. self.assertEqual(q, 0)
  49. def test_entity_find_unknown_dps_fails(self):
  50. """Test that finding a dps that doesn't exist fails."""
  51. cfg = get_config("kogan_switch")
  52. non_existing = cfg.primary_entity.find_dps("missing")
  53. self.assertIsNone(non_existing)
  54. async def test_dps_async_set_readonly_value_fails(self):
  55. """Test that setting a readonly dps fails."""
  56. mock_device = MagicMock()
  57. cfg = get_config("kogan_switch")
  58. voltage = cfg.primary_entity.find_dps("voltage_v")
  59. with self.assertRaises(TypeError):
  60. await voltage.async_set_value(mock_device, 230)
  61. def test_dps_values_returns_none_with_no_mapping(self):
  62. """Test that a dps with no mapping returns None as its possible values"""
  63. mock_device = MagicMock()
  64. cfg = get_config("kogan_switch")
  65. voltage = cfg.primary_entity.find_dps("voltage_v")
  66. self.assertIsNone(voltage.values(mock_device))
  67. # Test detection of all devices.
  68. def _test_detect(self, payload, dev_type, legacy_class):
  69. """Test that payload is detected as the correct type and class."""
  70. matched = False
  71. false_matches = []
  72. quality = 0
  73. for cfg in possible_matches(payload):
  74. self.assertTrue(cfg.matches(payload))
  75. if cfg.legacy_type == dev_type:
  76. self.assertFalse(matched)
  77. matched = True
  78. quality = cfg.match_quality(payload)
  79. if legacy_class is not None:
  80. cfg_class = cfg.primary_entity.legacy_class
  81. if cfg_class is None:
  82. for e in cfg.secondary_entities():
  83. cfg_class = e.legacy_class
  84. if cfg_class is not None:
  85. break
  86. self.assertEqual(
  87. cfg_class.__name__,
  88. legacy_class,
  89. )
  90. else:
  91. false_matches.append(cfg)
  92. self.assertTrue(matched)
  93. if quality < 100:
  94. warn(f"{dev_type} detected with imperfect quality {quality}%")
  95. best_q = 0
  96. for cfg in false_matches:
  97. q = cfg.match_quality(payload)
  98. if q > best_q:
  99. best_q = q
  100. self.assertGreater(quality, best_q)
  101. # Ensure the same correct config is returned when looked up by type
  102. cfg = get_config(dev_type)
  103. if legacy_class is not None:
  104. cfg_class = cfg.primary_entity.legacy_class
  105. if cfg_class is None:
  106. for e in cfg.secondary_entities():
  107. cfg_class = e.legacy_class
  108. if cfg_class is not None:
  109. break
  110. self.assertEqual(
  111. cfg_class.__name__,
  112. legacy_class,
  113. )
  114. def test_gpph_heater_detection(self):
  115. """Test that GPPH heater can be detected from its sample payload."""
  116. self._test_detect(GPPH_HEATER_PAYLOAD, "heater", "GoldairHeater")
  117. def test_goldair_dehumidifier_detection(self):
  118. """Test that Goldair dehumidifier can be detected from its sample payload."""
  119. self._test_detect(
  120. DEHUMIDIFIER_PAYLOAD,
  121. "dehumidifier",
  122. "GoldairDehumidifier",
  123. )
  124. # Non-legacy devices endup being the same as the tests in test_device.py, so
  125. # skip them.