Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions homeassistant/components/tuya/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ class DPCode(StrEnum):
COLOUR_DATA = "colour_data" # Colored light mode
COLOUR_DATA_HSV = "colour_data_hsv" # Colored light mode
COLOUR_DATA_V2 = "colour_data_v2" # Colored light mode
COMPRESSOR_STRENGTH = "compressor_strength"
CONCENTRATION_SET = "concentration_set" # Concentration setting
CONTROL = "control"
CONTROL_2 = "control_2"
Expand Down Expand Up @@ -911,8 +912,10 @@ class DPCode(StrEnum):
TARGET_DIS_CLOSEST = "target_dis_closest" # Closest target distance
TDS_IN = "tds_in" # Total dissolved solids
TEMP = "temp" # Temperature setting
TEMP_AROUND = "temp_around" # Current around (outside) temperature
TEMP_BOILING_C = "temp_boiling_c"
TEMP_BOILING_F = "temp_boiling_f"
TEMP_COILER = "temp_coiler" # Current coiler temperature
TEMP_CONTROLLER = "temp_controller"
TEMP_CORRECTION = "temp_correction"
TEMP_CURRENT = "temp_current" # Current temperature in °C
Expand All @@ -933,9 +936,11 @@ class DPCode(StrEnum):
"temp_current_external_f" # Current external temperature in Fahrenheit
)
TEMP_CURRENT_F = "temp_current_f" # Current temperature in °F
TEMP_EFFLUENT = "temp_effluent" # Current flow temperature
TEMP_INDOOR = "temp_indoor" # Indoor temperature in °C
TEMP_SET = "temp_set" # Set the temperature in °C
TEMP_SET_F = "temp_set_f" # Set the temperature in °F
TEMP_VENTING = "temp_venting" # Current heat plate temperature
TEMP_UNIT_CONVERT = "temp_unit_convert" # Temperature unit switching
TEMP_VALUE = "temp_value" # Color temperature
Comment thread
tbouron marked this conversation as resolved.
Outdated
TEMP_VALUE_V2 = "temp_value_v2"
Expand Down
67 changes: 61 additions & 6 deletions homeassistant/components/tuya/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Support for Tuya sensors."""

from dataclasses import dataclass
from dataclasses import dataclass, replace

from tuya_device_handlers.definition.sensor import (
SensorDefinition,
Expand Down Expand Up @@ -38,6 +38,7 @@
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfPower,
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant, callback
Expand All @@ -48,6 +49,7 @@
from .const import (
DEVICE_CLASS_UNITS,
DOMAIN,
FAHRENHEIT_ALIASES,
LOGGER,
TUYA_DISCOVERY_NEW,
DeviceCategory,
Expand Down Expand Up @@ -1613,12 +1615,42 @@ class TuyaSensorEntityDescription(SensorEntityDescription):
),
),
DeviceCategory.ZNRB: (
TuyaSensorEntityDescription(
key=DPCode.COMPRESSOR_STRENGTH,
translation_key="compressor_strength",
Comment thread
tbouron marked this conversation as resolved.
state_class=SensorStateClass.MEASUREMENT,
Comment thread
epenet marked this conversation as resolved.
native_unit_of_measurement=PERCENTAGE,
Comment thread
tbouron marked this conversation as resolved.
Outdated
Comment thread
tbouron marked this conversation as resolved.
Outdated
Comment thread
tbouron marked this conversation as resolved.
Outdated
),
TuyaSensorEntityDescription(
key=DPCode.TEMP_AROUND,
translation_key="outside_temperature",
Comment thread
tbouron marked this conversation as resolved.
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
Comment thread
tbouron marked this conversation as resolved.
TuyaSensorEntityDescription(
key=DPCode.TEMP_COILER,
translation_key="coiler_temperature",
Comment thread
tbouron marked this conversation as resolved.
Outdated
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
Comment thread
tbouron marked this conversation as resolved.
Comment thread
epenet marked this conversation as resolved.
TuyaSensorEntityDescription(
key=DPCode.TEMP_CURRENT,
translation_key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
Comment thread
tbouron marked this conversation as resolved.
),
TuyaSensorEntityDescription(
Comment thread
epenet marked this conversation as resolved.
key=DPCode.TEMP_EFFLUENT,
translation_key="flow_temperature",
Comment thread
tbouron marked this conversation as resolved.
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
Comment thread
tbouron marked this conversation as resolved.
TuyaSensorEntityDescription(
key=DPCode.TEMP_VENTING,
translation_key="heat_exchanger_temperature",
Comment thread
tbouron marked this conversation as resolved.
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
Comment thread
tbouron marked this conversation as resolved.
Comment thread
tbouron marked this conversation as resolved.
Comment thread
epenet marked this conversation as resolved.
),
DeviceCategory.ZWJCY: (
TuyaSensorEntityDescription(
Expand Down Expand Up @@ -1647,6 +1679,19 @@ class TuyaSensorEntityDescription(SensorEntityDescription):
SENSORS[DeviceCategory.PC] = SENSORS[DeviceCategory.KG]


def _get_temp_unit_from_device(
device: CustomerDevice,
) -> UnitOfTemperature | None:
"""Return the temperature unit reported by the device via TEMP_UNIT_CONVERT, or None."""
unit_value = device.status.get(DPCode.TEMP_UNIT_CONVERT)
if unit_value is None:
return None
unit_str = str(unit_value).lower()
if unit_str in FAHRENHEIT_ALIASES:
return UnitOfTemperature.FAHRENHEIT
return UnitOfTemperature.CELSIUS


async def async_setup_entry(
hass: HomeAssistant,
entry: TuyaConfigEntry,
Expand All @@ -1662,17 +1707,27 @@ def async_discover_device(device_ids: list[str]) -> None:
for device_id in device_ids:
device = manager.device_map[device_id]
if descriptions := SENSORS.get(device.category):
entities.extend(
TuyaSensorEntity(device, manager, description, definition)
for description in descriptions
if (
temp_unit = _get_temp_unit_from_device(device)
for description in descriptions:
if not (
definition := get_default_definition(
device,
description.dpcode or description.key,
description.wrapper_class,
)
):
continue
if (
temp_unit is not None
and description.device_class == SensorDeviceClass.TEMPERATURE
and description.native_unit_of_measurement is None
):
description = replace(
description, native_unit_of_measurement=temp_unit
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #170338 - which I think should be merged first.

I think this should then be handled inside _validate_device_class_unit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, that makes sense. I'll rebase on your PR locally and make that change so it can be ready once yours is merged. Thank you @epenet

entities.append(
TuyaSensorEntity(device, manager, description, definition)
)
)

async_add_entities(entities)

Expand Down
15 changes: 15 additions & 0 deletions homeassistant/components/tuya/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@
"cleaning_time": {
"name": "Cleaning time"
},
"coiler_temperature": {
"name": "Coiler temperature"
Comment thread
tbouron marked this conversation as resolved.
Outdated
},
"compressor_strength": {
"name": "Compressor strength"
},
"concentration_carbon_dioxide": {
"name": "Concentration of carbon dioxide"
},
Expand Down Expand Up @@ -691,12 +697,18 @@
"filter_utilization": {
"name": "Filter utilization"
},
"flow_temperature": {
"name": "Flow temperature"
},
"formaldehyde": {
"name": "[%key:component::tuya::entity::binary_sensor::formaldehyde::name%]"
},
"gas": {
"name": "Gas"
},
"heat_exchanger_temperature": {
"name": "Heat exchanger temperature"
},
"heat_index_temperature": {
"name": "Heat index"
},
Expand Down Expand Up @@ -779,6 +791,9 @@
"work": "Working"
}
},
"outside_temperature": {
"name": "Outside temperature"
},
"oxydo_reduction_potential": {
"name": "Oxydo reduction potential"
},
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/tuya/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@
),
),
DeviceCategory.ZNRB: (
SwitchEntityDescription(
key=DPCode.CHILD_LOCK,
translation_key="child_lock",
icon="mdi:account-lock",
Comment thread
tbouron marked this conversation as resolved.
),
SwitchEntityDescription(
key=DPCode.SWITCH,
translation_key="switch",
Expand Down
Loading
Loading