test_climate.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_CLIMATE,
  6. CONF_DEVICE_ID,
  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={CONF_TYPE: "heater", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: True},
  17. )
  18. # although async, the async_add_entities function passed to
  19. # async_setup_entry is called truly asynchronously. If we use
  20. # AsyncMock, it expects us to await the result.
  21. m_add_entities = Mock()
  22. m_device = AsyncMock()
  23. hass.data[DOMAIN] = {}
  24. hass.data[DOMAIN]["dummy"] = {}
  25. hass.data[DOMAIN]["dummy"]["device"] = m_device
  26. await async_setup_entry(hass, entry, m_add_entities)
  27. assert type(hass.data[DOMAIN]["dummy"][CONF_CLIMATE]) == TuyaLocalClimate
  28. m_add_entities.assert_called_once()
  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. "climate_dehumidifier_as_climate": True,
  37. },
  38. )
  39. # although async, the async_add_entities function passed to
  40. # async_setup_entry is called truly asynchronously. If we use
  41. # AsyncMock, it expects us to await the result.
  42. m_add_entities = Mock()
  43. m_device = AsyncMock()
  44. hass.data[DOMAIN] = {}
  45. hass.data[DOMAIN]["dummy"] = {}
  46. hass.data[DOMAIN]["dummy"]["device"] = m_device
  47. await async_setup_entry(hass, entry, m_add_entities)
  48. assert (
  49. type(hass.data[DOMAIN]["dummy"]["climate_dehumidifier_as_climate"])
  50. == TuyaLocalClimate
  51. )
  52. m_add_entities.assert_called_once()
  53. async def test_init_entry_fails_if_device_has_no_climate(hass):
  54. """Test initialisation when device has no matching entity"""
  55. entry = MockConfigEntry(
  56. domain=DOMAIN,
  57. data={CONF_TYPE: "kogan_switch", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: True},
  58. )
  59. # although async, the async_add_entities function passed to
  60. # async_setup_entry is called truly asynchronously. If we use
  61. # AsyncMock, it expects us to await the result.
  62. m_add_entities = Mock()
  63. m_device = AsyncMock()
  64. hass.data[DOMAIN] = {}
  65. hass.data[DOMAIN]["dummy"] = {}
  66. hass.data[DOMAIN]["dummy"]["device"] = m_device
  67. try:
  68. await async_setup_entry(hass, entry, m_add_entities)
  69. assert False
  70. except ValueError:
  71. pass
  72. m_add_entities.assert_not_called()
  73. async def test_init_entry_fails_if_config_is_missing(hass):
  74. """Test initialisation when device has no matching entity"""
  75. entry = MockConfigEntry(
  76. domain=DOMAIN,
  77. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy", CONF_CLIMATE: True},
  78. )
  79. # although async, the async_add_entities function passed to
  80. # async_setup_entry is called truly asynchronously. If we use
  81. # AsyncMock, it expects us to await the result.
  82. m_add_entities = Mock()
  83. m_device = AsyncMock()
  84. hass.data[DOMAIN] = {}
  85. hass.data[DOMAIN]["dummy"] = {}
  86. hass.data[DOMAIN]["dummy"]["device"] = m_device
  87. try:
  88. await async_setup_entry(hass, entry, m_add_entities)
  89. assert False
  90. except ValueError:
  91. pass
  92. m_add_entities.assert_not_called()