Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new temperature_delta device class #139024

Draft
wants to merge 18 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 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
8 changes: 8 additions & 0 deletions homeassistant/components/number/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
UnitOfSoundPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTemperatureDelta,
UnitOfTime,
UnitOfVolume,
UnitOfVolumeFlowRate,
Expand Down Expand Up @@ -359,6 +360,12 @@ class NumberDeviceClass(StrEnum):
Unit of measurement: `°C`, `°F`, `K`
"""

TEMPERATURE_DELTA = "temperature_delta"
"""Temperature difference.

Unit of measurement: `°C`, `°F`, `K`
"""

VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds"
"""Amount of VOC.

Expand Down Expand Up @@ -504,6 +511,7 @@ class NumberDeviceClass(StrEnum):
NumberDeviceClass.SPEED: {*UnitOfSpeed, *UnitOfVolumetricFlux},
NumberDeviceClass.SULPHUR_DIOXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
NumberDeviceClass.TEMPERATURE: set(UnitOfTemperature),
NumberDeviceClass.TEMPERATURE_DELTA: set(UnitOfTemperatureDelta),
NumberDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: {
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
},
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/number/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
"temperature": {
"default": "mdi:thermometer"
},
"temperature_delta": {
"default": "mdi:thermometer"
},
"volatile_organic_compounds": {
"default": "mdi:molecule"
},
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/number/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@
"temperature": {
"name": "[%key:component::sensor::entity_component::temperature::name%]"
},
"temperature_delta": {
"name": "[%key:component::sensor::entity_component::temperature_delta::name%]"
},
"volatile_organic_compounds": {
"name": "[%key:component::sensor::entity_component::volatile_organic_compounds::name%]"
},
Expand Down
11 changes: 11 additions & 0 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
UnitOfSoundPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTemperatureDelta,
UnitOfTime,
UnitOfVolume,
UnitOfVolumeFlowRate,
Expand All @@ -60,6 +61,7 @@
PressureConverter,
SpeedConverter,
TemperatureConverter,
TemperatureDeltaConverter,
UnitlessRatioConverter,
VolumeConverter,
VolumeFlowRateConverter,
Expand Down Expand Up @@ -389,6 +391,12 @@ class SensorDeviceClass(StrEnum):
Unit of measurement: `°C`, `°F`, `K`
"""

TEMPERATURE_DELTA = "temperature_delta"
"""Temperature difference.

Unit of measurement: `°C`, `°F`, `K`
"""

VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds"
"""Amount of VOC.

