4
0

test_climate.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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", CONF_CLIMATE: True},
  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={
  35. CONF_TYPE: "goldair_dehumidifier",
  36. CONF_DEVICE_ID: "dummy",
  37. "climate_dehumidifier_as_climate": True,
  38. },
  39. )
  40. # although async, the async_add_entities function passed to
  41. # async_setup_entry is called truly asynchronously. If we use
  42. # AsyncMock, it expects us to await the result.
  43. m_add_entities = Mock()
  44. m_device = AsyncMock()
  45. hass.data[DOMAIN] = {}
  46. hass.data[DOMAIN]["dummy"] = {}
  47. hass.data[DOMAIN]["dummy"]["device"] = m_device
  48. await async_setup_entry(hass, entry, m_add_entities)
  49. assert (
  50. type(hass.data[DOMAIN]["dummy"]["climate_dehumidifier_as_climate"])
  51. == GoldairDehumidifier
  52. )
  53. m_add_entities.assert_called_once()
  54. async def test_init_entry_fails_if_device_has_no_climate(hass):
  55. """Test initialisation when device has no matching entity"""
  56. entry = MockConfigEntry(
  57. domain=DOMAIN,
  58. data={CONF_TYPE: "kogan_switch", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: True},
  59. )
  60. # although async, the async_add_entities function passed to
  61. # async_setup_entry is called truly asynchronously. If we use
  62. # AsyncMock, it expects us to await the result.
  63. m_add_entities = Mock()
  64. m_device = AsyncMock()
  65. hass.data[DOMAIN] = {}
  66. hass.data[DOMAIN]["dummy"] = {}
  67. hass.data[DOMAIN]["dummy"]["device"] = m_device
  68. try:
  69. await async_setup_entry(hass, entry, m_add_entities)
  70. assert False
  71. except ValueError:
  72. pass
  73. m_add_entities.assert_not_called()
  74. async def test_init_entry_fails_if_config_is_missing(hass):
  75. """Test initialisation when device has no matching entity"""
  76. entry = MockConfigEntry(
  77. domain=DOMAIN,
  78. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: True},
  79. )
  80. # although async, the async_add_entities function passed to
  81. # async_setup_entry is called truly asynchronously. If we use
  82. # AsyncMock, it expects us to await the result.
  83. m_add_entities = Mock()
  84. m_device = AsyncMock()
  85. hass.data[DOMAIN] = {}
  86. hass.data[DOMAIN]["dummy"] = {}
  87. hass.data[DOMAIN]["dummy"]["device"] = m_device
  88. try:
  89. await async_setup_entry(hass, entry, m_add_entities)
  90. assert False
  91. except ValueError:
  92. pass
  93. m_add_entities.assert_not_called()