test_kogan_switch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """Tests for the switch entity."""
  2. from unittest import IsolatedAsyncioTestCase
  3. from unittest.mock import AsyncMock, patch
  4. from homeassistant.components.switch import DEVICE_CLASS_OUTLET
  5. from homeassistant.const import STATE_UNAVAILABLE
  6. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  7. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  8. from ..const import KOGAN_SOCKET_PAYLOAD
  9. from ..helpers import assert_device_properties_set
  10. SWITCH_DPS = "1"
  11. TIMER_DPS = "2"
  12. CURRENT_DPS = "4"
  13. POWER_DPS = "5"
  14. VOLTAGE_DPS = "6"
  15. class TestKoganSwitch(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. cfg = TuyaDeviceConfig("kogan_switch.yaml")
  21. switch = cfg.primary_entity
  22. self.switch_name = switch.name
  23. self.subject = TuyaLocalSwitch(self.mock_device(), switch)
  24. self.dps = KOGAN_SOCKET_PAYLOAD.copy()
  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_friendly_name_returns_config_name(self):
  31. self.assertEqual(self.subject.friendly_name, self.switch_name)
  32. def test_unique_id_returns_device_unique_id(self):
  33. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  34. def test_device_info_returns_device_info_from_device(self):
  35. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  36. def test_device_class_is_outlet(self):
  37. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  38. def test_is_on(self):
  39. self.dps[SWITCH_DPS] - True
  40. self.assertTrue(self.subject.is_on)
  41. self.dps[SWITCH_DPS] = False
  42. self.assertFalse(self.subject.is_on)
  43. def test_is_on_when_unavailable(self):
  44. self.dps[SWITCH_DPS] = None
  45. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  46. async def test_turn_on(self):
  47. async with assert_device_properties_set(
  48. self.subject._device, {SWITCH_DPS: True}
  49. ):
  50. await self.subject.async_turn_on()
  51. async def test_turn_off(self):
  52. async with assert_device_properties_set(
  53. self.subject._device, {SWITCH_DPS: False}
  54. ):
  55. await self.subject.async_turn_off()
  56. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  57. self.dps[SWITCH_DPS] = False
  58. async with assert_device_properties_set(
  59. self.subject._device, {SWITCH_DPS: True}
  60. ):
  61. await self.subject.async_toggle()
  62. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  63. self.dps[SWITCH_DPS] = True
  64. async with assert_device_properties_set(
  65. self.subject._device, {SWITCH_DPS: False}
  66. ):
  67. await self.subject.async_toggle()
  68. def test_current_power_w(self):
  69. self.dps[POWER_DPS] = 1234
  70. self.assertEqual(self.subject.current_power_w, 123.4)
  71. def test_device_state_attributes_set(self):
  72. self.dps[TIMER_DPS] = 1
  73. self.dps[VOLTAGE_DPS] = 2350
  74. self.dps[CURRENT_DPS] = 1234
  75. self.dps[POWER_DPS] = 5678
  76. self.assertCountEqual(
  77. self.subject.device_state_attributes,
  78. {
  79. "timer": 1,
  80. "current_a": 1.234,
  81. "voltage_v": 235.0,
  82. "current_power_w": 567.8,
  83. },
  84. )
  85. self.dps[TIMER_DPS] = 0
  86. self.dps[CURRENT_DPS] = None
  87. self.dps[VOLTAGE_DPS] = None
  88. self.dps[POWER_DPS] = None
  89. self.assertCountEqual(
  90. self.subject.device_state_attributes,
  91. {
  92. "timer": 0,
  93. "current_a": None,
  94. "voltage_v": None,
  95. "current_power_w": None,
  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()