Fix unit parsing in Tuya climate entities (#157964)

This commit is contained in:
epenet
2025-12-04 16:17:39 +01:00
committed by GitHub
parent c333036959
commit 4805b33a27
5 changed files with 177 additions and 2 deletions

View File

@@ -208,6 +208,18 @@ def _get_temperature_wrappers(
device, DPCode.TEMP_SET_F, prefer_function=True
)
# If there is a temp unit convert dpcode, override empty units
if (
temp_unit_convert := DPCodeEnumWrapper.find_dpcode(
device, DPCode.TEMP_UNIT_CONVERT
)
) is not None:
for wrapper in (temp_current, temp_current_f, temp_set, temp_set_f):
if wrapper is not None and not wrapper.type_information.unit:
wrapper.type_information.unit = temp_unit_convert.read_device_status(
device
)
# Get wrappers for celsius and fahrenheit
# We need to check the unit of measurement
current_celsius = _get_temperature_wrapper(

View File

@@ -182,7 +182,11 @@ async def _create_device(hass: HomeAssistant, mock_device_code: str) -> Customer
key: DeviceFunction(
code=key,
type=value["type"],
values=json_dumps(value["value"]),
values=(
values
if isinstance(values := value["value"], str)
else json_dumps(values)
),
)
for key, value in details["function"].items()
}
@@ -190,7 +194,11 @@ async def _create_device(hass: HomeAssistant, mock_device_code: str) -> Customer
key: DeviceStatusRange(
code=key,
type=value["type"],
values=json_dumps(value["value"]),
values=(
values
if isinstance(values := value["value"], str)
else json_dumps(values)
),
)
for key, value in details["status_range"].items()
}

View File

@@ -0,0 +1,53 @@
{
"endpoint": "https://apigw.tuyaeu.com",
"mqtt_connected": true,
"disabled_by": null,
"disabled_polling": false,
"name": "ITC-308-WIFI Thermostat",
"category": "wk",
"product_id": "B0eP8qYAdpUo4yR9",
"product_name": "ITC-308-WIFI Thermostat",
"online": true,
"sub": false,
"time_zone": "+01:00",
"active_time": "2022-02-08T10:49:39+00:00",
"create_time": "2022-02-08T10:49:39+00:00",
"update_time": "2022-02-08T10:49:39+00:00",
"function": {
"temp_unit_convert": {
"type": "Enum",
"value": "{\"range\":[\"c\",\"f\"]}"
},
"temp_set": {
"type": "Integer",
"value": "{\"unit\":\"\",\"min\":-400,\"max\":2120,\"scale\":1,\"step\":5}"
}
},
"status_range": {
"temp_unit_convert": {
"type": "Enum",
"value": "{\"range\":[\"c\",\"f\"]}"
},
"temp_current": {
"type": "Integer",
"value": "{\"unit\":\"\u2103\",\"min\":-500,\"max\":1200,\"scale\":1,\"step\":10}"
},
"temp_set": {
"type": "Integer",
"value": "{\"unit\":\"\",\"min\":-400,\"max\":2120,\"scale\":1,\"step\":5}"
},
"temp_current_f": {
"type": "Integer",
"value": "{\"unit\":\"\u2109\",\"min\":-500,\"max\":2480,\"scale\":1,\"step\":10}"
}
},
"status": {
"temp_unit_convert": "c",
"temp_current": 340,
"temp_set": 350,
"temp_current_f": 932
},
"set_up": true,
"support_local": true,
"warnings": null
}

View File

@@ -654,6 +654,68 @@
'state': 'off',
})
# ---
# name: test_platform_setup_and_discovery[climate.itc_308_wifi_thermostat-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'hvac_modes': list([
]),
'max_temp': 212.0,
'min_temp': -40.0,
'target_temp_step': 0.5,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'climate',
'entity_category': None,
'entity_id': 'climate.itc_308_wifi_thermostat',
'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': None,
'platform': 'tuya',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <ClimateEntityFeature: 1>,
'translation_key': None,
'unique_id': 'tuya.9Ry4oUpdAYq8Pe0Bkw',
'unit_of_measurement': None,
})
# ---
# name: test_platform_setup_and_discovery[climate.itc_308_wifi_thermostat-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'current_temperature': 34.0,
'friendly_name': 'ITC-308-WIFI Thermostat',
'hvac_modes': list([
]),
'max_temp': 212.0,
'min_temp': -40.0,
'supported_features': <ClimateEntityFeature: 1>,
'target_temp_step': 0.5,
'temperature': 35.0,
}),
'context': <ANY>,
'entity_id': 'climate.itc_308_wifi_thermostat',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_platform_setup_and_discovery[climate.kabinet-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
@@ -1450,6 +1512,15 @@
'target_temp_step': 1.0,
})
# ---
# name: test_us_customary_system[climate.itc_308_wifi_thermostat]
ReadOnlyDict({
'current_temperature': 93,
'max_temp': 414,
'min_temp': -40,
'target_temp_step': 0.5,
'temperature': 95,
})
# ---
# name: test_us_customary_system[climate.kabinet]
ReadOnlyDict({
'current_temperature': 67,

View File

@@ -1301,6 +1301,37 @@
'via_device_id': None,
})
# ---
# name: test_device_registry[9Ry4oUpdAYq8Pe0Bkw]
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',
'9Ry4oUpdAYq8Pe0Bkw',
),
}),
'labels': set({
}),
'manufacturer': 'Tuya',
'model': 'ITC-308-WIFI Thermostat',
'model_id': 'B0eP8qYAdpUo4yR9',
'name': 'ITC-308-WIFI Thermostat',
'name_by_user': None,
'primary_config_entry': <ANY>,
'serial_number': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_device_registry[9c1vlsxoscm]
DeviceRegistryEntrySnapshot({
'area_id': None,