test_smartplugv1.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Tests for the switch entity."""
  2. from homeassistant.components.switch import SwitchDeviceClass
  3. from homeassistant.components.sensor import SensorDeviceClass
  4. from homeassistant.const import (
  5. UnitOfElectricCurrent,
  6. UnitOfElectricPotential,
  7. UnitOfTime,
  8. UnitOfPower,
  9. )
  10. from ..const import KOGAN_SOCKET_PAYLOAD
  11. from ..mixins.number import BasicNumberTests
  12. from ..mixins.sensor import MultiSensorTests
  13. from ..mixins.switch import SwitchableTests
  14. from .base_device_tests import TuyaDeviceTestCase
  15. SWITCH_DPS = "1"
  16. TIMER_DPS = "2"
  17. CURRENT_DPS = "4"
  18. POWER_DPS = "5"
  19. VOLTAGE_DPS = "6"
  20. class TestKoganSwitch(
  21. BasicNumberTests, MultiSensorTests, SwitchableTests, TuyaDeviceTestCase
  22. ):
  23. __test__ = True
  24. def setUp(self):
  25. self.setUpForConfig("smartplugv1.yaml", KOGAN_SOCKET_PAYLOAD)
  26. self.subject = self.entities.get("switch")
  27. self.setUpSwitchable(SWITCH_DPS, self.subject)
  28. self.setUpBasicNumber(
  29. TIMER_DPS,
  30. self.entities.get("number_timer"),
  31. max=1440.0,
  32. unit=UnitOfTime.MINUTES,
  33. scale=60,
  34. )
  35. self.setUpMultiSensors(
  36. [
  37. {
  38. "name": "sensor_voltage",
  39. "dps": VOLTAGE_DPS,
  40. "unit": UnitOfElectricPotential.VOLT,
  41. "device_class": SensorDeviceClass.VOLTAGE,
  42. "state_class": "measurement",
  43. "testdata": (2300, 230.0),
  44. },
  45. {
  46. "name": "sensor_current",
  47. "dps": CURRENT_DPS,
  48. "unit": UnitOfElectricCurrent.MILLIAMPERE,
  49. "device_class": SensorDeviceClass.CURRENT,
  50. "state_class": "measurement",
  51. },
  52. {
  53. "name": "sensor_power",
  54. "dps": POWER_DPS,
  55. "unit": UnitOfPower.WATT,
  56. "device_class": SensorDeviceClass.POWER,
  57. "state_class": "measurement",
  58. "testdata": (1234, 123.4),
  59. },
  60. ]
  61. )
  62. self.mark_secondary(
  63. [
  64. "number_timer",
  65. "sensor_current",
  66. "sensor_power",
  67. "sensor_voltage",
  68. ]
  69. )
  70. def test_device_class_is_outlet(self):
  71. self.assertEqual(self.subject.device_class, SwitchDeviceClass.OUTLET)
  72. def test_current_power_w(self):
  73. self.dps[POWER_DPS] = 1234
  74. self.assertEqual(self.subject.current_power_w, 123.4)
  75. def test_extra_state_attributes_set(self):
  76. self.dps[TIMER_DPS] = 1
  77. self.dps[VOLTAGE_DPS] = 2350
  78. self.dps[CURRENT_DPS] = 1234
  79. self.dps[POWER_DPS] = 5678
  80. self.assertDictEqual(
  81. self.subject.extra_state_attributes,
  82. {
  83. "timer": 1,
  84. "current_a": 1.234,
  85. "voltage_v": 235.0,
  86. "current_power_w": 567.8,
  87. },
  88. )