test_deta_fan.py 3.8 KB

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