Expand Down Expand Up @@ -528,6 +536,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.PRESSURE: PressureConverter,
SensorDeviceClass.SPEED: SpeedConverter,
SensorDeviceClass.TEMPERATURE: TemperatureConverter,
SensorDeviceClass.TEMPERATURE_DELTA: TemperatureDeltaConverter,
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: UnitlessRatioConverter,
SensorDeviceClass.VOLTAGE: ElectricPotentialConverter,
SensorDeviceClass.VOLUME: VolumeConverter,
Expand Down Expand Up @@ -600,6 +609,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.SPEED: {*UnitOfSpeed, *UnitOfVolumetricFlux},
SensorDeviceClass.SULPHUR_DIOXIDE: {CONCENTRATION_MICROGRAMS_PER_CUBIC_METER},
SensorDeviceClass.TEMPERATURE: set(UnitOfTemperature),
SensorDeviceClass.TEMPERATURE_DELTA: set(UnitOfTemperatureDelta),
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: {
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
},
Expand Down Expand Up @@ -672,6 +682,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.SPEED: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.SULPHUR_DIOXIDE: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.TEMPERATURE: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.TEMPERATURE_DELTA: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.TIMESTAMP: set(),
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: {SensorStateClass.MEASUREMENT},
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/sensor/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
CONF_IS_SOUND_PRESSURE = "is_sound_pressure"
CONF_IS_SULPHUR_DIOXIDE = "is_sulphur_dioxide"
CONF_IS_TEMPERATURE = "is_temperature"
CONF_IS_TEMPERATURE_DELTA = "is_temperature_delta"
CONF_IS_VALUE = "is_value"
CONF_IS_VOLATILE_ORGANIC_COMPOUNDS = "is_volatile_organic_compounds"
CONF_IS_VOLATILE_ORGANIC_COMPOUNDS_PARTS = "is_volatile_organic_compounds_parts"
Expand Down Expand Up @@ -134,6 +135,7 @@
SensorDeviceClass.SPEED: [{CONF_TYPE: CONF_IS_SPEED}],
SensorDeviceClass.SULPHUR_DIOXIDE: [{CONF_TYPE: CONF_IS_SULPHUR_DIOXIDE}],
SensorDeviceClass.TEMPERATURE: [{CONF_TYPE: CONF_IS_TEMPERATURE}],
SensorDeviceClass.TEMPERATURE_DELTA: [{CONF_TYPE: CONF_IS_TEMPERATURE_DELTA}],
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: [
{CONF_TYPE: CONF_IS_VOLATILE_ORGANIC_COMPOUNDS}
],
Expand Down Expand Up @@ -199,6 +201,7 @@
CONF_IS_SPEED,
CONF_IS_SULPHUR_DIOXIDE,
CONF_IS_TEMPERATURE,
CONF_IS_TEMPERATURE_DELTA,
CONF_IS_VOLATILE_ORGANIC_COMPOUNDS,
CONF_IS_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
CONF_IS_VOLTAGE,
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/sensor/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
CONF_SPEED = "speed"
CONF_SULPHUR_DIOXIDE = "sulphur_dioxide"
CONF_TEMPERATURE = "temperature"
CONF_TEMPERATURE_DELTA = "temperature_delta"
CONF_VALUE = "value"
CONF_VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds"
CONF_VOLATILE_ORGANIC_COMPOUNDS_PARTS = "volatile_organic_compounds_parts"
Expand Down Expand Up @@ -133,6 +134,7 @@
SensorDeviceClass.SPEED: [{CONF_TYPE: CONF_SPEED}],
SensorDeviceClass.SULPHUR_DIOXIDE: [{CONF_TYPE: CONF_SULPHUR_DIOXIDE}],
SensorDeviceClass.TEMPERATURE: [{CONF_TYPE: CONF_TEMPERATURE}],
SensorDeviceClass.TEMPERATURE_DELTA: [{CONF_TYPE: CONF_TEMPERATURE_DELTA}],
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: [
{CONF_TYPE: CONF_VOLATILE_ORGANIC_COMPOUNDS}
],
Expand Down Expand Up @@ -199,6 +201,7 @@
CONF_SPEED,
CONF_SULPHUR_DIOXIDE,
CONF_TEMPERATURE,
CONF_TEMPERATURE_DELTA,
CONF_VOLATILE_ORGANIC_COMPOUNDS,
CONF_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
CONF_VOLTAGE,
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/sensor/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
"temperature": {
"default": "mdi:thermometer"
},
"temperature_delta": {
"default": "mdi:thermometer"
},
"timestamp": {
"default": "mdi:clock"
},
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/sensor/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"is_speed": "Current {entity_name} speed",
"is_sulphur_dioxide": "Current {entity_name} sulphur dioxide concentration level",
"is_temperature": "Current {entity_name} temperature",
"is_temperature_delta": "Current {entity_name} temperature delta",
"is_value": "Current {entity_name} value",
"is_volatile_organic_compounds": "Current {entity_name} volatile organic compounds concentration level",
"is_volatile_organic_compounds_parts": "[%key:component::sensor::device_automation::condition_type::is_volatile_organic_compounds%]",
Expand Down Expand Up @@ -98,6 +99,7 @@
"speed": "{entity_name} speed changes",
"sulphur_dioxide": "{entity_name} sulphur dioxide concentration changes",
"temperature": "{entity_name} temperature changes",
"temperature_delta": "{entity_name} temperature delta changes",
"value": "{entity_name} value changes",
"volatile_organic_compounds": "{entity_name} volatile organic compounds concentration changes",
"volatile_organic_compounds_parts": "[%key:component::sensor::device_automation::trigger_type::volatile_organic_compounds%]",
Expand Down Expand Up @@ -274,6 +276,9 @@
"temperature": {
"name": "Temperature"
},
"temperature_delta": {
"name": "Temperature delta"
},
"timestamp": {
"name": "Timestamp"
},
Expand Down
10 changes: 10 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ class UnitOfTemperature(StrEnum):
KELVIN = "K"


# Temperature delta units
class UnitOfTemperatureDelta(StrEnum):
"""Temperature delta units."""

CELSIUS = "°C"
FAHRENHEIT = "°F"
KELVIN = "K"


# Time units
class UnitOfTime(StrEnum):
"""Time units."""
Expand Down Expand Up @@ -1073,6 +1082,7 @@ class UnitOfDataRate(StrEnum):
PRESSURE: Final = "pressure"
VOLUME: Final = "volume"
TEMPERATURE: Final = "temperature"
TEMPERATURE_DELTA: Final = "temperature_delta"
SPEED: Final = "speed"
WIND_SPEED: Final = "wind_speed"
ILLUMINANCE: Final = "illuminance"
Expand Down
18 changes: 18 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTemperatureDelta,
UnitOfTime,
UnitOfVolume,
UnitOfVolumeFlowRate,
Expand Down Expand Up @@ -647,6 +648,23 @@ def _celsius_to_kelvin(cls, celsius: float) -> float:
return celsius + 273.15


class TemperatureDeltaConverter(BaseUnitConverter):
"""Utility to convert temperature delta values."""

