Skip to content

Commit 2e2bbd9

Browse files
authored
Improve typing in Tuya builder (#4302)
1 parent f11926e commit 2e2bbd9

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

zhaquirks/tuya/builder/__init__.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import math
77
import pathlib
88
from types import FrameType
9-
from typing import Any
9+
from typing import Any, Self
1010

1111
from zigpy.quirks import _DEVICE_REGISTRY
1212
from zigpy.quirks.registry import DeviceRegistry
@@ -217,7 +217,7 @@ def _tuya_battery(
217217
dp_id: int,
218218
power_cfg: PowerConfiguration,
219219
scale: float,
220-
) -> QuirkBuilder:
220+
) -> Self:
221221
"""Add a Tuya Battery Power Configuration."""
222222
self.tuya_dp(
223223
dp_id,
@@ -236,7 +236,7 @@ def tuya_battery(
236236
battery_qty: int | None = 2,
237237
battery_voltage: int | None = None,
238238
scale: float = 2,
239-
) -> QuirkBuilder:
239+
) -> Self:
240240
"""Add a Tuya Battery Power Configuration."""
241241

242242
if power_cfg:
@@ -265,7 +265,7 @@ def tuya_illuminance(
265265
converter: Callable[[Any], Any] | None = (
266266
lambda x: 10000 * math.log10(x) + 1 if x != 0 else 0
267267
),
268-
) -> QuirkBuilder:
268+
) -> Self:
269269
"""Add a Tuya Illuminance Configuration."""
270270
self.tuya_dp(
271271
dp_id,
@@ -276,7 +276,7 @@ def tuya_illuminance(
276276
self.adds(illuminance_cfg)
277277
return self
278278

279-
def tuya_contact(self, dp_id: int):
279+
def tuya_contact(self, dp_id: int) -> Self:
280280
"""Add a Tuya IAS contact sensor."""
281281
self.tuya_ias(
282282
dp_id=dp_id,
@@ -290,7 +290,7 @@ def tuya_co2(
290290
dp_id: int,
291291
co2_cfg: TuyaLocalCluster = TuyaCO2Concentration,
292292
scale: float = 1e-6,
293-
) -> QuirkBuilder:
293+
) -> Self:
294294
"""Add a Tuya CO2 Configuration."""
295295
self.tuya_dp(
296296
dp_id,
@@ -306,7 +306,7 @@ def tuya_electrical_conductivity(
306306
dp_id: int,
307307
ec_cfg: TuyaLocalCluster = TuyaElectricalConductivity,
308308
scale: float = 1,
309-
) -> QuirkBuilder:
309+
) -> Self:
310310
"""Add a Tuya Electrical Conductivity Configuration."""
311311
self.tuya_dp(
312312
dp_id,
@@ -326,7 +326,7 @@ def tuya_formaldehyde(
326326
((MOL_VOL_AIR_NTP * x) / TuyaFormaldehydeConcentration.MOLECULAR_MASS), 2
327327
)
328328
* 1e-6,
329-
) -> QuirkBuilder:
329+
) -> Self:
330330
"""Add a Tuya Formaldehyde Configuration."""
331331
self.tuya_dp(
332332
dp_id,
@@ -342,7 +342,7 @@ def tuya_pm25(
342342
dp_id: int,
343343
pm25_cfg: TuyaLocalCluster = TuyaPM25Concentration,
344344
scale: float = 1,
345-
) -> QuirkBuilder:
345+
) -> Self:
346346
"""Add a Tuya PM25 Configuration."""
347347
self.tuya_dp(
348348
dp_id,
@@ -353,7 +353,7 @@ def tuya_pm25(
353353
self.adds(pm25_cfg)
354354
return self
355355

356-
def tuya_gas(self, dp_id: int):
356+
def tuya_gas(self, dp_id: int) -> Self:
357357
"""Add a Tuya IAS gas sensor."""
358358
self.tuya_ias(
359359
dp_id=dp_id,
@@ -362,7 +362,7 @@ def tuya_gas(self, dp_id: int):
362362
)
363363
return self
364364

365-
def tuya_smoke(self, dp_id: int):
365+
def tuya_smoke(self, dp_id: int) -> Self:
366366
"""Add a Tuya IAS smoke/fire sensor."""
367367
self.tuya_ias(
368368
dp_id=dp_id,
@@ -376,7 +376,7 @@ def tuya_ias(
376376
dp_id: int,
377377
ias_cfg: TuyaLocalCluster,
378378
converter: Callable[[Any], Any] | None = None,
379-
) -> QuirkBuilder:
379+
) -> Self:
380380
"""Add a Tuya IAS Configuration."""
381381
self.tuya_dp(
382382
dp_id,
@@ -392,7 +392,7 @@ def tuya_metering(
392392
dp_id: int,
393393
metering_cfg: TuyaLocalCluster = TuyaValveWaterConsumedNoInstDemand,
394394
scale: float = 1,
395-
) -> QuirkBuilder:
395+
) -> Self:
396396
"""Add a Tuya Metering Configuration."""
397397
self.tuya_dp(
398398
dp_id,
@@ -407,7 +407,7 @@ def tuya_onoff(
407407
self,
408408
dp_id: int,
409409
onoff_cfg: TuyaLocalCluster = TuyaOnOffNM,
410-
) -> QuirkBuilder:
410+
) -> Self:
411411
"""Add a Tuya OnOff Configuration."""
412412
self.tuya_dp(
413413
dp_id,
@@ -422,7 +422,7 @@ def tuya_humidity(
422422
dp_id: int,
423423
rh_cfg: TuyaLocalCluster = TuyaRelativeHumidity,
424424
scale: float = 100,
425-
) -> QuirkBuilder:
425+
) -> Self:
426426
"""Add a Tuya Relative Humidity Configuration."""
427427
self.tuya_dp(
428428
dp_id,
@@ -438,7 +438,7 @@ def tuya_soil_moisture(
438438
dp_id: int,
439439
soil_cfg: TuyaLocalCluster = TuyaSoilMoisture,
440440
scale: float = 100,
441-
) -> QuirkBuilder:
441+
) -> Self:
442442
"""Add a Tuya Soil Moisture Configuration."""
443443
self.tuya_dp(
444444
dp_id,
@@ -454,7 +454,7 @@ def tuya_temperature(
454454
dp_id: int,
455455
temp_cfg: TuyaLocalCluster = TuyaTemperatureMeasurement,
456456
scale: float = 100,
457-
) -> QuirkBuilder:
457+
) -> Self:
458458
"""Add a Tuya Temperature Configuration."""
459459
self.tuya_dp(
460460
dp_id,
@@ -465,7 +465,7 @@ def tuya_temperature(
465465
self.adds(temp_cfg)
466466
return self
467467

468-
def tuya_vibration(self, dp_id: int):
468+
def tuya_vibration(self, dp_id: int) -> Self:
469469
"""Add a Tuya IAS vibration sensor."""
470470
self.tuya_ias(
471471
dp_id=dp_id,
@@ -479,7 +479,7 @@ def tuya_voc(
479479
dp_id: int,
480480
voc_cfg: TuyaLocalCluster = TuyaAirQualityVOC,
481481
scale: float = 1e-6,
482-
) -> QuirkBuilder:
482+
) -> Self:
483483
"""Add a Tuya VOC Configuration."""
484484
self.tuya_dp(
485485
dp_id,
@@ -497,7 +497,7 @@ def tuya_attribute(
497497
type: type = t.uint16_t,
498498
access: foundation.ZCLAttributeAccess = foundation.ZCLAttributeAccess.NONE,
499499
is_manufacturer_specific=True,
500-
) -> QuirkBuilder:
500+
) -> Self:
501501
"""Add an attribute to AttributeDefs."""
502502
attr_id: int = int.from_bytes([0xEF, dp_id])
503503

@@ -522,7 +522,7 @@ def tuya_dp(
522522
dp_converter: Callable[[Any], Any] | None = None,
523523
endpoint_id: int | None = None,
524524
dp_handler: str = "_dp_2_attr_update",
525-
) -> QuirkBuilder:
525+
) -> Self:
526526
"""Add Tuya DP Converter."""
527527

528528
self.tuya_dp_multi(
@@ -545,7 +545,7 @@ def tuya_dp_multi(
545545
dp_id: int,
546546
attribute_mapping: list[DPToAttributeMapping],
547547
dp_handler: str = "_dp_2_attr_update",
548-
) -> QuirkBuilder: # fmt: skip
548+
) -> Self: # fmt: skip
549549
"""Add Tuya DP Converter that maps to multiple attributes."""
550550

551551
if dp_id in self.tuya_dp_to_attribute:
@@ -567,7 +567,7 @@ def tuya_dp_attribute(
567567
type: type = t.uint16_t,
568568
access: foundation.ZCLAttributeAccess = foundation.ZCLAttributeAccess.NONE,
569569
is_manufacturer_specific=True,
570-
) -> QuirkBuilder:
570+
) -> Self:
571571
"""Add an Tuya DataPoint and corresponding AttributeDef."""
572572
self.tuya_attribute(
573573
dp_id=dp_id,
@@ -602,7 +602,7 @@ def tuya_switch(
602602
attribute_initialized_from_cache: bool = True,
603603
translation_key: str | None = None,
604604
fallback_name: str | None = None,
605-
) -> QuirkBuilder:
605+
) -> Self:
606606
"""Add an EntityMetadata containing SwitchMetadata and return self.
607607
608608
This method allows exposing a switch entity in Home Assistant.
@@ -645,7 +645,7 @@ def tuya_enum(
645645
attribute_initialized_from_cache: bool = True,
646646
translation_key: str | None = None,
647647
fallback_name: str | None = None,
648-
) -> QuirkBuilder:
648+
) -> Self:
649649
"""Add an EntityMetadata containing ZCLEnumMetadata and return self.
650650
651651
This method allows exposing an enum based entity in Home Assistant.
@@ -691,7 +691,7 @@ def tuya_number(
691691
attribute_initialized_from_cache: bool = True,
692692
translation_key: str | None = None,
693693
fallback_name: str | None = None,
694-
) -> QuirkBuilder:
694+
) -> Self:
695695
"""Add an EntityMetadata containing NumberMetadata and return self.
696696
697697
This method allows exposing a number entity in Home Assistant.
@@ -733,7 +733,7 @@ def tuya_binary_sensor(
733733
attribute_initialized_from_cache: bool = True,
734734
translation_key: str | None = None,
735735
fallback_name: str | None = None,
736-
) -> QuirkBuilder:
736+
) -> Self:
737737
"""Add an EntityMetadata containing BinarySensorMetadata and return self.
738738
739739
This method allows exposing a binary sensor entity in Home Assistant.
@@ -777,7 +777,7 @@ def tuya_sensor(
777777
attribute_initialized_from_cache: bool = True,
778778
translation_key: str | None = None,
779779
fallback_name: str | None = None,
780-
) -> QuirkBuilder:
780+
) -> Self:
781781
"""Add an EntityMetadata containing ZCLSensorMetadata and return self.
782782
783783
This method allows exposing a sensor entity in Home Assistant.
@@ -812,7 +812,7 @@ def tuya_sensor(
812812

813813
def tuya_enchantment(
814814
self, read_attr_spell: bool = True, data_query_spell: bool = False
815-
) -> QuirkBuilder:
815+
) -> Self:
816816
"""Set the Tuya enchantment spells."""
817817

818818
class EnchantedDeviceV2(CustomDeviceV2, BaseEnchantedDevice):

zhaquirks/tuya/ts0601_power.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from zigpy.zcl.clusters.general import LevelControl, OnOff
1313
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
1414

15-
from zhaquirks.tuya import DPToAttributeMapping, TuyaLocalCluster
15+
from zhaquirks.tuya import TuyaLocalCluster
1616
from zhaquirks.tuya.builder import TuyaQuirkBuilder
17+
from zhaquirks.tuya.mcu import DPToAttributeMapping
1718

1819

1920
def dp_to_power(data: bytes) -> int:

0 commit comments

Comments
 (0)