test_switch.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Tests for the switch 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_SWITCH,
  7. CONF_TYPE,
  8. DOMAIN,
  9. )
  10. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  11. from custom_components.tuya_local.switch 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: "kogan_switch", CONF_DEVICE_ID: "dummy", CONF_SWITCH: True},
  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_SWITCH]) == TuyaLocalSwitch
  28. m_add_entities.assert_called_once()
  29. async def test_init_entry_as_secondary(hass):
  30. """Test the initialisation."""
  31. entry = MockConfigEntry(
  32. domain=DOMAIN,
  33. data={CONF_TYPE: "deta_fan", CONF_DEVICE_ID: "dummy", "switch_master": True},
  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. await async_setup_entry(hass, entry, m_add_entities)
  44. assert type(hass.data[DOMAIN]["dummy"]["switch_master"]) == TuyaLocalSwitch
  45. m_add_entities.assert_called_once()
  46. async def test_init_entry_fails_if_device_has_no_switch(hass):
  47. """Test initialisation when device has no matching entity"""
  48. entry = MockConfigEntry(
  49. domain=DOMAIN,
  50. data={CONF_TYPE: "kogan_heater", CONF_DEVICE_ID: "dummy", CONF_SWITCH: True},
  51. )
  52. # although async, the async_add_entities function passed to
  53. # async_setup_entry is called truly asynchronously. If we use
  54. # AsyncMock, it expects us to await the result.
  55. m_add_entities = Mock()
  56. m_device = AsyncMock()
  57. hass.data[DOMAIN] = {}
  58. hass.data[DOMAIN]["dummy"] = {}
  59. hass.data[DOMAIN]["dummy"]["device"] = m_device
  60. try:
  61. await async_setup_entry(hass, entry, m_add_entities)
  62. assert False
  63. except ValueError:
  64. pass
  65. m_add_entities.assert_not_called()
  66. async def test_init_entry_fails_if_config_is_missing(hass):
  67. """Test initialisation when device has no matching entity"""
  68. entry = MockConfigEntry(
  69. domain=DOMAIN,
  70. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy", CONF_SWITCH: True},
  71. )
  72. # although async, the async_add_entities function passed to
  73. # async_setup_entry is called truly asynchronously. If we use
  74. # AsyncMock, it expects us to await the result.
  75. m_add_entities = Mock()
  76. m_device = AsyncMock()
  77. hass.data[DOMAIN] = {}
  78. hass.data[DOMAIN]["dummy"] = {}
  79. hass.data[DOMAIN]["dummy"]["device"] = m_device
  80. try:
  81. await async_setup_entry(hass, entry, m_add_entities)
  82. assert False
  83. except ValueError:
  84. pass
  85. m_add_entities.assert_not_called()