test_lawn_mower.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Tests for the lawn_mower entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from pytest_homeassistant_custom_component.common import MockConfigEntry
  5. from custom_components.tuya_local.const import (
  6. CONF_DEVICE_ID,
  7. CONF_PROTOCOL_VERSION,
  8. CONF_TYPE,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.lawn_mower import (
  12. TuyaLocalLawnMower,
  13. async_setup_entry,
  14. )
  15. @pytest.mark.asyncio
  16. async def test_init_entry(hass):
  17. """Test the initialisation."""
  18. entry = MockConfigEntry(
  19. domain=DOMAIN,
  20. data={
  21. CONF_TYPE: "moebot_s_mower",
  22. CONF_DEVICE_ID: "dummy",
  23. CONF_PROTOCOL_VERSION: "auto",
  24. },
  25. )
  26. m_add_entities = Mock()
  27. m_device = AsyncMock()
  28. hass.data[DOMAIN] = {
  29. "dummy": {
  30. "device": m_device,
  31. },
  32. }
  33. await async_setup_entry(hass, entry, m_add_entities)
  34. assert type(hass.data[DOMAIN]["dummy"]["lawn_mower"]) is TuyaLocalLawnMower
  35. m_add_entities.assert_called_once()
  36. @pytest.mark.asyncio
  37. async def test_init_entry_fails_if_device_has_no_lawn_mower(hass):
  38. """Test initialisation when device has no matching entity"""
  39. entry = MockConfigEntry(
  40. domain=DOMAIN,
  41. data={
  42. CONF_TYPE: "kogan_heater",
  43. CONF_DEVICE_ID: "dummy",
  44. CONF_PROTOCOL_VERSION: "auto",
  45. },
  46. )
  47. m_add_entities = Mock()
  48. m_device = AsyncMock()
  49. hass.data[DOMAIN] = {
  50. "dummy": {
  51. "device": m_device,
  52. },
  53. }
  54. try:
  55. await async_setup_entry(hass, entry, m_add_entities)
  56. assert False
  57. except ValueError:
  58. pass
  59. m_add_entities.assert_not_called()
  60. @pytest.mark.asyncio
  61. async def test_init_entry_fails_if_config_is_missing(hass):
  62. """Test initialisation when device has no matching entity"""
  63. entry = MockConfigEntry(
  64. domain=DOMAIN,
  65. data={
  66. CONF_TYPE: "non_existing",
  67. CONF_DEVICE_ID: "dummy",
  68. CONF_PROTOCOL_VERSION: "auto",
  69. },
  70. )
  71. # although async, the async_add_entities function passed to
  72. # async_setup_entry is called truly asynchronously. If we use
  73. # AsyncMock, it expects us to await the result.
  74. m_add_entities = Mock()
  75. m_device = AsyncMock()
  76. hass.data[DOMAIN] = {}
  77. hass.data[DOMAIN]["dummy"] = {}
  78. hass.data[DOMAIN]["dummy"]["device"] = m_device
  79. try:
  80. await async_setup_entry(hass, entry, m_add_entities)
  81. assert False
  82. except ValueError:
  83. pass
  84. m_add_entities.assert_not_called()