Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions zhaquirks/tuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,15 @@ def handle_get_data(self, command: TuyaCommand) -> foundation.Status:
dp_error = True
# return foundation.Status.UNSUPPORTED_ATTRIBUTE

_LOGGER.debug(
"[0x%04x:%s:0x%04x] Received value %s " "for attribute 0x%04x",
self.endpoint.device.nwk,
self.endpoint.endpoint_id,
self.cluster_id,
record.data.payload,
record.dp,
)

return (
foundation.Status.SUCCESS
if not dp_error
Expand Down
201 changes: 200 additions & 1 deletion zhaquirks/tuya/ts0601_din_power.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tuya Din Power Meter."""

from zigpy.profiles import zha
from zigpy.quirks.v2 import SensorDeviceClass, SensorStateClass
from zigpy.quirks.v2.homeassistant import UnitOfEnergy
import zigpy.types as t
from zigpy.zcl.clusters.general import Basic, Groups, Ota, Scenes, Time
from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement
Expand All @@ -15,7 +17,13 @@
OUTPUT_CLUSTERS,
PROFILE_ID,
)
from zhaquirks.tuya import TuyaManufClusterAttributes, TuyaOnOff, TuyaSwitch
from zhaquirks.tuya import (
TuyaLocalCluster,
TuyaManufClusterAttributes,
TuyaOnOff,
TuyaSwitch,
)
from zhaquirks.tuya.builder import TuyaQuirkBuilder

TUYA_TOTAL_ENERGY_ATTR = 0x0211
TUYA_CURRENT_ATTR = 0x0212
Expand Down Expand Up @@ -233,6 +241,197 @@ def __init__(self, *args, **kwargs):
}


class Tuya3PhaseElectricalMeasurement(ElectricalMeasurement, TuyaLocalCluster):
"""Tuya Electrical Measurement cluster."""

_CONSTANT_ATTRIBUTES = {
ElectricalMeasurement.AttributeDefs.ac_current_divisor.id: 1000,
ElectricalMeasurement.AttributeDefs.ac_voltage_divisor.id: 10,
ElectricalMeasurement.AttributeDefs.ac_frequency_divisor.id: 100,
}


(
TuyaQuirkBuilder("_TZE204_ugekduaj", "TS0601")
.tuya_dp(
dp_id=0x65,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="ac_frequency",
)
# Energy consumed
.tuya_sensor(
dp_id=0x01,
attribute_name="energy_consumed",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
fallback_name="Total energy",
)
.tuya_sensor(
dp_id=0x70,
attribute_name="energy_consumed_ph_a",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_ph_a",
fallback_name="Energy phase A",
)
.tuya_sensor(
dp_id=0x72,
attribute_name="energy_consumed_ph_b",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_ph_b",
fallback_name="Energy phase B",
)
.tuya_sensor(
dp_id=0x74,
attribute_name="energy_consumed_ph_c",
divisor=100,
type=t.uint32_t,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_ph_c",
fallback_name="Energy phase C",
)
# Energy produced
.tuya_sensor(
dp_id=0x02,
attribute_name="energy_produced",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_produced",
fallback_name="Energy produced",
)
.tuya_sensor(
dp_id=0x71,
attribute_name="energy_produced_ph_a",
type=t.uint32_t,
converter=lambda x: x / 100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_produced_ph_a",
fallback_name="Energy produced phase A",
)
.tuya_sensor(
dp_id=0x73,
attribute_name="energy_produced_ph_b",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_produced_ph_b",
fallback_name="Energy produced phase B",
)
.tuya_sensor(
dp_id=0x75,
attribute_name="energy_produced_ph_c",
type=t.uint32_t,
divisor=100,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
unit=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_produced_ph_c",
fallback_name="Energy produced phase C",
)
# Power
.tuya_dp(
dp_id=0x6F,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="total_active_power",
)
.tuya_dp(
dp_id=0x68,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="active_power",
)
.tuya_dp(
dp_id=0x6B,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="active_power_ph_b",
)
.tuya_dp(
dp_id=0x6E,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="active_power_ph_c",
)
# Voltage
.tuya_dp(
dp_id=0x66,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_voltage",
)
.tuya_dp(
dp_id=0x69,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_voltage_ph_b",
)
.tuya_dp(
dp_id=0x6C,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_voltage_ph_c",
)
# Current
.tuya_dp(
dp_id=0x67,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_current",
)
.tuya_dp(
dp_id=0x6A,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_current_ph_b",
)
.tuya_dp(
dp_id=0x6D,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="rms_current_ph_c",
)
# Power factor
.tuya_sensor(
dp_id=0x0F,
attribute_name="power_factor",
type=t.uint8_t,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.POWER_FACTOR,
# unit=PERCENTAGE, # ZHA fails to validate this one
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add this now. All units are supported with zigpy/zha#352 now.

translation_key="total_power_factor",
fallback_name="Total power factor",
)
.tuya_dp(
dp_id=0x76,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="power_factor",
)
.tuya_dp(
dp_id=0x77,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="power_factor_ph_b",
)
.tuya_dp(
dp_id=0x78,
ep_attribute=Tuya3PhaseElectricalMeasurement.ep_attribute,
attribute_name="power_factor_ph_c",
)
.adds(Tuya3PhaseElectricalMeasurement)
.skip_configuration()
.add_to_registry()
)


class HikingPowerMeter(TuyaSwitch):
"""Hiking Power Meter Device - DDS238-2."""

Expand Down
Loading