From 3a6441fd9147d9abe91c53984e8683d128cf5704 Mon Sep 17 00:00:00 2001 From: Bander <46300268+xZetsubou@users.noreply.github.com> Date: Sun, 14 Jan 2024 00:09:50 +0300 Subject: [PATCH] Add Eletricity sensor and fix UoM for current xZetsubou/hass-localtuya#109 * Lights add scene data and colour data for "dd" category --- .../localtuya/core/ha_entities/base.py | 1 + .../localtuya/core/ha_entities/lights.py | 7 ++-- .../localtuya/core/ha_entities/sensors.py | 39 ++++++++++++++----- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/custom_components/localtuya/core/ha_entities/base.py b/custom_components/localtuya/core/ha_entities/base.py index cb7073ae9..d97cd24d4 100644 --- a/custom_components/localtuya/core/ha_entities/base.py +++ b/custom_components/localtuya/core/ha_entities/base.py @@ -98,6 +98,7 @@ class DPCode(StrEnum): https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq """ + ADD_ELE = "add_ele" AIR_QUALITY = "air_quality" ALARM_LOCK = "alarm_lock" ALARM_MESSAGE = "alarm_message" diff --git a/custom_components/localtuya/core/ha_entities/lights.py b/custom_components/localtuya/core/ha_entities/lights.py index 854a509e5..73dcc2ffb 100644 --- a/custom_components/localtuya/core/ha_entities/lights.py +++ b/custom_components/localtuya/core/ha_entities/lights.py @@ -66,9 +66,10 @@ def localtuya_light( id=DPCode.SWITCH_LED, name=None, color_mode=DPCode.WORK_MODE, - brightness=DPCode.BRIGHT_VALUE, - color_temp=DPCode.TEMP_VALUE, - color=DPCode.COLOUR_DATA, + brightness=(DPCode.BRIGHT_VALUE_V2, DPCode.BRIGHT_VALUE), + color_temp=(DPCode.TEMP_VALUE_V2, DPCode.TEMP_VALUE), + color=(DPCode.COLOUR_DATA_V2, DPCode.COLOUR_DATA), + scene=(DPCode.SCENE_DATA_V2, DPCode.SCENE_DATA), custom_configs=localtuya_light(29, 1000, 2700, 6500, False, False) # default_color_type=DEFAULT_COLOR_TYPE_DATA_V2, ), diff --git a/custom_components/localtuya/core/ha_entities/sensors.py b/custom_components/localtuya/core/ha_entities/sensors.py index 7022212e9..26466b28c 100644 --- a/custom_components/localtuya/core/ha_entities/sensors.py +++ b/custom_components/localtuya/core/ha_entities/sensors.py @@ -17,6 +17,7 @@ UnitOfTime, CONF_UNIT_OF_MEASUREMENT, UnitOfTemperature, + UnitOfEnergy, ) from .base import DPCode, LocalTuyaEntity, CONF_DEVICE_CLASS, EntityCategory @@ -353,7 +354,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.CUR_VOLTAGE, @@ -362,6 +363,12 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di state_class=SensorStateClass.MEASUREMENT, custom_configs=localtuya_sensor(UnitOfElectricPotential.VOLT, 0.1), ), + LocalTuyaEntity( + id=DPCode.ADD_ELE, + name="Electricity", + device_class=SensorDeviceClass.ENERGY, + custom_configs=localtuya_sensor(UnitOfEnergy.KILO_WATT_HOUR, 0.001), + ), ), # IoT Switch # Note: Undocumented @@ -379,7 +386,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), # entity_registry_enabled_default=False, ), LocalTuyaEntity( @@ -390,6 +397,12 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di custom_configs=localtuya_sensor(UnitOfElectricPotential.VOLT, 0.1), # entity_registry_enabled_default=False, ), + LocalTuyaEntity( + id=DPCode.ADD_ELE, + name="Electricity", + device_class=SensorDeviceClass.ENERGY, + custom_configs=localtuya_sensor(UnitOfEnergy.KILO_WATT_HOUR, 0.001), + ), ), # Luminance Sensor # https://developer.tuya.com/en/docs/iot/categoryldcg?id=Kaiuz3n7u69l8 @@ -530,7 +543,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), ), # Gas Detector @@ -716,7 +729,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase C Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_A, @@ -737,7 +750,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase B Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_B, @@ -758,7 +771,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase C Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_C, @@ -789,7 +802,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase C Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_A, @@ -810,7 +823,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase B Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_B, @@ -831,7 +844,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di name="Phase C Power", device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), ), LocalTuyaEntity( id=DPCode.PHASE_C, @@ -1037,7 +1050,7 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, - custom_configs=localtuya_sensor(UnitOfPower.KILO_WATT, 0.1), + custom_configs=localtuya_sensor(UnitOfPower.WATT, 0.1), # entity_registry_enabled_default=False, ), LocalTuyaEntity( @@ -1049,6 +1062,12 @@ def localtuya_sensor(unit_of_measurement=None, scale_factor: float = None) -> di custom_configs=localtuya_sensor(UnitOfElectricPotential.VOLT, 0.1), # entity_registry_enabled_default=False, ), + LocalTuyaEntity( + id=DPCode.ADD_ELE, + name="Electricity", + device_class=SensorDeviceClass.ENERGY, + custom_configs=localtuya_sensor(UnitOfEnergy.KILO_WATT_HOUR, 0.001), + ), ), # Dehumidifier # https://developer.tuya.com/en/docs/iot/s?id=K9gf48r6jke8e