test_alt_switch.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, KOGAN_SOCKET_CLEAR_PAYLOAD
  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. # since the socket needs to handle both types, give the mock some
  26. # dummy fields to prevent breakage.
  27. self.dps = KOGAN_SOCKET_CLEAR_PAYLOAD.copy()
  28. self.dps.update(KOGAN_SOCKET_PAYLOAD2)
  29. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  30. def test_should_poll(self):
  31. self.assertTrue(self.subject.should_poll)
  32. def test_name_returns_device_name(self):
  33. self.assertEqual(self.subject.name, self.subject._device.name)
  34. def test_unique_id_returns_device_unique_id(self):
  35. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  36. def test_device_info_returns_device_info_from_device(self):
  37. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  38. def test_device_class_is_outlet(self):
  39. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  40. def test_is_on(self):
  41. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  42. self.assertEqual(self.subject.is_on, True)
  43. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  44. self.assertEqual(self.subject.is_on, False)
  45. def test_is_on_when_unavailable(self):
  46. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = None
  47. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  48. async def test_turn_on(self):
  49. async with assert_device_properties_set(
  50. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  51. ):
  52. await self.subject.async_turn_on()
  53. async def test_turn_off(self):
  54. async with assert_device_properties_set(
  55. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  56. ):
  57. await self.subject.async_turn_off()
  58. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  59. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  60. async with assert_device_properties_set(
  61. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  62. ):
  63. await self.subject.async_toggle()
  64. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  65. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  66. async with assert_device_properties_set(
  67. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  68. ):
  69. await self.subject.async_toggle()
  70. def test_current_power_w(self):
  71. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = 1234
  72. self.assertEqual(self.subject.current_power_w, 123.4)
  73. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = None
  74. self.assertEqual(self.subject.current_power_w, STATE_UNAVAILABLE)
  75. def test_device_state_attributes_set(self):
  76. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_TIMER]] = 1
  77. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V]] = 2350
  78. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A]] = 1234
  79. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = 5678
  80. self.assertEqual(
  81. self.subject.device_state_attributes,
  82. {
  83. ATTR_TIMER: 1,
  84. ATTR_CURRENT_A: 1.234,
  85. ATTR_VOLTAGE_V: 235.0,
  86. ATTR_CURRENT_POWER_W: 567.8,
  87. },
  88. )
  89. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_TIMER]] = 0
  90. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_VOLTAGE_V]] = None
  91. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_A]] = None
  92. self.dps[PROPERTY_TO_DPS_ID[ATTR_ALT_CURRENT_POWER_W]] = None
  93. self.assertEqual(
  94. self.subject.device_state_attributes,
  95. {
  96. ATTR_TIMER: 0,
  97. ATTR_CURRENT_A: None,
  98. ATTR_VOLTAGE_V: None,
  99. ATTR_CURRENT_POWER_W: STATE_UNAVAILABLE,
  100. },
  101. )
  102. async def test_update(self):
  103. result = AsyncMock()
  104. self.subject._device.async_refresh.return_value = result()
  105. await self.subject.async_update()
  106. self.subject._device.async_refresh.assert_called_once()
  107. result.assert_awaited()