test_climate.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. m_device.get_property = Mock(return_value=None)
  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"]) is 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. # is 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: "smartplugv1",
  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()