test_humidifier.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Tests for the humidifier 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.humidifier import TuyaLocalHumidifier
  10. from custom_components.tuya_local.humidifier import async_setup_entry
  11. async def test_init_entry(hass):
  12. """Test the initialisation."""
  13. entry = MockConfigEntry(
  14. domain=DOMAIN,
  15. data={
  16. CONF_TYPE: "dehumidifier",
  17. CONF_DEVICE_ID: "dummy",
  18. },
  19. )
  20. # although async, the async_add_entities function passed to
  21. # async_setup_entry is called truly asynchronously. If we use
  22. # AsyncMock, it expects us to await the result.
  23. m_add_entities = Mock()
  24. m_device = AsyncMock()
  25. hass.data[DOMAIN] = {}
  26. hass.data[DOMAIN]["dummy"] = {}
  27. hass.data[DOMAIN]["dummy"]["device"] = m_device
  28. await async_setup_entry(hass, entry, m_add_entities)
  29. assert type(hass.data[DOMAIN]["dummy"]["humidifier"]) == TuyaLocalHumidifier
  30. m_add_entities.assert_called_once()
  31. async def test_init_entry_fails_if_device_has_no_humidifier(hass):
  32. """Test initialisation when device has no matching entity"""
  33. entry = MockConfigEntry(
  34. domain=DOMAIN,
  35. data={
  36. CONF_TYPE: "kogan_heater",
  37. CONF_DEVICE_ID: "dummy",
  38. },
  39. )
  40. # although async, the async_add_entities function passed to
  41. # async_setup_entry is called truly asynchronously. If we use
  42. # AsyncMock, it expects us to await the result.
  43. m_add_entities = Mock()
  44. m_device = AsyncMock()
  45. hass.data[DOMAIN] = {}
  46. hass.data[DOMAIN]["dummy"] = {}
  47. hass.data[DOMAIN]["dummy"]["device"] = m_device
  48. try:
  49. await async_setup_entry(hass, entry, m_add_entities)
  50. assert False
  51. except ValueError:
  52. pass
  53. m_add_entities.assert_not_called()
  54. async def test_init_entry_fails_if_config_is_missing(hass):
  55. """Test initialisation when device has no matching entity"""
  56. entry = MockConfigEntry(
  57. domain=DOMAIN,
  58. data={
  59. CONF_TYPE: "non_existing",
  60. CONF_DEVICE_ID: "dummy",
  61. },
  62. )
  63. # although async, the async_add_entities function passed to
  64. # async_setup_entry is called truly asynchronously. If we use
  65. # AsyncMock, it expects us to await the result.
  66. m_add_entities = Mock()
  67. m_device = AsyncMock()
  68. hass.data[DOMAIN] = {}
  69. hass.data[DOMAIN]["dummy"] = {}
  70. hass.data[DOMAIN]["dummy"]["device"] = m_device
  71. try:
  72. await async_setup_entry(hass, entry, m_add_entities)
  73. assert False
  74. except ValueError:
  75. pass
  76. m_add_entities.assert_not_called()