4
0

test_device_config.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. """Test the config parser"""
  2. import unittest
  3. from warnings import warn
  4. from custom_components.tuya_local.helpers.device_config import (
  5. available_configs,
  6. config_for_legacy_use,
  7. possible_matches,
  8. TuyaDeviceConfig,
  9. )
  10. from .const import (
  11. DEHUMIDIFIER_PAYLOAD,
  12. EUROM_600_HEATER_PAYLOAD,
  13. FAN_PAYLOAD,
  14. GARDENPAC_HEATPUMP_PAYLOAD,
  15. GECO_HEATER_PAYLOAD,
  16. GPCV_HEATER_PAYLOAD,
  17. GPPH_HEATER_PAYLOAD,
  18. GSH_HEATER_PAYLOAD,
  19. KOGAN_HEATER_PAYLOAD,
  20. KOGAN_SOCKET_PAYLOAD,
  21. KOGAN_SOCKET_PAYLOAD2,
  22. PURLINE_M100_HEATER_PAYLOAD,
  23. REMORA_HEATPUMP_PAYLOAD,
  24. BWT_HEATPUMP_PAYLOAD,
  25. EANONS_HUMIDIFIER_PAYLOAD,
  26. INKBIRD_THERMOSTAT_PAYLOAD,
  27. ANKO_FAN_PAYLOAD,
  28. )
  29. class TestDeviceConfig(unittest.TestCase):
  30. """Test the device config parser"""
  31. def test_can_find_config_files(self):
  32. """Test that the config files can be found by the parser."""
  33. found = False
  34. for cfg in available_configs():
  35. found = True
  36. break
  37. self.assertTrue(found)
  38. def test_config_files_parse(self):
  39. for cfg in available_configs():
  40. parsed = TuyaDeviceConfig(cfg)
  41. self.assertIsNotNone(parsed.name)
  42. def test_config_files_have_legacy_link(self):
  43. """
  44. Initially, we require a link between the new style config, and the old
  45. classes so we can transition over to the new config. When the
  46. transition is complete, we will drop the requirement, as new devices
  47. will only be added as config files.
  48. """
  49. for cfg in available_configs():
  50. parsed = TuyaDeviceConfig(cfg)
  51. self.assertIsNotNone(parsed.legacy_type)
  52. self.assertIsNotNone(parsed.primary_entity)
  53. # Most of the device_config functionality is exercised during testing of
  54. # the various supported devices. These tests concentrate only on the gaps.
  55. def test_match_quality(self):
  56. """Test the match_quality function."""
  57. cfg = config_for_legacy_use("deta_fan")
  58. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  59. self.assertEqual(q, 0)
  60. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  61. self.assertEqual(q, 0)
  62. def test_entity_find_unknown_dps_fails(self):
  63. """Test that finding a dps that doesn't exist fails."""
  64. cfg = config_for_legacy_use("kogan_switch")
  65. non_existing = cfg.primary_entity.find_dps("missing")
  66. self.assertIsNone(non_existing)
  67. async def test_dps_async_set_readonly_value_fails(self):
  68. """Test that setting a readonly dps fails."""
  69. mock_device = MagicMock()
  70. cfg = config_for_legacy_use("kogan_switch")
  71. voltage = cfg.primary_entity.find_dps("voltage_v")
  72. with self.assertRaises(TypeError):
  73. await voltage.async_set_value(mock_device, 230)
  74. async def test_dps_values_returns_none_with_no_mapping(self):
  75. """Test that a dps with no mapping returns None as its possible values"""
  76. cfg = config_for_legacy_use("kogan_switch")
  77. voltage = cfg.primary_entity.find_dps("voltage_v")
  78. self.assertIsNone(voltage.values)
  79. # Test detection of all devices.
  80. def _test_detect(self, payload, legacy_type, legacy_class):
  81. """Test that payload is detected as the correct type and class."""
  82. matched = False
  83. false_matches = []
  84. quality = 0
  85. for cfg in possible_matches(payload):
  86. self.assertTrue(cfg.matches(payload))
  87. if cfg.legacy_type == legacy_type:
  88. self.assertFalse(matched)
  89. matched = True
  90. quality = cfg.match_quality(payload)
  91. if legacy_class is not None:
  92. cfg_class = cfg.primary_entity.legacy_class
  93. if cfg_class is None:
  94. for e in cfg.secondary_entities():
  95. cfg_class = e.legacy_class
  96. if cfg_class is not None:
  97. break
  98. self.assertEqual(
  99. cfg_class.__name__,
  100. legacy_class,
  101. )
  102. else:
  103. false_matches.append(cfg)
  104. self.assertTrue(matched)
  105. if quality < 100:
  106. warn(f"{legacy_type} detected with imperfect quality {quality}%")
  107. best_q = 0
  108. for cfg in false_matches:
  109. q = cfg.match_quality(payload)
  110. if q > best_q:
  111. best_q = q
  112. self.assertGreater(quality, best_q)
  113. # Ensure the same correct config is returned when looked up by type
  114. cfg = config_for_legacy_use(legacy_type)
  115. if legacy_class is not None:
  116. cfg_class = cfg.primary_entity.legacy_class
  117. if cfg_class is None:
  118. for e in cfg.secondary_entities():
  119. cfg_class = e.legacy_class
  120. if cfg_class is not None:
  121. break
  122. self.assertEqual(
  123. cfg_class.__name__,
  124. legacy_class,
  125. )
  126. def test_gpph_heater_detection(self):
  127. """Test that GPPH heater can be detected from its sample payload."""
  128. self._test_detect(GPPH_HEATER_PAYLOAD, "heater", "GoldairHeater")
  129. def test_gpcv_heater_detection(self):
  130. """Test that GPCV heater can be detected from its sample payload."""
  131. self._test_detect(
  132. GPCV_HEATER_PAYLOAD,
  133. "gpcv_heater",
  134. None,
  135. )
  136. def test_eurom_heater_detection(self):
  137. """Test that Eurom heater can be detected from its sample payload."""
  138. self._test_detect(
  139. EUROM_600_HEATER_PAYLOAD,
  140. "eurom_heater",
  141. None,
  142. )
  143. def test_geco_heater_detection(self):
  144. """Test that GECO heater can be detected from its sample payload."""
  145. self._test_detect(
  146. GECO_HEATER_PAYLOAD,
  147. "geco_heater",
  148. None,
  149. )
  150. def test_kogan_heater_detection(self):
  151. """Test that Kogan heater can be detected from its sample payload."""
  152. self._test_detect(
  153. KOGAN_HEATER_PAYLOAD,
  154. "kogan_heater",
  155. None,
  156. )
  157. def test_goldair_dehumidifier_detection(self):
  158. """Test that Goldair dehumidifier can be detected from its sample payload."""
  159. self._test_detect(
  160. DEHUMIDIFIER_PAYLOAD,
  161. "dehumidifier",
  162. "GoldairDehumidifier",
  163. )
  164. def test_goldair_fan_detection(self):
  165. """Test that Goldair fan can be detected from its sample payload."""
  166. self._test_detect(FAN_PAYLOAD, "fan", None)
  167. def test_kogan_socket_detection(self):
  168. """Test that 1st gen Kogan Socket can be detected from its sample payload."""
  169. self._test_detect(
  170. KOGAN_SOCKET_PAYLOAD,
  171. "kogan_switch",
  172. None,
  173. )
  174. def test_kogan_socket2_detection(self):
  175. """Test that 2nd gen Kogan Socket can be detected from its sample payload."""
  176. self._test_detect(
  177. KOGAN_SOCKET_PAYLOAD2,
  178. "kogan_switch",
  179. None,
  180. )
  181. def test_gsh_heater_detection(self):
  182. """Test that GSH heater can be detected from its sample payload."""
  183. self._test_detect(
  184. GSH_HEATER_PAYLOAD,
  185. "gsh_heater",
  186. None,
  187. )
  188. def test_gardenpac_heatpump_detection(self):
  189. """Test that GardenPac heatpump can be detected from its sample payload."""
  190. self._test_detect(
  191. GARDENPAC_HEATPUMP_PAYLOAD,
  192. "gardenpac_heatpump",
  193. None,
  194. )
  195. def test_purline_heater_detection(self):
  196. """Test that Purline heater can be detected from its sample payload."""
  197. self._test_detect(
  198. PURLINE_M100_HEATER_PAYLOAD,
  199. "purline_m100_heater",
  200. None,
  201. )
  202. # Non-legacy devices endup being the same as the tests in test_device.py, so
  203. # skip them.