From 376f5daacc3cdf4fd239d474446e49dbe5659b6f Mon Sep 17 00:00:00 2001 From: mrrstux Date: Wed, 27 Nov 2024 20:48:58 +0100 Subject: [PATCH] Allow custom tuya_meterig value converter. Convert RoyalGardineer valve water consumed value from deciliters to liters. --- zhaquirks/tuya/builder/__init__.py | 19 ++++++++++--------- zhaquirks/tuya/ts0601_valve.py | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/zhaquirks/tuya/builder/__init__.py b/zhaquirks/tuya/builder/__init__.py index a902fd4d5e..1cdbf93981 100644 --- a/zhaquirks/tuya/builder/__init__.py +++ b/zhaquirks/tuya/builder/__init__.py @@ -4,6 +4,13 @@ from enum import Enum from typing import Any, Optional +from zhaquirks.tuya import ( + TUYA_CLUSTER_ID, + PowerConfiguration, + TuyaLocalCluster, + TuyaPowerConfigurationCluster2AAA, +) +from zhaquirks.tuya.mcu import DPToAttributeMapping, TuyaMCUCluster, TuyaOnOffNM from zigpy.quirks import _DEVICE_REGISTRY from zigpy.quirks.registry import DeviceRegistry from zigpy.quirks.v2 import QuirkBuilder, QuirksV2RegistryEntry @@ -20,14 +27,6 @@ ) from zigpy.zcl.clusters.smartenergy import Metering -from zhaquirks.tuya import ( - TUYA_CLUSTER_ID, - PowerConfiguration, - TuyaLocalCluster, - TuyaPowerConfigurationCluster2AAA, -) -from zhaquirks.tuya.mcu import DPToAttributeMapping, TuyaMCUCluster, TuyaOnOffNM - class TuyaRelativeHumidity(RelativeHumidity, TuyaLocalCluster): """Tuya local RelativeHumidity cluster.""" @@ -93,13 +92,15 @@ def tuya_battery( def tuya_metering( self, dp_id: int, + converter: Optional[Callable[[Any], Any]] = None, metering_cfg: TuyaLocalCluster = TuyaValveWaterConsumed, ) -> QuirkBuilder: """Add a Tuya Metering Configuration.""" self.tuya_dp( dp_id, metering_cfg.ep_attribute, - "current_summ_delivered", + attribute_name="current_summ_delivered", + converter=converter, ) self.adds(metering_cfg) return self diff --git a/zhaquirks/tuya/ts0601_valve.py b/zhaquirks/tuya/ts0601_valve.py index 802aa413cd..6097f44031 100644 --- a/zhaquirks/tuya/ts0601_valve.py +++ b/zhaquirks/tuya/ts0601_valve.py @@ -2,16 +2,6 @@ from datetime import datetime, timedelta, timezone -from zigpy.profiles import zha -from zigpy.quirks import CustomDevice -from zigpy.quirks.v2 import EntityPlatform, EntityType -from zigpy.quirks.v2.homeassistant import UnitOfTime -from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass -import zigpy.types as t -from zigpy.zcl import foundation -from zigpy.zcl.clusters.general import Basic, Groups, Identify, OnOff, Ota, Scenes, Time -from zigpy.zcl.clusters.smartenergy import Metering - from zhaquirks import DoublingPowerConfigurationCluster from zhaquirks.const import ( DEVICE_TYPE, @@ -35,6 +25,15 @@ TuyaOnOff, TuyaPowerConfigurationCluster, ) +from zigpy.profiles import zha +from zigpy.quirks import CustomDevice +from zigpy.quirks.v2 import EntityPlatform, EntityType +from zigpy.quirks.v2.homeassistant import UnitOfTime +from zigpy.quirks.v2.homeassistant.sensor import SensorDeviceClass, SensorStateClass +import zigpy.types as t +from zigpy.zcl import foundation +from zigpy.zcl.clusters.general import Basic, Groups, Identify, OnOff, Ota, Scenes, Time +from zigpy.zcl.clusters.smartenergy import Metering class TuyaValveWaterConsumed(Metering, TuyaLocalCluster): @@ -592,8 +591,8 @@ class RoyalGardineerTimerState(t.enum8): TuyaQuirkBuilder("_TZE200_2wg5qrjy", "TS0601") .tuya_onoff(dp_id=1) .tuya_battery(dp_id=7, power_cfg=TuyaPowerConfigurationCluster2AA) - # Might need a converter: x // 10 - .tuya_metering(dp_id=5) + # Water consumed (value comes in deciliters - convert it to liters) + .tuya_metering(dp_id=5, converter=lambda x: x // 10) # Timer time left/remaining (raw value in seconds) .tuya_number( dp_id=11,