test_switch.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. """Tests for the switch entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. from unittest import IsolatedAsyncioTestCase
  4. from unittest.mock import AsyncMock, Mock, patch
  5. from homeassistant.components.switch import ATTR_CURRENT_POWER_W, DEVICE_CLASS_OUTLET
  6. from homeassistant.const import STATE_UNAVAILABLE
  7. from custom_components.tuya_local.const import (
  8. CONF_DEVICE_ID,
  9. CONF_SWITCH,
  10. CONF_TYPE,
  11. CONF_TYPE_AUTO,
  12. CONF_TYPE_KOGAN_SWITCH,
  13. DOMAIN,
  14. )
  15. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  16. from custom_components.tuya_local.helpers.device_config import config_for_legacy_use
  17. from custom_components.tuya_local.kogan_socket.switch import KoganSocketSwitch
  18. from custom_components.tuya_local.switch import async_setup_entry
  19. from .const import KOGAN_SOCKET_PAYLOAD
  20. from .helpers import assert_device_properties_set
  21. KOGAN_SWITCH_DPS = "1"
  22. KOGAN_TIMER_DPS = "2"
  23. KOGAN_CURRENT_DPS = "4"
  24. KOGAN_POWER_DPS = "5"
  25. KOGAN_VOLTAGE_DPS = "6"
  26. async def test_init_entry(hass):
  27. """Test the initialisation."""
  28. entry = MockConfigEntry(
  29. domain=DOMAIN,
  30. data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
  31. )
  32. # although async, the async_add_entities function passed to
  33. # async_setup_entry is called truly asynchronously. If we use
  34. # AsyncMock, it expects us to await the result.
  35. m_add_entities = Mock()
  36. m_device = AsyncMock()
  37. m_device.async_inferred_type = AsyncMock(return_value=CONF_TYPE_KOGAN_SWITCH)
  38. hass.data[DOMAIN] = {}
  39. hass.data[DOMAIN]["dummy"] = {}
  40. hass.data[DOMAIN]["dummy"]["device"] = m_device
  41. await async_setup_entry(hass, entry, m_add_entities)
  42. assert type(hass.data[DOMAIN]["dummy"][CONF_SWITCH]) == TuyaLocalSwitch
  43. m_add_entities.assert_called_once()
  44. class TestTuyaLocalSwitch(IsolatedAsyncioTestCase):
  45. def setUp(self):
  46. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  47. self.addCleanup(device_patcher.stop)
  48. self.mock_device = device_patcher.start()
  49. kogan_switch_config = config_for_legacy_use(CONF_TYPE_KOGAN_SWITCH)
  50. switch = kogan_switch_config.primary_entity
  51. self.subject = TuyaLocalSwitch(self.mock_device(), switch)
  52. self.dps = KOGAN_SOCKET_PAYLOAD.copy()
  53. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  54. def test_should_poll(self):
  55. self.assertTrue(self.subject.should_poll)
  56. def test_name_returns_device_name(self):
  57. self.assertEqual(self.subject.name, self.subject._device.name)
  58. def test_unique_id_returns_device_unique_id(self):
  59. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  60. def test_device_class_is_outlet(self):
  61. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  62. def test_is_on(self):
  63. self.dps[KOGAN_SWITCH_DPS] - True
  64. self.assertTrue(self.subject.is_on)
  65. self.dps[KOGAN_SWITCH_DPS] = False
  66. self.assertFalse(self.subject.is_on)
  67. def test_is_on_when_unavailable(self):
  68. self.dps[KOGAN_SWITCH_DPS] = None
  69. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  70. async def test_turn_on(self):
  71. async with assert_device_properties_set(
  72. self.subject._device, {KOGAN_SWITCH_DPS: True}
  73. ):
  74. await self.subject.async_turn_on()
  75. async def test_turn_off(self):
  76. async with assert_device_properties_set(
  77. self.subject._device, {KOGAN_SWITCH_DPS: False}
  78. ):
  79. await self.subject.async_turn_off()
  80. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  81. self.dps[KOGAN_SWITCH_DPS] = False
  82. async with assert_device_properties_set(
  83. self.subject._device, {KOGAN_SWITCH_DPS: True}
  84. ):
  85. await self.subject.async_toggle()
  86. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  87. self.dps[KOGAN_SWITCH_DPS] = True
  88. async with assert_device_properties_set(
  89. self.subject._device, {KOGAN_SWITCH_DPS: False}
  90. ):
  91. await self.subject.async_toggle()
  92. def test_current_power_w(self):
  93. self.dps[KOGAN_POWER_DPS] = 1234
  94. self.assertEqual(self.subject.current_power_w, 123.4)
  95. def test_device_state_attributes_set(self):
  96. self.dps[KOGAN_TIMER_DPS] = 1
  97. self.dps[KOGAN_VOLTAGE_DPS] = 2350
  98. self.dps[KOGAN_CURRENT_DPS] = 1234
  99. self.dps[KOGAN_POWER_DPS] = 5678
  100. self.assertEqual(
  101. self.subject.device_state_attributes,
  102. {
  103. "timer": 1,
  104. "current_a": 1.234,
  105. "voltage_v": 235.0,
  106. "current_power_w": 567.8,
  107. },
  108. )
  109. self.dps[KOGAN_TIMER_DPS] = 0
  110. self.dps[KOGAN_CURRENT_DPS] = None
  111. self.dps[KOGAN_VOLTAGE_DPS] = None
  112. self.dps[KOGAN_POWER_DPS] = None
  113. self.assertEqual(
  114. self.subject.device_state_attributes,
  115. {
  116. "timer": 0,
  117. "current_a": None,
  118. "voltage_v": None,
  119. "current_power_w": None,
  120. },
  121. )
  122. async def test_update(self):
  123. result = AsyncMock()
  124. self.subject._device.async_refresh.return_value = result()
  125. await self.subject.async_update()
  126. self.subject._device.async_refresh.assert_called_once()
  127. result.assert_awaited()