test_device_config.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. for cfg in available_configs():
  24. parsed = TuyaDeviceConfig(cfg)
  25. self.assertIsNotNone(parsed.name)
  26. def test_config_files_have_legacy_link(self):
  27. """
  28. Initially, we require a link between the new style config, and the old
  29. classes so we can transition over to the new config. When the
  30. transition is complete, we will drop the requirement, as new devices
  31. will only be added as config files.
  32. """
  33. for cfg in available_configs():
  34. parsed = TuyaDeviceConfig(cfg)
  35. self.assertIsNotNone(parsed.legacy_type)
  36. self.assertIsNotNone(parsed.primary_entity)
  37. # Most of the device_config functionality is exercised during testing of
  38. # the various supported devices. These tests concentrate only on the gaps.
  39. def test_match_quality(self):
  40. """Test the match_quality function."""
  41. cfg = get_config("deta_fan")
  42. q = cfg.match_quality({**KOGAN_HEATER_PAYLOAD, "updated_at": 0})
  43. self.assertEqual(q, 0)
  44. q = cfg.match_quality({**GPPH_HEATER_PAYLOAD})
  45. self.assertEqual(q, 0)
  46. def test_entity_find_unknown_dps_fails(self):
  47. """Test that finding a dps that doesn't exist fails."""
  48. cfg = get_config("kogan_switch")
  49. non_existing = cfg.primary_entity.find_dps("missing")
  50. self.assertIsNone(non_existing)
  51. async def test_dps_async_set_readonly_value_fails(self):
  52. """Test that setting a readonly dps fails."""
  53. mock_device = MagicMock()
  54. cfg = get_config("kogan_switch")
  55. voltage = cfg.primary_entity.find_dps("voltage_v")
  56. with self.assertRaises(TypeError):
  57. await voltage.async_set_value(mock_device, 230)
  58. def test_dps_values_returns_none_with_no_mapping(self):
  59. """Test that a dps with no mapping returns None as its possible values"""
  60. mock_device = MagicMock()
  61. cfg = get_config("kogan_switch")
  62. voltage = cfg.primary_entity.find_dps("voltage_v")
  63. self.assertIsNone(voltage.values(mock_device))
  64. def test_config_returned(self):
  65. """Test that config file is returned by config"""
  66. cfg = get_config("kogan_switch")
  67. self.assertEqual(cfg.config, "smartplugv1.yaml")