test_climate.py 4.0 KB

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