test_grid_connect_double_power_point.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Tests for the switch entity."""
  2. from homeassistant.components.switch import DEVICE_CLASS_OUTLET
  3. from ..const import GRIDCONNECT_2SOCKET_PAYLOAD
  4. from ..helpers import assert_device_properties_set
  5. from .base_device_tests import TuyaDeviceTestCase
  6. SWITCH1_DPS = "1"
  7. SWITCH2_DPS = "2"
  8. COUNTDOWN1_DPS = "9"
  9. COUNTDOWN2_DPS = "10"
  10. UNKNOWN17_DPS = "17"
  11. CURRENT_DPS = "18"
  12. POWER_DPS = "19"
  13. VOLTAGE_DPS = "20"
  14. UNKNOWN21_DPS = "21"
  15. UNKNOWN22_DPS = "22"
  16. UNKNOWN23_DPS = "23"
  17. UNKNOWN24_DPS = "24"
  18. UNKNOWN25_DPS = "25"
  19. UNKNOWN38_DPS = "38"
  20. LOCK_DPS = "40"
  21. MASTER_DPS = "101"
  22. class TestGridConnectDoubleSwitch(TuyaDeviceTestCase):
  23. __test__ = True
  24. def setUp(self):
  25. self.setUpForConfig(
  26. "grid_connect_usb_double_power_point.yaml",
  27. GRIDCONNECT_2SOCKET_PAYLOAD,
  28. )
  29. self.subject = self.entities.get("switch_master")
  30. self.switch1 = self.entities.get("switch_outlet_1")
  31. self.switch2 = self.entities.get("switch_outlet_2")
  32. self.lock = self.entities.get("lock_child_lock")
  33. def test_device_class_is_outlet(self):
  34. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  35. def test_is_on(self):
  36. self.dps[MASTER_DPS] - True
  37. self.assertTrue(self.subject.is_on)
  38. self.dps[MASTER_DPS] = False
  39. self.assertFalse(self.subject.is_on)
  40. self.assertIsNone(self.switch1.is_on)
  41. self.assertIsNone(self.switch1.is_on)
  42. self.dps[MASTER_DPS] = True
  43. self.dps[SWITCH1_DPS] = True
  44. self.dps[SWITCH2_DPS] = False
  45. self.assertTrue(self.switch1.is_on)
  46. self.assertFalse(self.switch2.is_on)
  47. self.dps[SWITCH1_DPS] = False
  48. self.dps[SWITCH2_DPS] = True
  49. self.assertFalse(self.switch1.is_on)
  50. self.assertTrue(self.switch2.is_on)
  51. def test_is_on_when_unavailable(self):
  52. self.dps[MASTER_DPS] = None
  53. self.assertIsNone(self.subject.is_on)
  54. async def test_turn_on(self):
  55. async with assert_device_properties_set(
  56. self.subject._device, {MASTER_DPS: True}
  57. ):
  58. await self.subject.async_turn_on()
  59. async with assert_device_properties_set(
  60. self.switch1._device, {SWITCH1_DPS: True}
  61. ):
  62. await self.switch1.async_turn_on()
  63. async with assert_device_properties_set(
  64. self.switch1._device, {SWITCH2_DPS: True}
  65. ):
  66. await self.switch2.async_turn_on()
  67. async def test_turn_off(self):
  68. async with assert_device_properties_set(
  69. self.subject._device, {MASTER_DPS: False}
  70. ):
  71. await self.subject.async_turn_off()
  72. async with assert_device_properties_set(
  73. self.switch1._device, {SWITCH1_DPS: False}
  74. ):
  75. await self.switch1.async_turn_off()
  76. async with assert_device_properties_set(
  77. self.switch1._device, {SWITCH2_DPS: False}
  78. ):
  79. await self.switch2.async_turn_off()
  80. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  81. self.dps[MASTER_DPS] = False
  82. self.dps[SWITCH1_DPS] = False
  83. self.dps[SWITCH2_DPS] = False
  84. async with assert_device_properties_set(
  85. self.subject._device, {MASTER_DPS: True}
  86. ):
  87. await self.subject.async_toggle()
  88. self.dps[MASTER_DPS] = True
  89. async with assert_device_properties_set(
  90. self.subject._device, {SWITCH1_DPS: True}
  91. ):
  92. await self.switch1.async_toggle()
  93. async with assert_device_properties_set(
  94. self.subject._device, {SWITCH2_DPS: True}
  95. ):
  96. await self.switch2.async_toggle()
  97. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  98. self.dps[MASTER_DPS] = True
  99. self.dps[SWITCH1_DPS] = True
  100. self.dps[SWITCH2_DPS] = True
  101. async with assert_device_properties_set(
  102. self.subject._device, {SWITCH1_DPS: False}
  103. ):
  104. await self.switch1.async_toggle()
  105. async with assert_device_properties_set(
  106. self.subject._device, {SWITCH2_DPS: False}
  107. ):
  108. await self.switch2.async_toggle()
  109. async with assert_device_properties_set(
  110. self.subject._device, {MASTER_DPS: False}
  111. ):
  112. await self.subject.async_toggle()
  113. async def test_turn_on_fails_when_master_is_off(self):
  114. self.dps[MASTER_DPS] = False
  115. self.dps[SWITCH1_DPS] = False
  116. self.dps[SWITCH2_DPS] = False
  117. with self.assertRaises(AttributeError):
  118. await self.switch1.async_turn_on()
  119. with self.assertRaises(AttributeError):
  120. await self.switch2.async_turn_on()
  121. def test_current_power_w(self):
  122. self.dps[POWER_DPS] = 1234
  123. self.assertEqual(self.subject.current_power_w, 123.4)
  124. def test_device_state_attributes_set(self):
  125. self.dps[COUNTDOWN1_DPS] = 9
  126. self.dps[COUNTDOWN2_DPS] = 10
  127. self.dps[UNKNOWN17_DPS] = 17
  128. self.dps[VOLTAGE_DPS] = 2350
  129. self.dps[CURRENT_DPS] = 1234
  130. self.dps[POWER_DPS] = 5678
  131. self.dps[UNKNOWN21_DPS] = 21
  132. self.dps[UNKNOWN22_DPS] = 22
  133. self.dps[UNKNOWN23_DPS] = 23
  134. self.dps[UNKNOWN24_DPS] = 24
  135. self.dps[UNKNOWN25_DPS] = 25
  136. self.dps[UNKNOWN38_DPS] = "38"
  137. self.assertDictEqual(
  138. self.subject.device_state_attributes,
  139. {
  140. "current_a": 1.234,
  141. "voltage_v": 235.0,
  142. "current_power_w": 567.8,
  143. "unknown_17": 17,
  144. "unknown_21": 21,
  145. "unknown_22": 22,
  146. "unknown_23": 23,
  147. "unknown_24": 24,
  148. "unknown_25": 25,
  149. "unknown_38": "38",
  150. },
  151. )
  152. self.assertDictEqual(
  153. self.switch1.device_state_attributes,
  154. {
  155. "countdown": 9,
  156. },
  157. )
  158. self.assertDictEqual(
  159. self.switch2.device_state_attributes,
  160. {
  161. "countdown": 10,
  162. },
  163. )