test_climate.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Tests for the light 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.climate import TuyaLocalClimate, async_setup_entry
  6. from custom_components.tuya_local.const import (
  7. CONF_DEVICE_ID,
  8. CONF_PROTOCOL_VERSION,
  9. CONF_TYPE,
  10. DOMAIN,
  11. )
  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: "heater",
  19. CONF_DEVICE_ID: "dummy",
  20. CONF_PROTOCOL_VERSION: "auto",
  21. },
  22. )
  23. # although async, the async_add_entities function passed to
  24. # async_setup_entry is called truly asynchronously. If we use
  25. # AsyncMock, it expects us to await the result.
  26. m_add_entities = Mock()
  27. m_device = AsyncMock()
  28. hass.data[DOMAIN] = {}
  29. hass.data[DOMAIN]["dummy"] = {}
  30. hass.data[DOMAIN]["dummy"]["device"] = m_device
  31. await async_setup_entry(hass, entry, m_add_entities)
  32. assert type(hass.data[DOMAIN]["dummy"]["climate"]) == TuyaLocalClimate
  33. m_add_entities.assert_called_once()
  34. # After removal of deprecated entities, there are no secondary climate devices to test against.
  35. # async def test_init_entry_as_secondary(hass):
  36. # """Test initialisation when climate is a secondary entity"""
  37. # entry = MockConfigEntry(
  38. # domain=DOMAIN,
  39. # data={
  40. # CONF_TYPE: "goldair_dehumidifier",
  41. # CONF_DEVICE_ID: "dummy",
  42. # },
  43. # )
  44. # # although async, the async_add_entities function passed to
  45. # # async_setup_entry is called truly asynchronously. If we use
  46. # # AsyncMock, it expects us to await the result.
  47. # m_add_entities = Mock()
  48. # m_device = AsyncMock()
  49. # hass.data[DOMAIN] = {}
  50. # hass.data[DOMAIN]["dummy"] = {}
  51. # hass.data[DOMAIN]["dummy"]["device"] = m_device
  52. # await async_setup_entry(hass, entry, m_add_entities)
  53. # assert (
  54. # type(hass.data[DOMAIN]["dummy"]["climate_dehumidifier_as_climate"])
  55. # == TuyaLocalClimate
  56. # )
  57. # m_add_entities.assert_called_once()
  58. @pytest.mark.asyncio
  59. async def test_init_entry_fails_if_device_has_no_climate(hass):
  60. """Test initialisation when device has no matching entity"""
  61. entry = MockConfigEntry(
  62. domain=DOMAIN,
  63. data={
  64. CONF_TYPE: "smartplugv1",
  65. CONF_DEVICE_ID: "dummy",
  66. CONF_PROTOCOL_VERSION: "auto",
  67. },
  68. )
  69. # although async, the async_add_entities function passed to
  70. # async_setup_entry is called truly asynchronously. If we use
  71. # AsyncMock, it expects us to await the result.
  72. m_add_entities = Mock()
  73. m_device = AsyncMock()
  74. hass.data[DOMAIN] = {}
  75. hass.data[DOMAIN]["dummy"] = {}
  76. hass.data[DOMAIN]["dummy"]["device"] = m_device
  77. try:
  78. await async_setup_entry(hass, entry, m_add_entities)
  79. assert False
  80. except ValueError:
  81. pass
  82. m_add_entities.assert_not_called()
  83. @pytest.mark.asyncio
  84. async def test_init_entry_fails_if_config_is_missing(hass):
  85. """Test initialisation when device has no matching entity"""
  86. entry = MockConfigEntry(
  87. domain=DOMAIN,
  88. data={
  89. CONF_TYPE: "non_existing",
  90. CONF_DEVICE_ID: "dummy",
  91. CONF_PROTOCOL_VERSION: "auto",
  92. },
  93. )
  94. # although async, the async_add_entities function passed to
  95. # async_setup_entry is called truly asynchronously. If we use
  96. # AsyncMock, it expects us to await the result.
  97. m_add_entities = Mock()
  98. m_device = AsyncMock()
  99. hass.data[DOMAIN] = {}
  100. hass.data[DOMAIN]["dummy"] = {}
  101. hass.data[DOMAIN]["dummy"]["device"] = m_device
  102. try:
  103. await async_setup_entry(hass, entry, m_add_entities)
  104. assert False
  105. except ValueError:
  106. pass
  107. m_add_entities.assert_not_called()