test_climate.py 3.8 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.generic.climate import TuyaLocalClimate
  11. from custom_components.tuya_local.climate 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: "heater", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: 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_CLIMATE]) == TuyaLocalClimate
  28. m_add_entities.assert_called_once()
  29. # After removal of deprecated entities, there are no secondary climate devices to test against.
  30. # async def test_init_entry_as_secondary(hass):
  31. # """Test initialisation when climate 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. # == TuyaLocalClimate
  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()