Ver código fonte

feat(fan): added support for gorilla fan v2

Aravind Jaimon 1 ano atrás
pai
commit
79a74d6ac3

+ 58 - 0
custom_components/tuya_local/devices/gorilla_fan_v2.yaml

@@ -0,0 +1,58 @@
+name: Gorilla Fan V2
+primary_entity:
+  entity: fan
+  dps:
+    - id: 1
+      type: boolean
+      name: switch
+      icon: mdi:ceiling-fan
+    - id: 2
+      type: string
+      name: preset_mode
+      mapping:
+        - dps_value: normal
+          value: Normal
+        - dps_value: boost
+          value: Boost
+    - id: 3
+      type: integer
+      name: speed
+      range:
+        min: 1
+        max: 5
+
+secondary_entities:
+  - entity: light
+    name: Night Light
+    icon: mdi:ceiling-fan-light
+    dps:
+      - id: 15
+        type: boolean
+        name: switch
+  - entity: switch
+    name: Boost
+    icon: mdi:car-turbocharger
+    dps:
+      - id: 115
+        type: boolean
+        name: switch
+  - entity: switch
+    name: Sleep
+    icon: mdi:sleep
+    dps:
+      - id: 113
+        type: boolean
+        name: switch
+  - entity: number
+    name: Timer
+    icon: mdi:timer-edit
+    translation_key: timer
+    category: config
+    dps:
+      - id: 102
+        type: integer
+        name: value
+        unit: min
+        range:
+          min: 0
+          max: 360

+ 71 - 0
tests/devices/test_gorilla_fan_v2.py

@@ -0,0 +1,71 @@
+from homeassistant.components.fan import FanEntityFeature
+
+from ..const import FAN_PAYLOAD
+from ..helpers import assert_device_properties_set
+from .base_device_tests import TuyaDeviceTestCase
+
+SWITCH_DPS = "1"
+PRESET_DPS = "2"
+FANMODE_DPS = "3"
+
+class TestGorillaFanV2(TuyaDeviceTestCase):
+    __test__ = True
+
+    def setUp(self):
+        self.setUpForConfig("gorilla_fan_v2.yaml", FAN_PAYLOAD)
+        self.subject = self.entities.get("fan")
+
+    def test_supported_features(self):
+        self.assertEqual(
+            self.subject.supported_features,
+            (
+                FanEntityFeature.PRESET_MODE
+                | FanEntityFeature.SET_SPEED
+            ),
+        )
+
+    def test_preset_mode(self):
+        self.dps[PRESET_DPS] = "normal"
+        self.assertEqual(self.subject.preset_mode, "Normal")
+
+        self.dps[PRESET_DPS] = "boost"
+        self.assertEqual(self.subject.preset_mode, "Boost")
+
+    def test_preset_modes(self):
+        self.assertCountEqual(self.subject.preset_modes, ["normal", "boost"])
+
+    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_boost(self):
+        async with assert_device_properties_set(
+            self.subject._device,
+            {PRESET_DPS: "boost"},
+        ):
+            await self.subject.async_set_preset_mode("Boost")
+
+    def test_speed(self):
+        self.dps[PRESET_DPS] = "normal"
+        self.dps[FANMODE_DPS] = 3
+        self.assertEqual(self.subject.percentage, 60)
+
+    async def test_set_speed_in_normal_mode(self):
+        self.dps[PRESET_DPS] = "normal"
+        async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 3}):
+            await self.subject.async_set_percentage(60)
+
+    async def test_set_speed_in_boost_mode(self):
+        self.dps[PRESET_DPS] = "boost"
+        async with assert_device_properties_set(self.subject._device, {FANMODE_DPS: 5}):
+            await self.subject.async_set_percentage(100)
+
+    async def test_set_speed_to_zero_turns_off(self):
+        async with assert_device_properties_set(
+            self.subject._device, {SWITCH_DPS: False}
+        ):
+            await self.subject.async_set_percentage(0)
+

+ 22 - 0
tests/test_fan.py

@@ -120,3 +120,25 @@ async def test_init_entry_fails_if_config_is_missing(hass):
     except ValueError:
         pass
     m_add_entities.assert_not_called()
+
+@pytest.mark.asyncio
+async def test_init_entry_for_gorilla_fan_v2(hass):
+    """Test initialisation for Gorilla Fan V2"""
+    entry = MockConfigEntry(
+        domain=DOMAIN,
+        data={
+            CONF_TYPE: "gorilla_fan_v2",
+            CONF_DEVICE_ID: "dummy",
+            CONF_PROTOCOL_VERSION: "auto",
+        },
+    )
+    m_add_entities = Mock()
+    m_device = AsyncMock()
+
+    hass.data[DOMAIN] = {}
+    hass.data[DOMAIN]["dummy"] = {}
+    hass.data[DOMAIN]["dummy"]["device"] = m_device
+
+    await async_setup_entry(hass, entry, m_add_entities)
+    assert type(hass.data[DOMAIN]["dummy"]["fan"]) == TuyaLocalFan
+    m_add_entities.assert_called_once()