test_switch.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  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. self.dps = KOGAN_SOCKET_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_device_class_is_outlet(self):
  32. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  33. def test_is_on(self):
  34. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  35. self.assertEqual(self.subject.is_on, True)
  36. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  37. self.assertEqual(self.subject.is_on, False)
  38. def test_is_on_when_unavailable(self):
  39. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = None
  40. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  41. async def test_turn_on(self):
  42. async with assert_device_properties_set(
  43. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  44. ):
  45. await self.subject.async_turn_on()
  46. async def test_turn_off(self):
  47. async with assert_device_properties_set(
  48. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  49. ):
  50. await self.subject.async_turn_off()
  51. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  52. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = False
  53. async with assert_device_properties_set(
  54. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: True}
  55. ):
  56. await self.subject.async_toggle()
  57. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  58. self.dps[PROPERTY_TO_DPS_ID[ATTR_SWITCH]] = True
  59. async with assert_device_properties_set(
  60. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_SWITCH]: False}
  61. ):
  62. await self.subject.async_toggle()
  63. def test_current_power_w(self):
  64. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]] = 1234
  65. self.assertEqual(self.subject.current_power_w, 123.4)
  66. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]] = None
  67. self.assertEqual(self.subject.current_power_w, STATE_UNAVAILABLE)
  68. def test_device_state_attributes_set(self):
  69. self.dps[PROPERTY_TO_DPS_ID[ATTR_TIMER]] = 1
  70. self.dps[PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V]] = 2350
  71. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_A]] = 1234
  72. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]] = 5678
  73. self.assertEqual(
  74. self.subject.device_state_attributes,
  75. {
  76. ATTR_TIMER: 1,
  77. ATTR_CURRENT_A: 1.234,
  78. ATTR_VOLTAGE_V: 235.0,
  79. ATTR_CURRENT_POWER_W: 567.8,
  80. },
  81. )
  82. self.dps[PROPERTY_TO_DPS_ID[ATTR_TIMER]] = 0
  83. self.dps[PROPERTY_TO_DPS_ID[ATTR_VOLTAGE_V]] = None
  84. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_A]] = None
  85. self.dps[PROPERTY_TO_DPS_ID[ATTR_CURRENT_POWER_W]] = None
  86. self.assertEqual(
  87. self.subject.device_state_attributes,
  88. {
  89. ATTR_TIMER: 0,
  90. ATTR_CURRENT_A: None,
  91. ATTR_VOLTAGE_V: None,
  92. ATTR_CURRENT_POWER_W: STATE_UNAVAILABLE,
  93. },
  94. )
  95. async def test_update(self):
  96. result = AsyncMock()
  97. self.subject._device.async_refresh.return_value = result()
  98. await self.subject.async_update()
  99. self.subject._device.async_refresh.assert_called_once()
  100. result.assert_awaited()