test_alt_switch.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.switch import ATTR_CURRENT_POWER_W, DEVICE_CLASS_OUTLET
  4. from homeassistant.const import STATE_UNAVAILABLE
  5. from custom_components.tuya_local.kogan_socket.const import (
  6. ATTR_SWITCH,
  7. ATTR_TIMER,
  8. ATTR_ALT_TIMER,
  9. ATTR_ALT_CURRENT_A,
  10. ATTR_ALT_CURRENT_POWER_W,
  11. ATTR_ALT_VOLTAGE_V,
  12. ATTR_CURRENT_A,
  13. ATTR_VOLTAGE_V,
  14. PROPERTY_TO_DPS_ID,
  15. )
  16. from custom_components.tuya_local.kogan_socket.switch import KoganSocketSwitch
  17. from ..const import KOGAN_SOCKET_PAYLOAD2
  18. from ..helpers import assert_device_properties_set
  19. class TestKoganSocket(IsolatedAsyncioTestCase):
  20. def setUp(self):
  21. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  22. self.addCleanup(device_patcher.stop)
  23. self.mock_device = device_patcher.start()
  24. self.subject = KoganSocketSwitch(self.mock_device())
  25. self.dps = KOGAN_SOCKET_PAYLOAD2.copy()
  26. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  27. def test_should_poll(self):
  28. self.assertTrue(self.subject.should_poll)
  29. def test_name_returns_device_name(self):
  30. self.assertEqual(self.subject.name, self.subject._device.name)
  31. def test_unique_id_returns_device_unique_id(self):
  32. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  33. def test_device_info_returns_device_info_from_device(self):
  34. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  35. def test_device_class_is_outlet(self):
  36. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  37. def test_is_on(self):
  38. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  39. self.assertEqual(self.subject.is_on, True)
  40. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  41. self.assertEqual(self.subject.is_on, False)
  42. def test_is_on_when_unavailable(self):
  43. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = None
  44. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  45. async def test_turn_on(self):
  46. async with assert_device_properties_set(
  47. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  48. ):
  49. await self.subject.async_turn_on()
  50. async def test_turn_off(self):
  51. async with assert_device_properties_set(
  52. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  53. ):
  54. await self.subject.async_turn_off()
  55. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  56. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  57. async with assert_device_properties_set(
  58. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  59. ):
  60. await self.subject.async_toggle()
  61. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  62. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  63. async with assert_device_properties_set(
  64. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  65. ):
  66. await self.subject.async_toggle()
  67. def test_current_power_w(self):
  68. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = 1234
  69. self.assertEqual(self.subject.current_power_w, 123.4)
  70. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = None
  71. self.assertEqual(self.subject.current_power_w, STATE_UNAVAILABLE)
  72. def test_device_state_attributes_set(self):
  73. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_TIMER]] = 1
  74. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V]] = 2350
  75. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A]] = 1234
  76. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = 5678
  77. self.assertEqual(
  78. self.subject.device_state_attributes,
  79. {
  80. ATTR_TIMER: 1,
  81. ATTR_CURRENT_A: 1.234,
  82. ATTR_VOLTAGE_V: 235.0,
  83. ATTR_CURRENT_POWER_W: 567.8,
  84. },
  85. )
  86. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_TIMER]] = 0
  87. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V]] = None
  88. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A]] = None
  89. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = None
  90. self.assertEqual(
  91. self.subject.device_state_attributes,
  92. {
  93. ATTR_TIMER: 0,
  94. ATTR_CURRENT_A: None,
  95. ATTR_VOLTAGE_V: None,
  96. ATTR_CURRENT_POWER_W: STATE_UNAVAILABLE,
  97. },
  98. )
  99. async def test_update(self):
  100. result = AsyncMock()
  101. self.subject._device.async_refresh.return_value = result()
  102. await self.subject.async_update()
  103. self.subject._device.async_refresh.assert_called_once()
  104. result.assert_awaited()