test_smartplugv2.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 KOGAN_SOCKET_PAYLOAD2
  5. from ..helpers import assert_device_properties_set
  6. from .base_device_tests import TuyaDeviceTestCase
  7. SWITCH_DPS = "1"
  8. TIMER_DPS = "9"
  9. CURRENT_DPS = "18"
  10. POWER_DPS = "19"
  11. VOLTAGE_DPS = "20"
  12. class TestSwitchV2(TuyaDeviceTestCase):
  13. __test__ = True
  14. def setUp(self):
  15. self.setUpForConfig("smartplugv2.yaml", KOGAN_SOCKET_PAYLOAD2)
  16. self.subject = self.entities.get("switch")
  17. def test_device_class_is_outlet(self):
  18. self.assertEqual(self.subject.device_class, DEVICE_CLASS_OUTLET)
  19. def test_is_on(self):
  20. self.dps[SWITCH_DPS] - True
  21. self.assertTrue(self.subject.is_on)
  22. self.dps[SWITCH_DPS] = False
  23. self.assertFalse(self.subject.is_on)
  24. def test_is_on_when_unavailable(self):
  25. self.dps[SWITCH_DPS] = None
  26. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  27. async def test_turn_on(self):
  28. async with assert_device_properties_set(
  29. self.subject._device, {SWITCH_DPS: True}
  30. ):
  31. await self.subject.async_turn_on()
  32. async def test_turn_off(self):
  33. async with assert_device_properties_set(
  34. self.subject._device, {SWITCH_DPS: False}
  35. ):
  36. await self.subject.async_turn_off()
  37. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  38. self.dps[SWITCH_DPS] = False
  39. async with assert_device_properties_set(
  40. self.subject._device, {SWITCH_DPS: True}
  41. ):
  42. await self.subject.async_toggle()
  43. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  44. self.dps[SWITCH_DPS] = True
  45. async with assert_device_properties_set(
  46. self.subject._device, {SWITCH_DPS: False}
  47. ):
  48. await self.subject.async_toggle()
  49. def test_current_power_w(self):
  50. self.dps[POWER_DPS] = 1234
  51. self.assertEqual(self.subject.current_power_w, 123.4)
  52. def test_device_state_attributes_set(self):
  53. self.dps[TIMER_DPS] = 1
  54. self.dps[VOLTAGE_DPS] = 2350
  55. self.dps[CURRENT_DPS] = 1234
  56. self.dps[POWER_DPS] = 5678
  57. self.assertDictEqual(
  58. self.subject.device_state_attributes,
  59. {
  60. "timer": 1,
  61. "current_a": 1.234,
  62. "voltage_v": 235.0,
  63. "current_power_w": 567.8,
  64. },
  65. )