Просмотр исходного кода

Implement unit tests for arlec fan.

- add scale back into speed, but set appropriately for 6 steps.
Jason Rumney 4 лет назад
Родитель
Сommit
a0ae341b8d
3 измененных файлов с 159 добавлено и 3 удалено
  1. 5 3
      custom_components/tuya_local/devices/arlec_fan.yaml
  2. 23 0
      tests/const.py
  3. 131 0
      tests/devices/test_arlec_fan.py

+ 5 - 3
custom_components/tuya_local/devices/arlec_fan.yaml

@@ -5,15 +5,17 @@ primary_entity:
     - id: 1
       type: boolean
       name: switch
-    - id: 4
-      type: string
-      name: direction
     - id: 3
       type: integer
       name: speed
       range:
         min: 1
         max: 6
+      mapping:
+        - scale: 0.06
+    - id: 4
+      type: string
+      name: direction
     - id: 102
       type: string
       name: preset_mode

+ 23 - 0
tests/const.py

@@ -414,3 +414,26 @@ RENPHO_PURIFIER_PAYLOAD = {
     "104": 0,
     "105": 0,
 }
+
+ARLEC_FAN_PAYLOAD = {
+    "1": True,
+    "3": 1,
+    "4": "forward",
+    "102": "normal",
+    "103": 0,
+}
+
+CARSON_CB_PAYOAD = {
+    "1": True,
+    "2": 20,
+    "3": 23,
+    "4": "COOL",
+    "5": 1,
+    "19": "C",
+    "102": False,
+    "103": 0,
+    "104": False,
+    "105": 0,
+    "106": 0,
+    "110": 0,
+}

+ 131 - 0
tests/devices/test_arlec_fan.py

@@ -0,0 +1,131 @@
+from homeassistant.components.fan import (
+    DIRECTION_FORWARD,
+    DIRECTION_REVERSE,
+    SUPPORT_DIRECTION,
+    SUPPORT_PRESET_MODE,
+    SUPPORT_SET_SPEED,
+)
+
+from homeassistant.const import STATE_UNAVAILABLE
+
+from ..const import ARLEC_FAN_PAYLOAD
+from ..helpers import assert_device_properties_set
+from .base_device_tests import TuyaDeviceTestCase
+
+SWITCH_DPS = "1"
+SPEED_DPS = "3"
+DIRECTION_DPS = "4"
+PRESET_DPS = "102"
+TIMER_DPS = "103"
+
+
+class TestArlecFan(TuyaDeviceTestCase):
+    __test__ = True
+
+    def setUp(self):
+        self.setUpForConfig("arlec_fan.yaml", ARLEC_FAN_PAYLOAD)
+        self.subject = self.entities["fan"]
+
+    def test_supported_features(self):
+        self.assertEqual(
+            self.subject.supported_features,
+            SUPPORT_DIRECTION | SUPPORT_PRESET_MODE | SUPPORT_SET_SPEED,
+        )
+
+    def test_is_on(self):
+        self.dps[SWITCH_DPS] = True
+        self.assertTrue(self.subject.is_on)
+
+        self.dps[SWITCH_DPS] = False
+        self.assertFalse(self.subject.is_on)
+
+        self.dps[SWITCH_DPS] = None
+        self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
+
+    async def test_turn_on(self):
+        async with assert_device_properties_set(
+            self.subject._device, {SWITCH_DPS: True}
+        ):
+            await self.subject.async_turn_on()
+
+    async def test_turn_off(self):
+        async with assert_device_properties_set(
+            self.subject._device, {SWITCH_DPS: False}
+        ):
+            await self.subject.async_turn_off()
+
+    def test_preset_mode(self):
+        self.dps[PRESET_DPS] = "normal"
+        self.assertEqual(self.subject.preset_mode, "normal")
+
+        self.dps[PRESET_DPS] = "breeze"
+        self.assertEqual(self.subject.preset_mode, "breeze")
+
+        self.dps[PRESET_DPS] = "sleep"
+        self.assertEqual(self.subject.preset_mode, "sleep")
+
+        self.dps[PRESET_DPS] = None
+        self.assertIs(self.subject.preset_mode, None)
+
+    def test_preset_modes(self):
+        self.assertCountEqual(self.subject.preset_modes, ["normal", "breeze", "sleep"])
+
+    async def test_set_preset_mode_to_normal(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {PRESET_DPS: "normal"},
+        ):
+            await self.subject.async_set_preset_mode("normal")
+
+    async def test_set_preset_mode_to_breeze(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {PRESET_DPS: "breeze"},
+        ):
+            await self.subject.async_set_preset_mode("breeze")
+
+    async def test_set_preset_mode_to_sleep(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {PRESET_DPS: "sleep"},
+        ):
+            await self.subject.async_set_preset_mode("sleep")
+
+    def test_direction(self):
+        self.dps[DIRECTION_DPS] = "forward"
+        self.assertEqual(self.subject.current_direction, DIRECTION_FORWARD)
+        self.dps[DIRECTION_DPS] = "reverse"
+        self.assertEqual(self.subject.current_direction, DIRECTION_REVERSE)
+
+    async def test_set_direction_forward(self):
+        async with assert_device_properties_set(
+            self.subject._device, {DIRECTION_DPS: "forward"}
+        ):
+            await self.subject.async_set_direction(DIRECTION_FORWARD)
+
+    async def test_set_direction_reverse(self):
+        async with assert_device_properties_set(
+            self.subject._device, {DIRECTION_DPS: "reverse"}
+        ):
+            await self.subject.async_set_direction(DIRECTION_REVERSE)
+
+    def test_speed(self):
+        self.dps[SPEED_DPS] = "3"
+        self.assertEqual(self.subject.percentage, 50)
+
+    def test_speed_step(self):
+        self.assertAlmostEqual(self.subject.percentage_step, 16.67, 2)
+        self.assertEqual(self.subject.speed_count, 6)
+
+    async def test_set_speed(self):
+        async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 2}):
+            await self.subject.async_set_percentage(33)
+
+    async def test_set_speed_in_normal_mode_snaps(self):
+        self.dps[PRESET_DPS] = "normal"
+        async with assert_device_properties_set(self.subject._device, {SPEED_DPS: 5}):
+            await self.subject.async_set_percentage(80)
+
+    def test_device_state_attributes(self):
+        self.dps[TIMER_DPS] = "5"
+        self.assertEqual(self.subject.device_state_attributes, {"timer": 5})