test_event.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.event import (
  6. TuyaLocalEvent,
  7. async_setup_entry,
  8. )
  9. from custom_components.tuya_local.const import (
  10. CONF_DEVICE_ID,
  11. CONF_PROTOCOL_VERSION,
  12. CONF_TYPE,
  13. DOMAIN,
  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"]) == 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()