Skip to content

Commit 4805b33

Browse files
authored
Fix unit parsing in Tuya climate entities (home-assistant#157964)
1 parent c333036 commit 4805b33

File tree

5 files changed

+177
-2
lines changed

5 files changed

+177
-2
lines changed

homeassistant/components/tuya/climate.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ def _get_temperature_wrappers(
208208
device, DPCode.TEMP_SET_F, prefer_function=True
209209
)
210210

211+
# If there is a temp unit convert dpcode, override empty units
212+
if (
213+
temp_unit_convert := DPCodeEnumWrapper.find_dpcode(
214+
device, DPCode.TEMP_UNIT_CONVERT
215+
)
216+
) is not None:
217+
for wrapper in (temp_current, temp_current_f, temp_set, temp_set_f):
218+
if wrapper is not None and not wrapper.type_information.unit:
219+
wrapper.type_information.unit = temp_unit_convert.read_device_status(
220+
device
221+
)
222+
211223
# Get wrappers for celsius and fahrenheit
212224
# We need to check the unit of measurement
213225
current_celsius = _get_temperature_wrapper(

tests/components/tuya/conftest.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,23 @@ async def _create_device(hass: HomeAssistant, mock_device_code: str) -> Customer
182182
key: DeviceFunction(
183183
code=key,
184184
type=value["type"],
185-
values=json_dumps(value["value"]),
185+
values=(
186+
values
187+
if isinstance(values := value["value"], str)
188+
else json_dumps(values)
189+
),
186190
)
187191
for key, value in details["function"].items()
188192
}
189193
device.status_range = {
190194
key: DeviceStatusRange(
191195
code=key,
192196
type=value["type"],
193-
values=json_dumps(value["value"]),
197+
values=(
198+
values
199+
if isinstance(values := value["value"], str)
200+
else json_dumps(values)
201+
),
194202
)
195203
for key, value in details["status_range"].items()
196204
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"endpoint": "https://apigw.tuyaeu.com",
3+
"mqtt_connected": true,
4+
"disabled_by": null,
5+
"disabled_polling": false,
6+
"name": "ITC-308-WIFI Thermostat",
7+
"category": "wk",
8+
"product_id": "B0eP8qYAdpUo4yR9",
9+
"product_name": "ITC-308-WIFI Thermostat",
10+
"online": true,
11+
"sub": false,
12+
"time_zone": "+01:00",
13+
"active_time": "2022-02-08T10:49:39+00:00",
14+
"create_time": "2022-02-08T10:49:39+00:00",
15+
"update_time": "2022-02-08T10:49:39+00:00",
16+
"function": {
17+
"temp_unit_convert": {
18+
"type": "Enum",
19+
"value": "{\"range\":[\"c\",\"f\"]}"
20+
},
21+
"temp_set": {
22+
"type": "Integer",
23+
"value": "{\"unit\":\"\",\"min\":-400,\"max\":2120,\"scale\":1,\"step\":5}"
24+
}
25+
},
26+
"status_range": {
27+
"temp_unit_convert": {
28+
"type": "Enum",
29+
"value": "{\"range\":[\"c\",\"f\"]}"
30+
},
31+
"temp_current": {
32+
"type": "Integer",
33+
"value": "{\"unit\":\"\u2103\",\"min\":-500,\"max\":1200,\"scale\":1,\"step\":10}"
34+
},
35+
"temp_set": {
36+
"type": "Integer",
37+
"value": "{\"unit\":\"\",\"min\":-400,\"max\":2120,\"scale\":1,\"step\":5}"
38+
},
39+
"temp_current_f": {
40+
"type": "Integer",
41+
"value": "{\"unit\":\"\u2109\",\"min\":-500,\"max\":2480,\"scale\":1,\"step\":10}"
42+
}
43+
},
44+
"status": {
45+
"temp_unit_convert": "c",
46+
"temp_current": 340,
47+
"temp_set": 350,
48+
"temp_current_f": 932
49+
},
50+
"set_up": true,
51+
"support_local": true,
52+
"warnings": null
53+
}

tests/components/tuya/snapshots/test_climate.ambr

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,68 @@
654654
'state': 'off',
655655
})
656656
# ---
657+
# name: test_platform_setup_and_discovery[climate.itc_308_wifi_thermostat-entry]
658+
EntityRegistryEntrySnapshot({
659+
'aliases': set({
660+
}),
661+
'area_id': None,
662+
'capabilities': dict({
663+
'hvac_modes': list([
664+
]),
665+
'max_temp': 212.0,
666+
'min_temp': -40.0,
667+
'target_temp_step': 0.5,
668+
}),
669+
'config_entry_id': <ANY>,
670+
'config_subentry_id': <ANY>,
671+
'device_class': None,
672+
'device_id': <ANY>,
673+
'disabled_by': None,
674+
'domain': 'climate',
675+
'entity_category': None,
676+
'entity_id': 'climate.itc_308_wifi_thermostat',
677+
'has_entity_name': True,
678+
'hidden_by': None,
679+
'icon': None,
680+
'id': <ANY>,
681+
'labels': set({
682+
}),
683+
'name': None,
684+
'options': dict({
685+
}),
686+
'original_device_class': None,
687+
'original_icon': None,
688+
'original_name': None,
689+
'platform': 'tuya',
690+
'previous_unique_id': None,
691+
'suggested_object_id': None,
692+
'supported_features': <ClimateEntityFeature: 1>,
693+
'translation_key': None,
694+
'unique_id': 'tuya.9Ry4oUpdAYq8Pe0Bkw',
695+
'unit_of_measurement': None,
696+
})
697+
# ---
698+
# name: test_platform_setup_and_discovery[climate.itc_308_wifi_thermostat-state]
699+
StateSnapshot({
700+
'attributes': ReadOnlyDict({
701+
'current_temperature': 34.0,
702+
'friendly_name': 'ITC-308-WIFI Thermostat',
703+
'hvac_modes': list([
704+
]),
705+
'max_temp': 212.0,
706+
'min_temp': -40.0,
707+
'supported_features': <ClimateEntityFeature: 1>,
708+
'target_temp_step': 0.5,
709+
'temperature': 35.0,
710+
}),
711+
'context': <ANY>,
712+
'entity_id': 'climate.itc_308_wifi_thermostat',
713+
'last_changed': <ANY>,
714+
'last_reported': <ANY>,
715+
'last_updated': <ANY>,
716+
'state': 'off',
717+
})
718+
# ---
657719
# name: test_platform_setup_and_discovery[climate.kabinet-entry]
658720
EntityRegistryEntrySnapshot({
659721
'aliases': set({
@@ -1450,6 +1512,15 @@
14501512
'target_temp_step': 1.0,
14511513
})
14521514
# ---
1515+
# name: test_us_customary_system[climate.itc_308_wifi_thermostat]
1516+
ReadOnlyDict({
1517+
'current_temperature': 93,
1518+
'max_temp': 414,
1519+
'min_temp': -40,
1520+
'target_temp_step': 0.5,
1521+
'temperature': 95,
1522+
})
1523+
# ---
14531524
# name: test_us_customary_system[climate.kabinet]
14541525
ReadOnlyDict({
14551526
'current_temperature': 67,

tests/components/tuya/snapshots/test_init.ambr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,37 @@
13011301
'via_device_id': None,
13021302
})
13031303
# ---
1304+
# name: test_device_registry[9Ry4oUpdAYq8Pe0Bkw]
1305+
DeviceRegistryEntrySnapshot({
1306+
'area_id': None,
1307+
'config_entries': <ANY>,
1308+
'config_entries_subentries': <ANY>,
1309+
'configuration_url': None,
1310+
'connections': set({
1311+
}),
1312+
'disabled_by': None,
1313+
'entry_type': None,
1314+
'hw_version': None,
1315+
'id': <ANY>,
1316+
'identifiers': set({
1317+
tuple(
1318+
'tuya',
1319+
'9Ry4oUpdAYq8Pe0Bkw',
1320+
),
1321+
}),
1322+
'labels': set({
1323+
}),
1324+
'manufacturer': 'Tuya',
1325+
'model': 'ITC-308-WIFI Thermostat',
1326+
'model_id': 'B0eP8qYAdpUo4yR9',
1327+
'name': 'ITC-308-WIFI Thermostat',
1328+
'name_by_user': None,
1329+
'primary_config_entry': <ANY>,
1330+
'serial_number': None,
1331+
'sw_version': None,
1332+
'via_device_id': None,
1333+
})
1334+
# ---
13041335
# name: test_device_registry[9c1vlsxoscm]
13051336
DeviceRegistryEntrySnapshot({
13061337
'area_id': None,

0 commit comments

Comments
 (0)