test_switch.py 5.7 KB

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