From 069a40842470ae17ba6da5bdd978ab505bd6bcce Mon Sep 17 00:00:00 2001 From: lcheng33775823 <1055446591@qq.com> Date: Tue, 15 Oct 2024 20:16:51 +0800 Subject: [PATCH 1/7] Add quirk for Sonoff ZBMINIR2 --- zhaquirks/sonoff/zbminir2.py | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 zhaquirks/sonoff/zbminir2.py diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py new file mode 100644 index 0000000000..dd17945b09 --- /dev/null +++ b/zhaquirks/sonoff/zbminir2.py @@ -0,0 +1,86 @@ +"""Sonoff ZBMINIR2 - Zigbee Switch.""" + +from zigpy import types +from zigpy.quirks import CustomCluster +from zigpy.quirks.v2 import QuirkBuilder +import zigpy.types as t +from zigpy.zcl import foundation +from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef + + +class CustomSonoffCluster(CustomCluster): + """Custom Sonoff cluster.""" + + cluster_id = 0xFC11 + + class AttributeDefs(BaseAttributeDefs): + """Attribute definitions.""" + + ExternalTriggerMode = ZCLAttributeDef( + name="ExternalTriggerMode", + id=0x0016, + type=t.uint8_t, + ) + + DetachRelay = ZCLAttributeDef( + name="DetachRelay", + id=0x0017, + type=t.Bool, + ) + + TurboMode = ZCLAttributeDef(name="TurboMode", id=0x0012, type=t.int16s) + + async def _read_attributes( + self, + attribute_ids: list[t.uint16_t], + *args, + manufacturer: int | t.uint16_t | None = None, + **kwargs, + ): + """Read attributes ZCL foundation command.""" + return await super()._read_attributes( + attribute_ids, + *args, + manufacturer=foundation.ZCLHeader.NO_MANUFACTURER_ID, + **kwargs, + ) + + @property + def _is_manuf_specific(self): + return False + + +class SonoffExternalSwitchTriggerType(types.enum8): + """extern switch trigger type.""" + + edge_trigger = 0x00 + pluse_trigger = 0x01 + normally_off_follow_trigger = 0x02 + normally_on_follow_trigger = 0x82 + + +( + QuirkBuilder("SONOFF", "ZBMINIR2") + .replaces(CustomSonoffCluster) + .enum( + CustomSonoffCluster.AttributeDefs.ExternalTriggerMode.name, + SonoffExternalSwitchTriggerType, + CustomSonoffCluster.cluster_id, + fallback_name="External Trigger Mode", + ) + .switch( + CustomSonoffCluster.AttributeDefs.TurboMode.name, + CustomSonoffCluster.cluster_id, + off_value=9, + on_value=20, + fallback_name="Turbo Mode", + ) + .switch( + CustomSonoffCluster.AttributeDefs.DetachRelay.name, + CustomSonoffCluster.cluster_id, + off_value=0, + on_value=1, + fallback_name="Detach Relay", + ) + .add_to_registry() +) From d779286c6a3d95a3a409b6f5ba978020d5755ec6 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:18:09 +0100 Subject: [PATCH 2/7] Rename attributes and cluster --- zhaquirks/sonoff/zbminir2.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index dd17945b09..7e02d30037 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -8,7 +8,7 @@ from zigpy.zcl.foundation import BaseAttributeDefs, ZCLAttributeDef -class CustomSonoffCluster(CustomCluster): +class SonoffCluster(CustomCluster): """Custom Sonoff cluster.""" cluster_id = 0xFC11 @@ -16,19 +16,18 @@ class CustomSonoffCluster(CustomCluster): class AttributeDefs(BaseAttributeDefs): """Attribute definitions.""" - ExternalTriggerMode = ZCLAttributeDef( - name="ExternalTriggerMode", + external_trigger_mode = ZCLAttributeDef( id=0x0016, type=t.uint8_t, ) - - DetachRelay = ZCLAttributeDef( - name="DetachRelay", + detach_relay = ZCLAttributeDef( id=0x0017, type=t.Bool, ) - - TurboMode = ZCLAttributeDef(name="TurboMode", id=0x0012, type=t.int16s) + turbo_mode = ZCLAttributeDef( + id=0x0012, + type=t.int16s, + ) async def _read_attributes( self, @@ -63,21 +62,21 @@ class SonoffExternalSwitchTriggerType(types.enum8): QuirkBuilder("SONOFF", "ZBMINIR2") .replaces(CustomSonoffCluster) .enum( - CustomSonoffCluster.AttributeDefs.ExternalTriggerMode.name, + SonoffCluster.AttributeDefs.external_trigger_mode.name, SonoffExternalSwitchTriggerType, - CustomSonoffCluster.cluster_id, + SonoffCluster.cluster_id, fallback_name="External Trigger Mode", ) .switch( - CustomSonoffCluster.AttributeDefs.TurboMode.name, - CustomSonoffCluster.cluster_id, + SonoffCluster.AttributeDefs.turbo_mode.name, + SonoffCluster.cluster_id, off_value=9, on_value=20, fallback_name="Turbo Mode", ) .switch( - CustomSonoffCluster.AttributeDefs.DetachRelay.name, - CustomSonoffCluster.cluster_id, + SonoffCluster.AttributeDefs.detach_relay.name, + SonoffCluster.cluster_id, off_value=0, on_value=1, fallback_name="Detach Relay", From 8b43a59d3c94d465e97e844b0a051f308749e406 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:18:34 +0100 Subject: [PATCH 3/7] Change entity names to match HA-style --- zhaquirks/sonoff/zbminir2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index 7e02d30037..6fa04d9783 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -65,21 +65,21 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.AttributeDefs.external_trigger_mode.name, SonoffExternalSwitchTriggerType, SonoffCluster.cluster_id, - fallback_name="External Trigger Mode", + fallback_name="External trigger mode", ) .switch( SonoffCluster.AttributeDefs.turbo_mode.name, SonoffCluster.cluster_id, off_value=9, on_value=20, - fallback_name="Turbo Mode", + fallback_name="Turbo mode", ) .switch( SonoffCluster.AttributeDefs.detach_relay.name, SonoffCluster.cluster_id, off_value=0, on_value=1, - fallback_name="Detach Relay", + fallback_name="Detach relay", ) .add_to_registry() ) From 6ce7cc6af75ce77fde51dfedddf9e8e1646c4af1 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:19:10 +0100 Subject: [PATCH 4/7] Fix typo changing`pluse` to `pulse` --- zhaquirks/sonoff/zbminir2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index 6fa04d9783..28869955f4 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -53,7 +53,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): """extern switch trigger type.""" edge_trigger = 0x00 - pluse_trigger = 0x01 + pulse_trigger = 0x01 normally_off_follow_trigger = 0x02 normally_on_follow_trigger = 0x82 From 17aa39e4fce38d5d24a624bcc0b7b507c3b1a675 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:20:25 +0100 Subject: [PATCH 5/7] Also change cluster name in `.replaces(cluster)` --- zhaquirks/sonoff/zbminir2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index 28869955f4..7978b0aa2f 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -60,7 +60,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): ( QuirkBuilder("SONOFF", "ZBMINIR2") - .replaces(CustomSonoffCluster) + .replaces(SonoffCluster) .enum( SonoffCluster.AttributeDefs.external_trigger_mode.name, SonoffExternalSwitchTriggerType, From 61906d212f85c5e713a440311cf797456cae4de1 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:22:58 +0100 Subject: [PATCH 6/7] Add translation keys to quirks v2 entities --- zhaquirks/sonoff/zbminir2.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index 7978b0aa2f..4b299f8830 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -65,6 +65,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.AttributeDefs.external_trigger_mode.name, SonoffExternalSwitchTriggerType, SonoffCluster.cluster_id, + translation_key="external_trigger_mode" fallback_name="External trigger mode", ) .switch( @@ -72,6 +73,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.cluster_id, off_value=9, on_value=20, + translation_key="turbo_mode", fallback_name="Turbo mode", ) .switch( @@ -79,6 +81,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.cluster_id, off_value=0, on_value=1, + translation_key="detach_relay" fallback_name="Detach relay", ) .add_to_registry() From d2483b88ee68bf1f74d3fe36667e5967dd804860 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Thu, 31 Oct 2024 02:23:48 +0100 Subject: [PATCH 7/7] Fix missing commas --- zhaquirks/sonoff/zbminir2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zhaquirks/sonoff/zbminir2.py b/zhaquirks/sonoff/zbminir2.py index 4b299f8830..d405ce47c6 100644 --- a/zhaquirks/sonoff/zbminir2.py +++ b/zhaquirks/sonoff/zbminir2.py @@ -65,7 +65,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.AttributeDefs.external_trigger_mode.name, SonoffExternalSwitchTriggerType, SonoffCluster.cluster_id, - translation_key="external_trigger_mode" + translation_key="external_trigger_mode", fallback_name="External trigger mode", ) .switch( @@ -81,7 +81,7 @@ class SonoffExternalSwitchTriggerType(types.enum8): SonoffCluster.cluster_id, off_value=0, on_value=1, - translation_key="detach_relay" + translation_key="detach_relay", fallback_name="Detach relay", ) .add_to_registry()