UNIT_CLASS = "temperature_delta"
VALID_UNITS = {
UnitOfTemperatureDelta.CELSIUS,
UnitOfTemperatureDelta.FAHRENHEIT,
UnitOfTemperatureDelta.KELVIN,
}
_UNIT_CONVERSION = {
UnitOfTemperatureDelta.CELSIUS: 1.0,
UnitOfTemperatureDelta.FAHRENHEIT: 1.8,
UnitOfTemperatureDelta.KELVIN: 1.0,
}
VALID_UNITS = set(UnitOfTemperatureDelta)


class UnitlessRatioConverter(BaseUnitConverter):
"""Utility to convert unitless ratios."""

Expand Down
21 changes: 21 additions & 0 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
MASS,
PRESSURE,
TEMPERATURE,
TEMPERATURE_DELTA,
UNIT_NOT_RECOGNIZED_TEMPLATE,
VOLUME,
WIND_SPEED,
Expand All @@ -24,6 +25,7 @@
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTemperatureDelta,
UnitOfVolume,
UnitOfVolumetricFlux,
)
Expand All @@ -34,6 +36,7 @@
PressureConverter,
SpeedConverter,
TemperatureConverter,
TemperatureDeltaConverter,
VolumeConverter,
)

Expand Down Expand Up @@ -63,11 +66,14 @@

TEMPERATURE_UNITS: set[str] = {UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS}

TEMPERATURE_DELTA_UNITS: = TemperatureDeltaConverter.VALID_UNITS

_VALID_BY_TYPE: dict[str, set[str] | set[str | None]] = {
LENGTH: LENGTH_UNITS,
ACCUMULATED_PRECIPITATION: LENGTH_UNITS,
WIND_SPEED: WIND_SPEED_UNITS,
TEMPERATURE: TEMPERATURE_UNITS,
TEMPERATURE_DELTA: TEMPERATURE_DELTA_UNITS,
MASS: MASS_UNITS,
VOLUME: VOLUME_UNITS,
PRESSURE: PRESSURE_UNITS,
Expand Down Expand Up @@ -96,6 +102,7 @@ def __init__(
mass: UnitOfMass,
pressure: UnitOfPressure,
temperature: UnitOfTemperature,
temperature_delta: UnitOfTemperatureDelta,
volume: UnitOfVolume,
wind_speed: UnitOfSpeed,
) -> None:
Expand All @@ -106,6 +113,7 @@ def __init__(
(accumulated_precipitation, ACCUMULATED_PRECIPITATION),
(area, AREA),
(temperature, TEMPERATURE),
(temperature_delta, TEMPERATURE_DELTA),
(length, LENGTH),
(wind_speed, WIND_SPEED),
(volume, VOLUME),
Expand All @@ -125,6 +133,7 @@ def __init__(
self.mass_unit = mass
self.pressure_unit = pressure
self.temperature_unit = temperature
self.temperature_delta_unit = temperature_delta
self.volume_unit = volume
self.wind_speed_unit = wind_speed
self._conversions = conversions
Expand All @@ -138,6 +147,15 @@ def temperature(self, temperature: float, from_unit: str) -> float:
temperature, from_unit, self.temperature_unit
)

def temperature_delta(self, temperature_delta: float, from_unit: str) -> float:
"""Convert the given temperature delta to this unit system."""
if not isinstance(temperature_delta, Number):
raise TypeError(f"{temperature_delta!s} is not a numeric value.")

return TemperatureDeltaConverter.convert(
temperature_delta, from_unit, self.temperature_delta_unit
)

def length(self, length: float | None, from_unit: str) -> float:
"""Convert the given length to this unit system."""
if not isinstance(length, Number):
Expand Down Expand Up @@ -207,6 +225,7 @@ def as_dict(self) -> dict[str, str]:
MASS: self.mass_unit,
PRESSURE: self.pressure_unit,
TEMPERATURE: self.temperature_unit,
TEMPERATURE_DELTA: self.temperature_delta_unit,
VOLUME: self.volume_unit,
WIND_SPEED: self.wind_speed_unit,
}
Expand Down Expand Up @@ -315,6 +334,7 @@ def _deprecated_unit_system(value: str) -> str:
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
temperature=UnitOfTemperature.CELSIUS,
temperature_delta=UnitOfTemperatureDelta.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
)
Expand Down Expand Up @@ -393,6 +413,7 @@ def _deprecated_unit_system(value: str) -> str:
mass=UnitOfMass.POUNDS,
pressure=UnitOfPressure.PSI,
temperature=UnitOfTemperature.FAHRENHEIT,
temperature_delta=UnitOfTemperatureDelta.FAHRENHEIT,
volume=UnitOfVolume.GALLONS,
wind_speed=UnitOfSpeed.MILES_PER_HOUR,
)
Expand Down