test_lock.py 2.7 KB

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