4
0

test_climate.py 3.9 KB

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