test_device_config.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. self.assertIsNotNone(parsed._config.get("name"), f"name missing from {cfg}")
  51. self.assertIsNotNone(
  52. parsed._config.get("primary_entity"),
  53. f"primary_entity missing from {cfg}",
  54. )
  55. self.check_entity(parsed.primary_entity, cfg)
  56. for entity in parsed.secondary_entities():
  57. self.check_entity(entity, cfg)
  58. # Most of the device_config functionality is exercised during testing of
  59. # the various supported devices. These tests concentrate only on the gaps.
  60. def test_match_quality(self):
  61. """Test the match_quality function."""
  62. cfg = get_config("deta_fan")
  63. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  64. self.assertEqual(q, 0)
  65. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  66. self.assertEqual(q, 0)
  67. def test_entity_find_unknown_dps_fails(self):
  68. """Test that finding a dps that doesn't exist fails."""
  69. cfg = get_config("kogan_switch")
  70. non_existing = cfg.primary_entity.find_dps("missing")
  71. self.assertIsNone(non_existing)
  72. async def test_dps_async_set_readonly_value_fails(self):
  73. """Test that setting a readonly dps fails."""
  74. mock_device = MagicMock()
  75. cfg = get_config("kogan_switch")
  76. voltage = cfg.primary_entity.find_dps("voltage_v")
  77. with self.assertRaises(TypeError):
  78. await voltage.async_set_value(mock_device, 230)
  79. def test_dps_values_returns_none_with_no_mapping(self):
  80. """
  81. Test that a dps with no mapping returns None as its possible values
  82. """
  83. mock_device = MagicMock()
  84. cfg = get_config("kogan_switch")
  85. voltage = cfg.primary_entity.find_dps("voltage_v")
  86. self.assertIsNone(voltage.values(mock_device))
  87. def test_config_returned(self):
  88. """Test that config file is returned by config"""
  89. cfg = get_config("kogan_switch")
  90. self.assertEqual(cfg.config, "smartplugv1.yaml")