test_lock.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  4. from homeassistant.const import STATE_UNAVAILABLE
  5. from custom_components.tuya_local.dehumidifier.const import (
  6. ATTR_CHILD_LOCK,
  7. ATTR_HVAC_MODE,
  8. PROPERTY_TO_DPS_ID,
  9. )
  10. from custom_components.tuya_local.dehumidifier.lock import GoldairDehumidifierChildLock
  11. from ..const import DEHUMIDIFIER_PAYLOAD
  12. from ..helpers import assert_device_properties_set
  13. class TestGoldairDehumidifierChildLock(IsolatedAsyncioTestCase):
  14. def setUp(self):
  15. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  16. self.addCleanup(device_patcher.stop)
  17. self.mock_device = device_patcher.start()
  18. self.subject = GoldairDehumidifierChildLock(self.mock_device())
  19. self.dps = DEHUMIDIFIER_PAYLOAD.copy()
  20. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  21. def test_should_poll(self):
  22. self.assertTrue(self.subject.should_poll)
  23. def test_name_returns_device_name(self):
  24. self.assertEqual(self.subject.name, self.subject._device.name)
  25. def test_unique_id_returns_device_unique_id(self):
  26. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  27. def test_device_info_returns_device_info_from_device(self):
  28. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  29. def test_state(self):
  30. self.dps[PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]] = True
  31. self.assertEqual(self.subject.state, STATE_LOCKED)
  32. self.dps[PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]] = False
  33. self.assertEqual(self.subject.state, STATE_UNLOCKED)
  34. self.dps[PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]] = None
  35. self.assertEqual(self.subject.state, STATE_UNAVAILABLE)
  36. def test_is_locked(self):
  37. self.dps[PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]] = True
  38. self.assertEqual(self.subject.is_locked, True)
  39. self.dps[PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]] = False
  40. self.assertEqual(self.subject.is_locked, False)
  41. async def test_lock(self):
  42. async with assert_device_properties_set(
  43. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]: True}
  44. ):
  45. await self.subject.async_lock()
  46. async def test_unlock(self):
  47. async with assert_device_properties_set(
  48. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_CHILD_LOCK]: False}
  49. ):
  50. await self.subject.async_unlock()
  51. async def test_update(self):
  52. result = AsyncMock()
  53. self.subject._device.async_refresh.return_value = result()
  54. await self.subject.async_update()
  55. self.subject._device.async_refresh.assert_called_once()
  56. result.assert_awaited()