test_cover.py 2.6 KB

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