test_water_heater.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Tests for the water heater 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.water_heater import TuyaLocalWaterHeater
  10. from custom_components.tuya_local.water_heater 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: "hydrotherm_dynamic_x8_water_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"]["water_heater"]) == TuyaLocalWaterHeater
  27. m_add_entities.assert_called_once()
  28. async def test_init_entry_fails_if_device_has_no_water_heater(hass):
  29. """Test initialisation when device has no matching entity"""
  30. entry = MockConfigEntry(
  31. domain=DOMAIN,
  32. data={CONF_TYPE: "kogan_switch", CONF_DEVICE_ID: "dummy"},
  33. )
  34. # although async, the async_add_entities function passed to
  35. # async_setup_entry is called truly asynchronously. If we use
  36. # AsyncMock, it expects us to await the result.
  37. m_add_entities = Mock()
  38. m_device = AsyncMock()
  39. hass.data[DOMAIN] = {}
  40. hass.data[DOMAIN]["dummy"] = {}
  41. hass.data[DOMAIN]["dummy"]["device"] = m_device
  42. try:
  43. await async_setup_entry(hass, entry, m_add_entities)
  44. assert False
  45. except ValueError:
  46. pass
  47. m_add_entities.assert_not_called()
  48. async def test_init_entry_fails_if_config_is_missing(hass):
  49. """Test initialisation when device has no matching entity"""
  50. entry = MockConfigEntry(
  51. domain=DOMAIN,
  52. data={CONF_TYPE: "non_existing", CONF_DEVICE_ID: "dummy"},
  53. )
  54. # although async, the async_add_entities function passed to
  55. # async_setup_entry is called truly asynchronously. If we use
  56. # AsyncMock, it expects us to await the result.
  57. m_add_entities = Mock()
  58. m_device = AsyncMock()
  59. hass.data[DOMAIN] = {}
  60. hass.data[DOMAIN]["dummy"] = {}
  61. hass.data[DOMAIN]["dummy"]["device"] = m_device
  62. try:
  63. await async_setup_entry(hass, entry, m_add_entities)
  64. assert False
  65. except ValueError:
  66. pass
  67. m_add_entities.assert_not_called()