test_grid_connect_double_power_point.py 6.3 KB

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