Skip to content

Commit 9201cc6

Browse files
committed
Refactor
1 parent 023ae59 commit 9201cc6

File tree

2 files changed

+62
-63
lines changed

2 files changed

+62
-63
lines changed

zhaquirks/tuya/mcu/__init__.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datetime
66
from typing import Any, Optional, Union
77

8+
from zigpy.quirks.v2 import QuirkBuilder, QuirksV2RegistryEntry
89
import zigpy.types as t
910
from zigpy.zcl import foundation
1011
from zigpy.zcl.clusters.general import LevelControl, OnOff
@@ -724,3 +725,62 @@ class TuyaLevelControlManufCluster(TuyaMCUCluster):
724725
17: "_dp_2_attr_update",
725726
18: "_dp_2_attr_update",
726727
}
728+
729+
730+
class TuyaQuirkBuilder(QuirkBuilder):
731+
"""Tuya QuirkBuilder."""
732+
733+
tuya_attributes = TuyaMCUCluster.attributes.copy()
734+
tuya_data_point_handlers: dict[int, str] = {}
735+
tuya_dp_to_attribute: dict[int, DPToAttributeMapping] = {}
736+
737+
def add_tuya_dp(
738+
self,
739+
dp_id: int,
740+
ep_attribute: str,
741+
attribute_name: Union[str, tuple],
742+
converter: Optional[
743+
Callable[
744+
[
745+
Any,
746+
],
747+
Any,
748+
]
749+
] = None,
750+
dp_converter: Optional[
751+
Callable[
752+
[
753+
Any,
754+
],
755+
Any,
756+
]
757+
] = None,
758+
endpoint_id: Optional[int] = None,
759+
dp_handler: str = "_dp_2_attr_update",
760+
):
761+
"""Add Tuya DP Converter."""
762+
self.tuya_dp_to_attribute.update(
763+
{
764+
dp_id: DPToAttributeMapping(
765+
ep_attribute,
766+
attribute_name,
767+
converter=converter,
768+
dp_converter=dp_converter,
769+
endpoint_id=endpoint_id,
770+
)
771+
}
772+
)
773+
self.tuya_data_point_handlers.update({dp_id: dp_handler})
774+
return self
775+
776+
def add_to_registry(self) -> QuirksV2RegistryEntry:
777+
"""Build the quirks v2 registry entry."""
778+
779+
class TuyaReplacementCluster(TuyaMCUCluster):
780+
"""Replacement Tuya Cluster."""
781+
782+
data_point_handlers: dict[int, str] = self.tuya_data_point_handlers
783+
dp_to_attribute: dict[int, DPToAttributeMapping] = self.tuya_dp_to_attribute
784+
785+
self.replaces(TuyaReplacementCluster)
786+
return super().add_to_registry()

zhaquirks/tuya/ts0601_sensor.py

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""Tuya temp and humidity sensors."""
22

3-
from collections.abc import Callable
4-
from typing import Any, Optional, Union
3+
from typing import Any
54

65
from zigpy.profiles import zha
76
from zigpy.quirks import CustomDevice
8-
from zigpy.quirks.v2 import QuirkBuilder, QuirksV2RegistryEntry
97
from zigpy.zcl.clusters.general import Basic, Groups, Ota, Scenes, Time
108
from zigpy.zcl.clusters.measurement import (
119
RelativeHumidity,
@@ -23,7 +21,7 @@
2321
SKIP_CONFIGURATION,
2422
)
2523
from zhaquirks.tuya import TuyaLocalCluster, TuyaPowerConfigurationCluster2AAA
26-
from zhaquirks.tuya.mcu import DPToAttributeMapping, TuyaMCUCluster
24+
from zhaquirks.tuya.mcu import DPToAttributeMapping, TuyaMCUCluster, TuyaQuirkBuilder
2725

2826

2927
class TuyaTemperatureMeasurement(TemperatureMeasurement, TuyaLocalCluster):
@@ -336,65 +334,6 @@ class TuyaTempHumiditySensorVar04(CustomDevice):
336334
}
337335

338336

339-
class TuyaQuirkBuilder(QuirkBuilder):
340-
"""Tuya QuirkBuilder."""
341-
342-
tuya_attributes = TuyaMCUCluster.attributes.copy()
343-
tuya_data_point_handlers: dict[int, str] = {}
344-
tuya_dp_to_attribute: dict[int, DPToAttributeMapping] = {}
345-
346-
def add_tuya_dp(
347-
self,
348-
dp_id: int,
349-
ep_attribute: str,
350-
attribute_name: Union[str, tuple],
351-
converter: Optional[
352-
Callable[
353-
[
354-
Any,
355-
],
356-
Any,
357-
]
358-
] = None,
359-
dp_converter: Optional[
360-
Callable[
361-
[
362-
Any,
363-
],
364-
Any,
365-
]
366-
] = None,
367-
endpoint_id: Optional[int] = None,
368-
dp_handler: str = "_dp_2_attr_update",
369-
):
370-
"""Add Tuya DP Converter."""
371-
self.tuya_dp_to_attribute.update(
372-
{
373-
dp_id: DPToAttributeMapping(
374-
ep_attribute,
375-
attribute_name,
376-
converter=converter,
377-
dp_converter=dp_converter,
378-
endpoint_id=endpoint_id,
379-
)
380-
}
381-
)
382-
self.tuya_data_point_handlers.update({dp_id: dp_handler})
383-
return self
384-
385-
def add_to_registry(self) -> QuirksV2RegistryEntry:
386-
"""Build the quirks v2 registry entry."""
387-
388-
class TuyaReplacementCluster(TuyaMCUCluster):
389-
"""Replacement Tuya Cluster."""
390-
391-
data_point_handlers: dict[int, str] = self.tuya_data_point_handlers
392-
dp_to_attribute: dict[int, DPToAttributeMapping] = self.tuya_dp_to_attribute
393-
394-
self.replaces(TuyaReplacementCluster)
395-
return super().add_to_registry()
396-
397-
398337
(
399338
TuyaQuirkBuilder("_TZE284_aao3yzhs", "TS0601")
400339
.also_applies_to("_TZE284_sgabhwa6", "TS0601")

0 commit comments

Comments
 (0)