Add energy consumption/production for Tuya kg category (smart switches) (#149234)

pull/151612/head
yufeng 2025-09-03 17:47:23 +08:00 committed by GitHub
parent c12b638b3d
commit 34c061df19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 1944 additions and 0 deletions

View File

@ -298,6 +298,7 @@ class DPCode(StrEnum):
PRESENCE_STATE = "presence_state"
PRESSURE_STATE = "pressure_state"
PRESSURE_VALUE = "pressure_value"
PRO_ADD_ELE = "pro_add_ele" # Produce energy
PUMP = "pump"
PUMP_RESET = "pump_reset" # Water pump reset
PUMP_TIME = "pump_time" # Water pump duration

View File

@ -680,6 +680,18 @@ SENSORS: dict[str, tuple[TuyaSensorEntityDescription, ...]] = {
suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
entity_registry_enabled_default=False,
),
TuyaSensorEntityDescription(
key=DPCode.ADD_ELE,
translation_key="total_energy",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
TuyaSensorEntityDescription(
key=DPCode.PRO_ADD_ELE,
translation_key="total_production",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
),
# Air Purifier
# https://developer.tuya.com/en/docs/iot/s?id=K9gf48r41mn81

View File

@ -66,6 +66,7 @@ DEVICE_MOCKS = [
"cz_t0a4hwsf8anfsadp", # https://github.com/home-assistant/core/issues/149704
"cz_tf6qp8t3hl9h7m94", # https://github.com/home-assistant/core/issues/143209
"cz_tkn2s79mzedk6pwr", # https://github.com/home-assistant/core/issues/146164
"cz_vrbpx6h7fsi5mujb", # https://github.com/home-assistant/core/pull/149234
"cz_vxqn72kwtosoy4d3", # https://github.com/home-assistant/core/issues/141278
"cz_w0qqde0g", # https://github.com/orgs/home-assistant/discussions/482
"cz_wifvoilfrqeo6hvu", # https://github.com/home-assistant/core/issues/146164

View File

@ -0,0 +1,223 @@
{
"endpoint": "https://apigw.tuyacn.com",
"mqtt_connected": true,
"disabled_by": null,
"disabled_polling": false,
"name": "\u63a5HA\u53cc\u5411\u8ba1\u91cf\u63d2\u5ea7",
"category": "cz",
"product_id": "vrbpx6h7fsi5mujb",
"product_name": "\u63a5HA\u53cc\u5411\u8ba1\u91cf\u63d2\u5ea7",
"online": true,
"sub": false,
"time_zone": "+08:00",
"active_time": "2025-07-17T09:18:54+00:00",
"create_time": "2025-07-17T09:18:54+00:00",
"update_time": "2025-07-17T09:18:54+00:00",
"function": {
"switch_1": {
"type": "Boolean",
"value": {}
},
"countdown_1": {
"type": "Integer",
"value": {
"min": 0,
"max": 43200,
"scale": 0,
"step": 1
}
},
"relay_status": {
"type": "Enum",
"value": {
"range": ["power_off", "power_on", "last"]
}
},
"light_mode": {
"type": "Enum",
"value": {
"range": ["relay", "pos", "none"]
}
},
"child_lock": {
"type": "Boolean",
"value": {}
},
"cycle_time": {
"type": "String",
"value": {}
},
"random_time": {
"type": "String",
"value": {}
},
"switch_inching": {
"type": "String",
"value": {}
}
},
"status_range": {
"switch_1": {
"type": "Boolean",
"value": {}
},
"countdown_1": {
"type": "Integer",
"value": {
"min": 0,
"max": 43200,
"scale": 0,
"step": 1
}
},
"add_ele": {
"type": "Integer",
"value": {
"unit": "kW\u00b7h",
"min": 0,
"max": 999999999,
"scale": 3,
"step": 100
}
},
"cur_current": {
"type": "Integer",
"value": {
"unit": "mA",
"min": 0,
"max": 30000,
"scale": 0,
"step": 1
}
},
"cur_power": {
"type": "Integer",
"value": {
"unit": "W",
"min": 0,
"max": 80000,
"scale": 1,
"step": 1
}
},
"cur_voltage": {
"type": "Integer",
"value": {
"unit": "V",
"min": 0,
"max": 5000,
"scale": 1,
"step": 1
}
},
"voltage_coe": {
"type": "Integer",
"value": {
"min": 0,
"max": 1000000,
"scale": 0,
"step": 1
}
},
"electric_coe": {
"type": "Integer",
"value": {
"min": 0,
"max": 1000000,
"scale": 0,
"step": 1
}
},
"power_coe": {
"type": "Integer",
"value": {
"min": 0,
"max": 1000000,
"scale": 0,
"step": 1
}
},
"electricity_coe": {
"type": "Integer",
"value": {
"min": 0,
"max": 1000000,
"scale": 0,
"step": 1
}
},
"fault": {
"type": "Bitmap",
"value": {
"label": ["ov_cr", "ov_vol", "ov_pwr", "ls_cr", "ls_vol", "ls_pow"]
}
},
"relay_status": {
"type": "Enum",
"value": {
"range": ["power_off", "power_on", "last"]
}
},
"light_mode": {
"type": "Enum",
"value": {
"range": ["relay", "pos", "none"]
}
},
"child_lock": {
"type": "Boolean",
"value": {}
},
"cycle_time": {
"type": "String",
"value": {}
},
"random_time": {
"type": "String",
"value": {}
},
"switch_inching": {
"type": "String",
"value": {}
},
"energy_status": {
"type": "Enum",
"value": {
"range": ["consumption", "production"]
}
},
"pro_add_ele": {
"type": "Integer",
"value": {
"unit": "kW\u00b7h",
"min": 0,
"max": 999999999,
"scale": 3,
"step": 100
}
}
},
"status": {
"switch_1": false,
"countdown_1": 0,
"add_ele": 900,
"cur_current": 0,
"cur_power": 0,
"cur_voltage": 0,
"voltage_coe": 0,
"electric_coe": 0,
"power_coe": 0,
"electricity_coe": 0,
"fault": 0,
"relay_status": "power_off",
"light_mode": "relay",
"child_lock": false,
"cycle_time": "",
"random_time": "",
"switch_inching": "",
"energy_status": "consumption",
"pro_add_ele": 1100
},
"set_up": true,
"support_local": true
}

View File

@ -1828,6 +1828,37 @@
'via_device_id': None,
})
# ---
# name: test_device_registry[bjum5isf7h6xpbrvzc]
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'config_entries_subentries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': None,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'tuya',
'bjum5isf7h6xpbrvzc',
),
}),
'labels': set({
}),
'manufacturer': 'Tuya',
'model': '接HA双向计量插座',
'model_id': 'vrbpx6h7fsi5mujb',
'name': '接HA双向计量插座',
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_device_registry[bl5cuqxnqzkfs]
DeviceRegistryEntrySnapshot({
'area_id': None,

View File

@ -2778,6 +2778,124 @@
'state': 'unknown',
})
# ---
# name: test_platform_setup_and_discovery[select.jie_hashuang_xiang_ji_liang_cha_zuo_indicator_light_mode-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'options': list([
'relay',
'pos',
'none',
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'select',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'select.jie_hashuang_xiang_ji_liang_cha_zuo_indicator_light_mode',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Indicator light mode',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'light_mode',
'unique_id': 'tuya.bjum5isf7h6xpbrvzclight_mode',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[select.jie_hashuang_xiang_ji_liang_cha_zuo_indicator_light_mode-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': '接HA双向计量插座 Indicator light mode',
'options': list([
'relay',
'pos',
'none',
]),
}),
'context': <ANY>,
'entity_id': 'select.jie_hashuang_xiang_ji_liang_cha_zuo_indicator_light_mode',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'relay',
})
# ---
# name: test_platform_setup_and_discovery[select.jie_hashuang_xiang_ji_liang_cha_zuo_power_on_behavior-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'options': list([
'power_off',
'power_on',
'last',
]),
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'select',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'select.jie_hashuang_xiang_ji_liang_cha_zuo_power_on_behavior',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Power on behavior',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'relay_status',
'unique_id': 'tuya.bjum5isf7h6xpbrvzcrelay_status',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[select.jie_hashuang_xiang_ji_liang_cha_zuo_power_on_behavior-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': '接HA双向计量插座 Power on behavior',
'options': list([
'power_off',
'power_on',
'last',
]),
}),
'context': <ANY>,
'entity_id': 'select.jie_hashuang_xiang_ji_liang_cha_zuo_power_on_behavior',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'power_off',
})
# ---
# name: test_platform_setup_and_discovery[select.kalado_air_purifier_countdown-entry]
EntityRegistryEntrySnapshot({
'aliases': set({

File diff suppressed because it is too large Load Diff

View File

@ -4739,6 +4739,103 @@
'state': 'off',
})
# ---
# name: test_platform_setup_and_discovery[switch.jie_hashuang_xiang_ji_liang_cha_zuo_child_lock-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.jie_hashuang_xiang_ji_liang_cha_zuo_child_lock',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Child lock',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'child_lock',
'unique_id': 'tuya.bjum5isf7h6xpbrvzcchild_lock',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[switch.jie_hashuang_xiang_ji_liang_cha_zuo_child_lock-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': '接HA双向计量插座 Child lock',
}),
'context': <ANY>,
'entity_id': 'switch.jie_hashuang_xiang_ji_liang_cha_zuo_child_lock',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_platform_setup_and_discovery[switch.jie_hashuang_xiang_ji_liang_cha_zuo_socket_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.jie_hashuang_xiang_ji_liang_cha_zuo_socket_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': 'Socket 1',
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'indexed_socket',
'unique_id': 'tuya.bjum5isf7h6xpbrvzcswitch_1',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[switch.jie_hashuang_xiang_ji_liang_cha_zuo_socket_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'outlet',
'friendly_name': '接HA双向计量插座 Socket 1',
}),
'context': <ANY>,
'entity_id': 'switch.jie_hashuang_xiang_ji_liang_cha_zuo_socket_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_platform_setup_and_discovery[switch.kabinet_child_lock-entry]
EntityRegistryEntrySnapshot({
'aliases': set({