test_device_config.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Test the config parser"""
  2. from unittest import IsolatedAsyncioTestCase
  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 test_config_files_parse(self):
  23. """
  24. All configs should be parsable and have at least a name and primary entity.
  25. """
  26. for cfg in available_configs():
  27. parsed = TuyaDeviceConfig(cfg)
  28. self.assertIsNotNone(parsed.name)
  29. self.assertIsNotNone(parsed.primary_entity)
  30. # Most of the device_config functionality is exercised during testing of
  31. # the various supported devices. These tests concentrate only on the gaps.
  32. def test_match_quality(self):
  33. """Test the match_quality function."""
  34. cfg = get_config("deta_fan")
  35. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  36. self.assertEqual(q, 0)
  37. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  38. self.assertEqual(q, 0)
  39. def test_entity_find_unknown_dps_fails(self):
  40. """Test that finding a dps that doesn't exist fails."""
  41. cfg = get_config("kogan_switch")
  42. non_existing = cfg.primary_entity.find_dps("missing")
  43. self.assertIsNone(non_existing)
  44. async def test_dps_async_set_readonly_value_fails(self):
  45. """Test that setting a readonly dps fails."""
  46. mock_device = MagicMock()
  47. cfg = get_config("kogan_switch")
  48. voltage = cfg.primary_entity.find_dps("voltage_v")
  49. with self.assertRaises(TypeError):
  50. await voltage.async_set_value(mock_device, 230)
  51. def test_dps_values_returns_none_with_no_mapping(self):
  52. """Test that a dps with no mapping returns None as its possible values"""
  53. mock_device = MagicMock()
  54. cfg = get_config("kogan_switch")
  55. voltage = cfg.primary_entity.find_dps("voltage_v")
  56. self.assertIsNone(voltage.values(mock_device))
  57. def test_config_returned(self):
  58. """Test that config file is returned by config"""
  59. cfg = get_config("kogan_switch")
  60. self.assertEqual(cfg.config, "smartplugv1.yaml")