test_select.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Tests for the select 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.select import TuyaLocalSelect, async_setup_entry
  12. @pytest.mark.asyncio
  13. async def test_init_entry(hass):
  14. """Test the initialisation."""
  15. entry = MockConfigEntry(
  16. domain=DOMAIN,
  17. data={
  18. CONF_TYPE: "arlec_fan",
  19. CONF_DEVICE_ID: "dummy",
  20. CONF_PROTOCOL_VERSION: "auto",
  21. },
  22. )
  23. m_add_entities = Mock()
  24. m_device = AsyncMock()
  25. hass.data[DOMAIN] = {
  26. "dummy": {"device": m_device},
  27. }
  28. await async_setup_entry(hass, entry, m_add_entities)
  29. assert type(hass.data[DOMAIN]["dummy"]["select_timer"]) is TuyaLocalSelect
  30. m_add_entities.assert_called_once()
  31. @pytest.mark.asyncio
  32. async def test_init_entry_fails_if_device_has_no_select(hass):
  33. """Test initialisation when device has no matching entity"""
  34. entry = MockConfigEntry(
  35. domain=DOMAIN,
  36. data={
  37. CONF_TYPE: "simple_switch",
  38. CONF_DEVICE_ID: "dummy",
  39. CONF_PROTOCOL_VERSION: "auto",
  40. },
  41. )
  42. m_add_entities = Mock()
  43. m_device = AsyncMock()
  44. hass.data[DOMAIN] = {
  45. "dummy": {"device": m_device},
  46. }
  47. try:
  48. await async_setup_entry(hass, entry, m_add_entities)
  49. assert False
  50. except ValueError:
  51. pass
  52. m_add_entities.assert_not_called()
  53. @pytest.mark.asyncio
  54. async def test_init_entry_fails_if_config_is_missing(hass):
  55. """Test initialisation when device has no matching entity"""
  56. entry = MockConfigEntry(
  57. domain=DOMAIN,
  58. data={
  59. CONF_TYPE: "non_existing",
  60. CONF_DEVICE_ID: "dummy",
  61. CONF_PROTOCOL_VERSION: "auto",
  62. },
  63. )
  64. m_add_entities = Mock()
  65. m_device = AsyncMock()
  66. hass.data[DOMAIN] = {
  67. "dummy": {"device": m_device},
  68. }
  69. try:
  70. await async_setup_entry(hass, entry, m_add_entities)
  71. assert False
  72. except ValueError:
  73. pass
  74. m_add_entities.assert_not_called()