test_device_config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Test the config parser"""
  2. from unittest import IsolatedAsyncioTestCase, TestCase
  3. from unittest.mock import MagicMock
  4. from custom_components.tuya_local.helpers.device_config import (
  5. available_configs,
  6. get_config,
  7. TuyaDeviceConfig,
  8. )
  9. from .const import (
  10. GPPH_HEATER_PAYLOAD,
  11. KOGAN_HEATER_PAYLOAD,
  12. )
  13. class TestDeviceConfig(IsolatedAsyncioTestCase):
  14. """Test the device config parser"""
  15. def test_can_find_config_files(self):
  16. """Test that the config files can be found by the parser."""
  17. found = False
  18. for cfg in available_configs():
  19. found = True
  20. break
  21. self.assertTrue(found)
  22. def check_entity(self, entity, cfg):
  23. """
  24. Check that the entity has a dps list and each dps has an id,
  25. type and name.
  26. """
  27. self.assertIsNotNone(
  28. entity._config.get("entity"), f"entity type missing in {cfg}"
  29. )
  30. e = entity.config_id
  31. self.assertIsNotNone(
  32. entity._config.get("dps"), f"dps missing from {e} in {cfg}"
  33. )
  34. for dp in entity.dps():
  35. self.assertIsNotNone(
  36. dp._config.get("id"), f"dp id missing from {e} in {cfg}"
  37. )
  38. self.assertIsNotNone(
  39. dp._config.get("type"), f"dp type missing from {e} in {cfg}"
  40. )
  41. self.assertIsNotNone(
  42. dp._config.get("name"), f"dp name missing from {e} in {cfg}"
  43. )
  44. def test_config_files_parse(self):
  45. """
  46. All configs should be parsable and meet certain criteria
  47. """
  48. for cfg in available_configs():
  49. parsed = TuyaDeviceConfig(cfg)
  50. # Check for error messages or unparsed config
  51. if isinstance(parsed, str) or isinstance(parsed._config, str):
  52. self.fail(f"unparsable yaml in {cfg}")
  53. self.assertIsNotNone(parsed._config.get("name"), f"name missing from {cfg}")
  54. self.assertIsNotNone(
  55. parsed._config.get("primary_entity"),
  56. f"primary_entity missing from {cfg}",
  57. )
  58. self.check_entity(parsed.primary_entity, cfg)
  59. for entity in parsed.secondary_entities():
  60. self.check_entity(entity, cfg)
  61. # Most of the device_config functionality is exercised during testing of
  62. # the various supported devices. These tests concentrate only on the gaps.
  63. def test_match_quality(self):
  64. """Test the match_quality function."""
  65. cfg = get_config("deta_fan")
  66. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  67. self.assertEqual(q, 0)
  68. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  69. self.assertEqual(q, 0)
  70. def test_entity_find_unknown_dps_fails(self):
  71. """Test that finding a dps that doesn't exist fails."""
  72. cfg = get_config("kogan_switch")
  73. non_existing = cfg.primary_entity.find_dps("missing")
  74. self.assertIsNone(non_existing)
  75. async def test_dps_async_set_readonly_value_fails(self):
  76. """Test that setting a readonly dps fails."""
  77. mock_device = MagicMock()
  78. cfg = get_config("kogan_switch")
  79. voltage = cfg.primary_entity.find_dps("voltage_v")
  80. with self.assertRaises(TypeError):
  81. await voltage.async_set_value(mock_device, 230)
  82. def test_dps_values_returns_none_with_no_mapping(self):
  83. """
  84. Test that a dps with no mapping returns None as its possible values
  85. """
  86. mock_device = MagicMock()
  87. cfg = get_config("kogan_switch")
  88. voltage = cfg.primary_entity.find_dps("voltage_v")
  89. self.assertIsNone(voltage.values(mock_device))
  90. def test_config_returned(self):
  91. """Test that config file is returned by config"""
  92. cfg = get_config("kogan_switch")
  93. self.assertEqual(cfg.config, "smartplugv1.yaml")