test_device_config.py 8.9 KB

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