test_cover.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Tests for the cover 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.cover import TuyaLocalCover
  10. from custom_components.tuya_local.cover 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: "garage_door_opener",
  17. CONF_DEVICE_ID: "dummy",
  18. },
  19. )
  20. m_add_entities = Mock()
  21. m_device = AsyncMock()
  22. hass.data[DOMAIN] = {
  23. "dummy": {
  24. "device": m_device,
  25. },
  26. }
  27. await async_setup_entry(hass, entry, m_add_entities)
  28. assert type(hass.data[DOMAIN]["dummy"]["cover"]) == TuyaLocalCover
  29. m_add_entities.assert_called_once()
  30. async def test_init_entry_fails_if_device_has_no_cover(hass):
  31. """Test initialisation when device has no matching entity"""
  32. entry = MockConfigEntry(
  33. domain=DOMAIN,
  34. data={CONF_TYPE: "kogan_heater", CONF_DEVICE_ID: "dummy"},
  35. )
  36. m_add_entities = Mock()
  37. m_device = AsyncMock()
  38. hass.data[DOMAIN] = {
  39. "dummy": {
  40. "device": m_device,
  41. },
  42. }
  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()