4
0

test_event.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Tests for the event entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from pytest_homeassistant_custom_component.common import MockConfigEntry
  5. from custom_components.tuya_local.const import (
  6. CONF_DEVICE_ID,
  7. CONF_PROTOCOL_VERSION,
  8. CONF_TYPE,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.event import (
  12. TuyaLocalEvent,
  13. async_setup_entry,
  14. )
  15. @pytest.mark.asyncio
  16. async def test_init_entry(hass):
  17. """Test the initialisation."""
  18. entry = MockConfigEntry(
  19. domain=DOMAIN,
  20. data={
  21. CONF_TYPE: "linkoze_dual_button",
  22. CONF_DEVICE_ID: "dummy",
  23. CONF_PROTOCOL_VERSION: "auto",
  24. },
  25. )
  26. m_add_entities = Mock()
  27. m_device = AsyncMock()
  28. hass.data[DOMAIN] = {
  29. "dummy": {"device": m_device},
  30. }
  31. await async_setup_entry(hass, entry, m_add_entities)
  32. assert type(hass.data[DOMAIN]["dummy"]["event_button_1"]) is TuyaLocalEvent
  33. m_add_entities.assert_called_once()
  34. @pytest.mark.asyncio
  35. async def test_init_entry_fails_if_device_has_no_event(hass):
  36. """Test initialisation when device has no matching entity"""
  37. entry = MockConfigEntry(
  38. domain=DOMAIN,
  39. data={
  40. CONF_TYPE: "mirabella_genio_usb",
  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": {"device": m_device},
  49. }
  50. try:
  51. await async_setup_entry(hass, entry, m_add_entities)
  52. assert False
  53. except ValueError:
  54. pass
  55. m_add_entities.assert_not_called()
  56. @pytest.mark.asyncio
  57. async def test_init_entry_fails_if_config_is_missing(hass):
  58. """Test initialisation when device has no matching entity"""
  59. entry = MockConfigEntry(
  60. domain=DOMAIN,
  61. data={
  62. CONF_TYPE: "non_existing",
  63. CONF_DEVICE_ID: "dummy",
  64. CONF_PROTOCOL_VERSION: "auto",
  65. },
  66. )
  67. m_add_entities = Mock()
  68. m_device = AsyncMock()
  69. hass.data[DOMAIN] = {
  70. "dummy": {"device": m_device},
  71. }
  72. try:
  73. await async_setup_entry(hass, entry, m_add_entities)
  74. assert False
  75. except ValueError:
  76. pass
  77. m_add_entities.assert_not_called()