test_lock.py 2.8 KB

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