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