test_climate.py 3.7 KB

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