From 499fe0bbdd27570d0b0dd6f30234a9f573736b49 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sat, 7 Sep 2024 17:29:19 +0300 Subject: [PATCH] Add support CK-BL602-W102SW18-01(226) #1425 --- custom_components/sonoff/core/devices.py | 10 +++ .../sonoff/core/ewelink/__init__.py | 4 +- custom_components/sonoff/switch.py | 13 ++++ tests/test_entity.py | 62 +++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) diff --git a/custom_components/sonoff/core/devices.py b/custom_components/sonoff/core/devices.py index 027deceb..4d349a8c 100644 --- a/custom_components/sonoff/core/devices.py +++ b/custom_components/sonoff/core/devices.py @@ -70,6 +70,7 @@ XZigbeeSwitches, XSwitchPOWR3, XDetach, + XBoolSwitch, ) # supported custom device_class @@ -371,6 +372,15 @@ def spec(cls, base: str = None, enabled: bool = None, **kwargs) -> type: 211: [Switch1, Switch2, Switch3, XT5Light, XT5Action], # T5-3C-86 # https://github.com/AlexxIT/SonoffLAN/issues/1251 212: [Switch1, Switch2, Switch3, Switch4, XT5Light, XT5Action], # T5-4C-86 + 226: [ + XBoolSwitch, + LED, + RSSI, + spec(XSensor, param="phase_0_c", uid="current"), + spec(XSensor, param="phase_0_p", uid="power"), + spec(XSensor, param="phase_0_v", uid="voltage"), + spec(XEnergyTotal, param="totalPower", uid="energy"), + ], # CK-BL602-W102SW18-01(226) 1000: [XRemoteButton, Battery], # zigbee_ON_OFF_SWITCH_1000 # https://github.com/AlexxIT/SonoffLAN/issues/1195 1256: [spec(XSwitch)], # ZCL_HA_DEVICEID_ON_OFF_LIGHT diff --git a/custom_components/sonoff/core/ewelink/__init__.py b/custom_components/sonoff/core/ewelink/__init__.py index c49a7e0c..756058e6 100644 --- a/custom_components/sonoff/core/ewelink/__init__.py +++ b/custom_components/sonoff/core/ewelink/__init__.py @@ -327,8 +327,8 @@ def update_device(self, device: XDevice): uiid = device["extra"]["uiid"] # [5] POW, [32] POWR2, [182] S40, [190] POWR3 - one channel, only cloud update - # [181] THR316D/THR320D - if uiid in (5, 32, 182, 190, 181): + # [181] THR316D/THR320D, [226] CK-BL602-W102SW18-01 + if uiid in (5, 32, 182, 190, 181, 226): if self.can_cloud(device): params = {"uiActive": 60} asyncio.create_task(self.cloud.send(device, params, timeout=0)) diff --git a/custom_components/sonoff/switch.py b/custom_components/sonoff/switch.py index 0b0cf135..17178077 100644 --- a/custom_components/sonoff/switch.py +++ b/custom_components/sonoff/switch.py @@ -133,3 +133,16 @@ async def async_turn_on(self, **kwargs): async def async_turn_off(self): await self.ewelink.send_cloud(self.device, {"relaySeparation": 0}) + + +class XBoolSwitch(XEntity, SwitchEntity): + params = {"switch"} + + def set_state(self, params: dict): + self._attr_is_on = params["switch"] + + async def async_turn_on(self, *args, **kwargs): + await self.ewelink.send(self.device, {"switch": True}) + + async def async_turn_off(self): + await self.ewelink.send(self.device, {"switch": False}) diff --git a/tests/test_entity.py b/tests/test_entity.py index f4309a11..19b73c49 100644 --- a/tests/test_entity.py +++ b/tests/test_entity.py @@ -60,6 +60,7 @@ XSwitchTH, XToggle, XZigbeeSwitches, + XBoolSwitch, ) from . import init, save_to, DEVICEID, DummyRegistry @@ -1959,3 +1960,64 @@ def test_snzb_03p(): assert entities[1].state == "on" assert entities[2].state == 100 assert entities[3].state == -68 + + +def test_226(): + device = { + "extra": {"uiid": 226}, + "params": { + "bindInfos": "***", + "version": 8, + "rssi": -77, + "fwVersion": "1.0.0", + "switch": True, + "pulse": "off", + "pulseWidth": 1000, + "sledOnline": "on", + "startup": "off", + "prepaid_support": True, + "alarm_v_min": -1, + "alarm_v_max": -1, + "alarm_c_min": -1, + "alarm_c_max": -1, + "alarm_p_min": -1, + "alarm_p_max": -1, + "availablePower": 0, + "prepaidEnale": False, + "prepaidLowBalanceAlert": False, + "prepaidLowest": 0, + "min_p_upper": 5000, + "min_p_lower": 1, + "max_p_upper": 15000, + "max_p_lower": 1, + "min_v_upper": 220, + "min_v_lower": 76, + "max_v_upper": 300, + "max_v_lower": 220, + "max_c_upper": 63, + "max_c_lower": 0.1, + "calibrateFlag": False, + "ssid": "***", + "bssid": "***", + "staMac": "***", + "totalPower": 0.7, + "phase_0_c": 1.13, + "phase_0_v": 241.08, + "phase_0_p": 51.83, + "uiActive": 60, + }, + "model": "CK-BL602-W102SW18-01(226)", + } + + entities = get_entitites(device) + + switch: SwitchEntity = next(e for e in entities if isinstance(e, XBoolSwitch)) + assert switch.state == "on" + current: XSensor = next(e for e in entities if e.uid == "current") + assert current.state == 1.13 + current: XSensor = next(e for e in entities if e.uid == "power") + assert current.state == 51.83 + current: XSensor = next(e for e in entities if e.uid == "voltage") + assert current.state == 241.08 + energy: XSensor = next(e for e in entities if e.uid == "energy") + assert energy.state == 0.7