test_lock.py 2.8 KB

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