test_smartplugv1.py 2.5 KB

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