test_lock.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """Tests for the lock entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. from unittest import IsolatedAsyncioTestCase
  4. from unittest.mock import AsyncMock, Mock, patch
  5. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  6. from homeassistant.const import STATE_UNAVAILABLE
  7. from custom_components.tuya_local.const import (
  8. CONF_CHILD_LOCK,
  9. CONF_DEVICE_ID,
  10. CONF_TYPE,
  11. CONF_TYPE_AUTO,
  12. CONF_TYPE_GPPH_HEATER,
  13. DOMAIN,
  14. )
  15. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  16. from custom_components.tuya_local.helpers.device_config import config_for_legacy_use
  17. from custom_components.tuya_local.lock import async_setup_entry
  18. from .const import GPPH_HEATER_PAYLOAD
  19. from .helpers import assert_device_properties_set
  20. GPPH_LOCK_DPS = "6"
  21. async def test_init_entry(hass):
  22. """Test the initialisation."""
  23. entry = MockConfigEntry(
  24. domain=DOMAIN,
  25. data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
  26. )
  27. # although async, the async_add_entities function passed to
  28. # async_setup_entry is called truly asynchronously. If we use
  29. # AsyncMock, it expects us to await the result.
  30. m_add_entities = Mock()
  31. m_device = AsyncMock()
  32. m_device.async_inferred_type = AsyncMock(return_value=CONF_TYPE_GPPH_HEATER)
  33. hass.data[DOMAIN] = {}
  34. hass.data[DOMAIN]["dummy"] = {}
  35. hass.data[DOMAIN]["dummy"]["device"] = m_device
  36. await async_setup_entry(hass, entry, m_add_entities)
  37. assert type(hass.data[DOMAIN]["dummy"][CONF_CHILD_LOCK]) == TuyaLocalLock
  38. m_add_entities.assert_called_once()
  39. class TestTuyaLocalLock(IsolatedAsyncioTestCase):
  40. def setUp(self):
  41. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  42. self.addCleanup(device_patcher.stop)
  43. self.mock_device = device_patcher.start()
  44. gpph_config = config_for_legacy_use(CONF_TYPE_GPPH_HEATER)
  45. for lock in gpph_config.secondary_entities():
  46. if lock.entity == "lock":
  47. break
  48. self.subject = TuyaLocalLock(self.mock_device(), lock)
  49. self.dps = GPPH_HEATER_PAYLOAD.copy()
  50. self.lock_name = lock.name
  51. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  52. def test_should_poll(self):
  53. self.assertTrue(self.subject.should_poll)
  54. def test_name_returns_device_name(self):
  55. self.assertEqual(self.subject.name, self.subject._device.name)
  56. def test_friendly_name_returns_config_name(self):
  57. self.assertEqual(self.subject.friendly_name, self.lock_name)
  58. def test_unique_id_returns_device_unique_id(self):
  59. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  60. def test_device_info_returns_device_info_from_device(self):
  61. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  62. def test_state(self):
  63. self.dps[GPPH_LOCK_DPS] = True
  64. self.assertEqual(self.subject.state, STATE_LOCKED)
  65. self.dps[GPPH_LOCK_DPS] = False
  66. self.assertEqual(self.subject.state, STATE_UNLOCKED)
  67. self.dps[GPPH_LOCK_DPS] = None
  68. self.assertEqual(self.subject.state, STATE_UNAVAILABLE)
  69. def test_state_attributes(self):
  70. self.assertEqual(self.subject.device_state_attributes, {})
  71. def test_is_locked(self):
  72. self.dps[GPPH_LOCK_DPS] = True
  73. self.assertTrue(self.subject.is_locked)
  74. self.dps[GPPH_LOCK_DPS] = False
  75. self.assertFalse(self.subject.is_locked)
  76. async def test_lock(self):
  77. async with assert_device_properties_set(
  78. self.subject._device, {GPPH_LOCK_DPS: True}
  79. ):
  80. await self.subject.async_lock()
  81. async def test_unlock(self):
  82. async with assert_device_properties_set(
  83. self.subject._device, {GPPH_LOCK_DPS: False}
  84. ):
  85. await self.subject.async_unlock()
  86. async def test_update(self):
  87. result = AsyncMock()
  88. self.subject._device.async_refresh.return_value = result()
  89. await self.subject.async_update()
  90. self.subject._device.async_refresh.assert_called_once()
  91. result.assert_awaited()