test_climate.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Tests for the light 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_CLIMATE,
  6. CONF_DEVICE_ID,
  7. CONF_TYPE,
  8. DOMAIN,
  9. )
  10. from custom_components.tuya_local.dehumidifier.climate import GoldairDehumidifier
  11. from custom_components.tuya_local.heater.climate import GoldairHeater
  12. from custom_components.tuya_local.climate 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: "heater", 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. hass.data[DOMAIN] = {}
  25. hass.data[DOMAIN]["dummy"] = {}
  26. hass.data[DOMAIN]["dummy"]["device"] = m_device
  27. await async_setup_entry(hass, entry, m_add_entities)
  28. assert type(hass.data[DOMAIN]["dummy"][CONF_CLIMATE]) == GoldairHeater
  29. m_add_entities.assert_called_once()
  30. async def test_init_entry_as_secondary(hass):
  31. """Test initialisation when fan is a secondary entity"""
  32. entry = MockConfigEntry(
  33. domain=DOMAIN,
  34. data={CONF_TYPE: "dehumidifier", CONF_DEVICE_ID: "dummy"},
  35. )
  36. # although async, the async_add_entities function passed to
  37. # async_setup_entry is called truly asynchronously. If we use
  38. # AsyncMock, it expects us to await the result.
  39. m_add_entities = Mock()
  40. m_device = AsyncMock()
  41. hass.data[DOMAIN] = {}
  42. hass.data[DOMAIN]["dummy"] = {}
  43. hass.data[DOMAIN]["dummy"]["device"] = m_device
  44. await async_setup_entry(hass, entry, m_add_entities)
  45. assert type(hass.data[DOMAIN]["dummy"][CONF_CLIMATE]) == GoldairDehumidifier
  46. m_add_entities.assert_called_once()
  47. async def test_init_entry_fails_if_device_has_no_climate(hass):
  48. """Test initialisation when device has no matching entity"""
  49. entry = MockConfigEntry(
  50. domain=DOMAIN,
  51. data={CONF_TYPE: "kogan_switch", CONF_DEVICE_ID: "dummy"},
  52. )
  53. # although async, the async_add_entities function passed to
  54. # async_setup_entry is called truly asynchronously. If we use
  55. # AsyncMock, it expects us to await the result.
  56. m_add_entities = Mock()
  57. m_device = AsyncMock()
  58. hass.data[DOMAIN] = {}
  59. hass.data[DOMAIN]["dummy"] = {}
  60. hass.data[DOMAIN]["dummy"]["device"] = m_device
  61. try:
  62. await async_setup_entry(hass, entry, m_add_entities)
  63. assert False
  64. except ValueError:
  65. pass
  66. m_add_entities.assert_not_called()
  67. async def test_init_entry_fails_if_config_is_missing(hass):
  68. """Test initialisation when device has no matching entity"""
  69. entry = MockConfigEntry(
  70. domain=DOMAIN,
  71. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy"},
  72. )
  73. # although async, the async_add_entities function passed to
  74. # async_setup_entry is called truly asynchronously. If we use
  75. # AsyncMock, it expects us to await the result.
  76. m_add_entities = Mock()
  77. m_device = AsyncMock()
  78. hass.data[DOMAIN] = {}
  79. hass.data[DOMAIN]["dummy"] = {}
  80. hass.data[DOMAIN]["dummy"]["device"] = m_device
  81. try:
  82. await async_setup_entry(hass, entry, m_add_entities)
  83. assert False
  84. except ValueError:
  85. pass
  86. m_add_entities.assert_not_called()