test_device_config.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """Test the config parser"""
  2. import unittest
  3. from warnings import warn
  4. from custom_components.tuya_local.const import (
  5. CONF_TYPE_DEHUMIDIFIER,
  6. CONF_TYPE_EUROM_600_HEATER,
  7. CONF_TYPE_FAN,
  8. CONF_TYPE_GARDENPAC_HEATPUMP,
  9. CONF_TYPE_GECO_HEATER,
  10. CONF_TYPE_GPCV_HEATER,
  11. CONF_TYPE_GPPH_HEATER,
  12. CONF_TYPE_GSH_HEATER,
  13. CONF_TYPE_KOGAN_HEATER,
  14. CONF_TYPE_KOGAN_SWITCH,
  15. CONF_TYPE_PURLINE_M100_HEATER,
  16. )
  17. from custom_components.tuya_local.helpers.device_config import (
  18. available_configs,
  19. config_for_legacy_use,
  20. possible_matches,
  21. TuyaDeviceConfig,
  22. )
  23. from .const import (
  24. DEHUMIDIFIER_PAYLOAD,
  25. EUROM_600_HEATER_PAYLOAD,
  26. FAN_PAYLOAD,
  27. GARDENPAC_HEATPUMP_PAYLOAD,
  28. GECO_HEATER_PAYLOAD,
  29. GPCV_HEATER_PAYLOAD,
  30. GPPH_HEATER_PAYLOAD,
  31. GSH_HEATER_PAYLOAD,
  32. KOGAN_HEATER_PAYLOAD,
  33. KOGAN_SOCKET_PAYLOAD,
  34. KOGAN_SOCKET_PAYLOAD2,
  35. PURLINE_M100_HEATER_PAYLOAD,
  36. REMORA_HEATPUMP_PAYLOAD,
  37. )
  38. class TestDeviceConfig(unittest.TestCase):
  39. """Test the device config parser"""
  40. def test_can_find_config_files(self):
  41. """Test that the config files can be found by the parser."""
  42. found = False
  43. for cfg in available_configs():
  44. found = True
  45. break
  46. self.assertTrue(found)
  47. def test_config_files_parse(self):
  48. for cfg in available_configs():
  49. parsed = TuyaDeviceConfig(cfg)
  50. self.assertIsNotNone(parsed.name)
  51. def test_config_files_have_legacy_link(self):
  52. """
  53. Initially, we require a link between the new style config, and the old
  54. classes so we can transition over to the new config. When the
  55. transition is complete, we will drop the requirement, as new devices
  56. will only be added as config files.
  57. """
  58. for cfg in available_configs():
  59. parsed = TuyaDeviceConfig(cfg)
  60. self.assertIsNotNone(parsed.legacy_type)
  61. self.assertIsNotNone(parsed.primary_entity)
  62. def _test_detect(self, payload, legacy_type, legacy_class):
  63. """Test that payload is detected as the correct type and class."""
  64. matched = False
  65. false_matches = []
  66. quality = 0
  67. for cfg in possible_matches(payload):
  68. self.assertTrue(cfg.matches(payload))
  69. if cfg.legacy_type == legacy_type:
  70. self.assertFalse(matched)
  71. matched = True
  72. quality = cfg.match_quality(payload)
  73. if legacy_class is not None:
  74. self.assertEqual(
  75. cfg.primary_entity.legacy_class.__name__,
  76. legacy_class,
  77. )
  78. else:
  79. false_matches.append(cfg)
  80. self.assertTrue(matched)
  81. if quality < 100:
  82. warn(f"{legacy_type} detected with quality {quality}")
  83. best_q = 0
  84. for cfg in false_matches:
  85. q = cfg.match_quality(payload)
  86. if q > best_q:
  87. best_q = q
  88. warn(f"{legacy_type} also detectable as {cfg.legacy_type} with quality {q}")
  89. self.assertGreater(quality, best_q)
  90. # Ensure the same correct config is returned when looked up by type
  91. cfg = config_for_legacy_use(legacy_type)
  92. if legacy_class is not None:
  93. self.assertEqual(
  94. cfg.primary_entity.legacy_class.__name__,
  95. legacy_class,
  96. )
  97. def test_gpph_heater_detection(self):
  98. """Test that GPPH heater can be detected from its sample payload."""
  99. self._test_detect(GPPH_HEATER_PAYLOAD, CONF_TYPE_GPPH_HEATER, "GoldairHeater")
  100. def test_gpcv_heater_detection(self):
  101. """Test that GPCV heater can be detected from its sample payload."""
  102. self._test_detect(
  103. GPCV_HEATER_PAYLOAD,
  104. CONF_TYPE_GPCV_HEATER,
  105. None,
  106. )
  107. def test_eurom_heater_detection(self):
  108. """Test that Eurom heater can be detected from its sample payload."""
  109. self._test_detect(
  110. EUROM_600_HEATER_PAYLOAD,
  111. CONF_TYPE_EUROM_600_HEATER,
  112. None,
  113. )
  114. def test_geco_heater_detection(self):
  115. """Test that GECO heater can be detected from its sample payload."""
  116. self._test_detect(
  117. GECO_HEATER_PAYLOAD,
  118. CONF_TYPE_GECO_HEATER,
  119. None,
  120. )
  121. def test_kogan_heater_detection(self):
  122. """Test that Kogan heater can be detected from its sample payload."""
  123. self._test_detect(
  124. KOGAN_HEATER_PAYLOAD,
  125. CONF_TYPE_KOGAN_HEATER,
  126. None,
  127. )
  128. def test_goldair_dehumidifier_detection(self):
  129. """Test that Goldair dehumidifier can be detected from its sample payload."""
  130. self._test_detect(
  131. DEHUMIDIFIER_PAYLOAD,
  132. CONF_TYPE_DEHUMIDIFIER,
  133. "GoldairDehumidifier",
  134. )
  135. def test_goldair_fan_detection(self):
  136. """Test that Goldair fan can be detected from its sample payload."""
  137. self._test_detect(FAN_PAYLOAD, CONF_TYPE_FAN, "GoldairFan")
  138. def test_kogan_socket_detection(self):
  139. """Test that 1st gen Kogan Socket can be detected from its sample payload."""
  140. self._test_detect(
  141. KOGAN_SOCKET_PAYLOAD,
  142. CONF_TYPE_KOGAN_SWITCH,
  143. None,
  144. )
  145. def test_kogan_socket2_detection(self):
  146. """Test that 2nd gen Kogan Socket can be detected from its sample payload."""
  147. self._test_detect(
  148. KOGAN_SOCKET_PAYLOAD2,
  149. CONF_TYPE_KOGAN_SWITCH,
  150. None,
  151. )
  152. def test_gsh_heater_detection(self):
  153. """Test that GSH heater can be detected from its sample payload."""
  154. self._test_detect(
  155. GSH_HEATER_PAYLOAD,
  156. CONF_TYPE_GSH_HEATER,
  157. None,
  158. )
  159. def test_gardenpac_heatpump_detection(self):
  160. """Test that GardenPac heatpump can be detected from its sample payload."""
  161. self._test_detect(
  162. GARDENPAC_HEATPUMP_PAYLOAD,
  163. CONF_TYPE_GARDENPAC_HEATPUMP,
  164. "GardenPACPoolHeatpump",
  165. )
  166. def test_purline_heater_detection(self):
  167. """Test that Purline heater can be detected from its sample payload."""
  168. self._test_detect(
  169. PURLINE_M100_HEATER_PAYLOAD,
  170. CONF_TYPE_PURLINE_M100_HEATER,
  171. "PurlineM100Heater",
  172. )
  173. # Non-legacy devices start here.
  174. def test_remora_heatpump_detection(self):
  175. """Test that Remora heatpump can be detected from its sample payload."""
  176. self._test_detect(REMORA_HEATPUMP_PAYLOAD, "remora_heatpump", None)