test_deta_fan.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from homeassistant.components.fan import SUPPORT_SET_SPEED
  2. from homeassistant.components.light import COLOR_MODE_ONOFF
  3. from homeassistant.const import STATE_UNAVAILABLE
  4. from ..const import DETA_FAN_PAYLOAD
  5. from ..helpers import assert_device_properties_set
  6. from .base_device_tests import TuyaDeviceTestCase
  7. SWITCH_DPS = "1"
  8. SPEED_DPS = "3"
  9. LIGHT_DPS = "9"
  10. MASTER_DPS = "101"
  11. TIMER_DPS = "102"
  12. LIGHT_TIMER_DPS = "103"
  13. class TestDetaFan(TuyaDeviceTestCase):
  14. __test__ = True
  15. def setUp(self):
  16. self.setUpForConfig("deta_fan.yaml", DETA_FAN_PAYLOAD)
  17. self.subject = self.entities["fan"]
  18. self.light = self.entities["light"]
  19. self.switch = self.entities["switch"]
  20. def test_supported_features(self):
  21. self.assertEqual(
  22. self.subject.supported_features,
  23. SUPPORT_SET_SPEED,
  24. )
  25. def test_is_on(self):
  26. self.dps[SWITCH_DPS] = True
  27. self.assertTrue(self.subject.is_on)
  28. self.dps[SWITCH_DPS] = False
  29. self.assertFalse(self.subject.is_on)
  30. self.dps[SWITCH_DPS] = None
  31. self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
  32. async def test_turn_on(self):
  33. async with assert_device_properties_set(
  34. self.subject._device, {SWITCH_DPS: True}
  35. ):
  36. await self.subject.async_turn_on()
  37. async def test_turn_off(self):
  38. async with assert_device_properties_set(
  39. self.subject._device, {SWITCH_DPS: False}
  40. ):
  41. await self.subject.async_turn_off()
  42. def test_speed(self):
  43. self.dps[SPEED_DPS] = "1"
  44. self.assertAlmostEqual(self.subject.percentage, 33.3, 1)
  45. def test_speed_step(self):
  46. self.assertAlmostEqual(self.subject.percentage_step, 33.3, 1)
  47. async def test_set_speed(self):
  48. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 2}):
  49. await self.subject.async_set_percentage(66.7)
  50. async def test_auto_stringify_speed(self):
  51. self.dps[SPEED_DPS] = "1"
  52. self.assertAlmostEqual(self.subject.percentage, 33.3, 1)
  53. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: "2"}):
  54. await self.subject.async_set_percentage(66.7)
  55. async def test_set_speed_snaps(self):
  56. async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 2}):
  57. await self.subject.async_set_percentage(55)
  58. def test_device_state_attributes(self):
  59. self.dps[TIMER_DPS] = "5"
  60. self.dps[LIGHT_TIMER_DPS] = "6"
  61. self.assertEqual(self.subject.device_state_attributes, {"timer": 5})
  62. self.assertEqual(self.light.device_state_attributes, {"timer": 6})
  63. def test_light_supported_color_modes(self):
  64. self.assertCountEqual(
  65. self.light.supported_color_modes,
  66. [COLOR_MODE_ONOFF],
  67. )
  68. def test_light_color_mode(self):
  69. self.assertEqual(self.light.color_mode, COLOR_MODE_ONOFF)
  70. def test_light_is_on(self):
  71. self.dps[LIGHT_DPS] = True
  72. self.assertTrue(self.light.is_on)
  73. self.dps[LIGHT_DPS] = False
  74. self.assertFalse(self.light.is_on)
  75. async def test_light_turn_on(self):
  76. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: True}):
  77. await self.light.async_turn_on()
  78. async def test_light_turn_off(self):
  79. async with assert_device_properties_set(self.light._device, {LIGHT_DPS: False}):
  80. await self.light.async_turn_off()
  81. def test_switch_is_on(self):
  82. self.dps[MASTER_DPS] = True
  83. self.assertTrue(self.switch.is_on)
  84. self.dps[MASTER_DPS] = False
  85. self.assertFalse(self.switch.is_on)
  86. self.dps[MASTER_DPS] = None
  87. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  88. async def test_switch_turn_on(self):
  89. async with assert_device_properties_set(
  90. self.switch._device, {MASTER_DPS: True}
  91. ):
  92. await self.switch.async_turn_on()
  93. async def test_switch_turn_off(self):
  94. async with assert_device_properties_set(
  95. self.light._device, {MASTER_DPS: False}
  96. ):
  97. await self.switch.async_turn_off()