test_switch.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. CONF_TYPE_AUTO,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  12. from custom_components.tuya_local.switch import async_setup_entry
  13. async def test_init_entry(hass):
  14. """Test the initialisation."""
  15. entry = MockConfigEntry(
  16. domain=DOMAIN,
  17. data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
  18. )
  19. # although async, the async_add_entities function passed to
  20. # async_setup_entry is called truly asynchronously. If we use
  21. # AsyncMock, it expects us to await the result.
  22. m_add_entities = Mock()
  23. m_device = AsyncMock()
  24. m_device.async_inferred_type = AsyncMock(return_value="kogan_switch")
  25. hass.data[DOMAIN] = {}
  26. hass.data[DOMAIN]["dummy"] = {}
  27. hass.data[DOMAIN]["dummy"]["device"] = m_device
  28. await async_setup_entry(hass, entry, m_add_entities)
  29. assert type(hass.data[DOMAIN]["dummy"][CONF_SWITCH]) == TuyaLocalSwitch
  30. m_add_entities.assert_called_once()