test_light.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """Tests for the light entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. from unittest.mock import AsyncMock, Mock
  4. from custom_components.tuya_local.const import (
  5. CONF_DEVICE_ID,
  6. CONF_TYPE,
  7. DOMAIN,
  8. )
  9. from custom_components.tuya_local.generic.light import TuyaLocalLight
  10. from custom_components.tuya_local.light import async_setup_entry
  11. async def test_init_entry(hass):
  12. """Test the initialisation."""
  13. entry = MockConfigEntry(
  14. domain=DOMAIN,
  15. data={
  16. CONF_TYPE: "goldair_gpph_heater",
  17. CONF_DEVICE_ID: "dummy",
  18. },
  19. )
  20. # although async, the async_add_entities function passed to
  21. # async_setup_entry is called truly asynchronously. If we use
  22. # AsyncMock, it expects us to await the result.
  23. m_add_entities = Mock()
  24. m_device = AsyncMock()
  25. hass.data[DOMAIN] = {}
  26. hass.data[DOMAIN]["dummy"] = {}
  27. hass.data[DOMAIN]["dummy"]["device"] = m_device
  28. await async_setup_entry(hass, entry, m_add_entities)
  29. assert type(hass.data[DOMAIN]["dummy"]["light_display"]) == TuyaLocalLight
  30. m_add_entities.assert_called_once()
  31. async def test_init_entry_fails_if_device_has_no_light(hass):
  32. """Test initialisation when device has no matching entity"""
  33. entry = MockConfigEntry(
  34. domain=DOMAIN,
  35. data={CONF_TYPE: "kogan_switch", CONF_DEVICE_ID: "dummy"},
  36. )
  37. # although async, the async_add_entities function passed to
  38. # async_setup_entry is called truly asynchronously. If we use
  39. # AsyncMock, it expects us to await the result.
  40. m_add_entities = Mock()
  41. m_device = AsyncMock()
  42. hass.data[DOMAIN] = {}
  43. hass.data[DOMAIN]["dummy"] = {}
  44. hass.data[DOMAIN]["dummy"]["device"] = m_device
  45. try:
  46. await async_setup_entry(hass, entry, m_add_entities)
  47. assert False
  48. except ValueError:
  49. pass
  50. m_add_entities.assert_not_called()
  51. async def test_init_entry_fails_if_config_is_missing(hass):
  52. """Test initialisation when device has no matching entity"""
  53. entry = MockConfigEntry(
  54. domain=DOMAIN,
  55. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy"},
  56. )
  57. # although async, the async_add_entities function passed to
  58. # async_setup_entry is called truly asynchronously. If we use
  59. # AsyncMock, it expects us to await the result.
  60. m_add_entities = Mock()
  61. m_device = AsyncMock()
  62. hass.data[DOMAIN] = {}
  63. hass.data[DOMAIN]["dummy"] = {}
  64. hass.data[DOMAIN]["dummy"]["device"] = m_device
  65. try:
  66. await async_setup_entry(hass, entry, m_add_entities)
  67. assert False
  68. except ValueError:
  69. pass
  70. m_add_entities.assert_not_called()