From 88d5dab64d24bc2d7f4184687d0b19aa9e79b610 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 7 Sep 2026 09:19:40 +0200 Subject: [PATCH] Refactor tuya CZ/KG sensor descriptions (#181504) --- homeassistant/components/tuya/sensor.py | 236 +++++++++++++----------- 1 file changed, 131 insertions(+), 105 deletions(-) diff --git a/homeassistant/components/tuya/sensor.py b/homeassistant/components/tuya/sensor.py index b0638c10428c..0268d3e0b604 100644 --- a/homeassistant/components/tuya/sensor.py +++ b/homeassistant/components/tuya/sensor.py @@ -278,6 +278,137 @@ SENSORS: dict[DeviceCategory, tuple[TuyaSensorEntityDescription, ...]] = { key=DPCode.WATER_LEVEL, translation_key="water_level_state" ), ), + DeviceCategory.CZ: ( + TuyaSensorEntityDescription( + key=DPCode.CUR_CURRENT, + translation_key="current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_POWER, + translation_key="power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_VOLTAGE, + translation_key="voltage", + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, + ), + TuyaSensorEntityDescription( + key=DPCode.ADD_ELE, + translation_key="total_energy", + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + TuyaSensorEntityDescription( + key=DPCode.PRO_ADD_ELE, + translation_key="total_production", + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + ), + TuyaSensorEntityDescription( + key=DPCode.DEVICE_STATE1, + translation_key="indexed_meter_status", + translation_placeholders={"index": "1"}, + ), + TuyaSensorEntityDescription( + key=DPCode.DEVICE_STATE2, + translation_key="indexed_meter_status", + translation_placeholders={"index": "2"}, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_CURRENT1, + translation_key="indexed_current", + translation_placeholders={"index": "1"}, + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_CURRENT2, + translation_key="indexed_current", + translation_placeholders={"index": "2"}, + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_POWER1, + translation_key="indexed_power", + translation_placeholders={"index": "1"}, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_POWER2, + translation_key="indexed_power", + translation_placeholders={"index": "2"}, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_VOLTAGE1, + translation_key="indexed_voltage", + translation_placeholders={"index": "1"}, + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, + ), + TuyaSensorEntityDescription( + key=DPCode.CUR_VOLTAGE2, + translation_key="indexed_voltage", + translation_placeholders={"index": "2"}, + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, + ), + TuyaSensorEntityDescription( + key=DPCode.TOTAL_ENERGY1, + translation_key="indexed_total_energy", + translation_placeholders={"index": "1"}, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + TuyaSensorEntityDescription( + key=DPCode.TOTAL_ENERGY2, + translation_key="indexed_total_energy", + translation_placeholders={"index": "2"}, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + TuyaSensorEntityDescription( + key=DPCode.TODAY_ACC_ENERGY1, + translation_key="indexed_energy_today", + translation_placeholders={"index": "1"}, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + TuyaSensorEntityDescription( + key=DPCode.TODAY_ACC_ENERGY2, + translation_key="indexed_energy_today", + translation_placeholders={"index": "2"}, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + TuyaSensorEntityDescription( + key=DPCode.ALL_ENERGY, + translation_key="total_energy", + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + ), + ), DeviceCategory.DGNBJ: ( TuyaSensorEntityDescription( key=DPCode.GAS_SENSOR_VALUE, @@ -1673,111 +1804,6 @@ SENSORS: dict[DeviceCategory, tuple[TuyaSensorEntityDescription, ...]] = { ), } -# Two-channel current transformer meters report a full set of electricity DPs -# per channel, plus a combined energy total for both channels. -DUAL_CHANNEL_METER_SENSORS: tuple[TuyaSensorEntityDescription, ...] = ( - TuyaSensorEntityDescription( - key=DPCode.DEVICE_STATE1, - translation_key="indexed_meter_status", - translation_placeholders={"index": "1"}, - ), - TuyaSensorEntityDescription( - key=DPCode.DEVICE_STATE2, - translation_key="indexed_meter_status", - translation_placeholders={"index": "2"}, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_CURRENT1, - translation_key="indexed_current", - translation_placeholders={"index": "1"}, - device_class=SensorDeviceClass.CURRENT, - state_class=SensorStateClass.MEASUREMENT, - suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_CURRENT2, - translation_key="indexed_current", - translation_placeholders={"index": "2"}, - device_class=SensorDeviceClass.CURRENT, - state_class=SensorStateClass.MEASUREMENT, - suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_POWER1, - translation_key="indexed_power", - translation_placeholders={"index": "1"}, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_POWER2, - translation_key="indexed_power", - translation_placeholders={"index": "2"}, - device_class=SensorDeviceClass.POWER, - state_class=SensorStateClass.MEASUREMENT, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_VOLTAGE1, - translation_key="indexed_voltage", - translation_placeholders={"index": "1"}, - device_class=SensorDeviceClass.VOLTAGE, - state_class=SensorStateClass.MEASUREMENT, - suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, - ), - TuyaSensorEntityDescription( - key=DPCode.CUR_VOLTAGE2, - translation_key="indexed_voltage", - translation_placeholders={"index": "2"}, - device_class=SensorDeviceClass.VOLTAGE, - state_class=SensorStateClass.MEASUREMENT, - suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, - ), - TuyaSensorEntityDescription( - key=DPCode.TOTAL_ENERGY1, - translation_key="indexed_total_energy", - translation_placeholders={"index": "1"}, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL_INCREASING, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - ), - TuyaSensorEntityDescription( - key=DPCode.TOTAL_ENERGY2, - translation_key="indexed_total_energy", - translation_placeholders={"index": "2"}, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL_INCREASING, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - ), - TuyaSensorEntityDescription( - key=DPCode.TODAY_ACC_ENERGY1, - translation_key="indexed_energy_today", - translation_placeholders={"index": "1"}, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL_INCREASING, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - ), - TuyaSensorEntityDescription( - key=DPCode.TODAY_ACC_ENERGY2, - translation_key="indexed_energy_today", - translation_placeholders={"index": "2"}, - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL_INCREASING, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - ), - TuyaSensorEntityDescription( - key=DPCode.ALL_ENERGY, - translation_key="total_energy", - device_class=SensorDeviceClass.ENERGY, - state_class=SensorStateClass.TOTAL_INCREASING, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - ), -) - -# Socket (duplicate of `kg`, plus the two-channel meter DPs) -SENSORS[DeviceCategory.CZ] = ( - *SENSORS[DeviceCategory.KG], - *DUAL_CHANNEL_METER_SENSORS, -) # Smart Camera - Low power consumption camera (duplicate of `sp`) SENSORS[DeviceCategory.DGHSXJ] = SENSORS[DeviceCategory.SP]