Remove deprecated temperature conversion of non temperature sensors (#69069)
parent
04dd7c3f7c
commit
e830032b33
|
@ -367,7 +367,10 @@ class SensorEntity(Entity):
|
|||
|
||||
native_unit_of_measurement = self.native_unit_of_measurement
|
||||
|
||||
if native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
||||
if (
|
||||
self.device_class == DEVICE_CLASS_TEMPERATURE
|
||||
and native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||
):
|
||||
return self.hass.config.units.temperature_unit
|
||||
|
||||
return native_unit_of_measurement
|
||||
|
@ -450,37 +453,6 @@ class SensorEntity(Entity):
|
|||
# Round to the wanted precision
|
||||
value = round(value_f_new) if prec == 0 else round(value_f_new, prec)
|
||||
|
||||
elif (
|
||||
value is not None
|
||||
and self.device_class != DEVICE_CLASS_TEMPERATURE
|
||||
and native_unit_of_measurement != self.hass.config.units.temperature_unit
|
||||
and native_unit_of_measurement in (TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||
):
|
||||
units = self.hass.config.units
|
||||
if not self._temperature_conversion_reported:
|
||||
self._temperature_conversion_reported = True
|
||||
report_issue = self._suggest_report_issue()
|
||||
_LOGGER.warning(
|
||||
"Entity %s (%s) with device_class %s reports a temperature in "
|
||||
"%s which will be converted to %s. Temperature conversion for "
|
||||
"entities without correct device_class is deprecated and will"
|
||||
" be removed from Home Assistant Core 2022.3. Please update "
|
||||
"your configuration if device_class is manually configured, "
|
||||
"otherwise %s",
|
||||
self.entity_id,
|
||||
type(self),
|
||||
self.device_class,
|
||||
native_unit_of_measurement,
|
||||
units.temperature_unit,
|
||||
report_issue,
|
||||
)
|
||||
value_s = str(value)
|
||||
prec = len(value_s) - value_s.index(".") - 1 if "." in value_s else 0
|
||||
# Suppress ValueError (Could not convert sensor_value to float)
|
||||
with suppress(ValueError):
|
||||
temp = units.temperature(float(value), native_unit_of_measurement) # type: ignore[arg-type]
|
||||
value = round(temp) if prec == 0 else round(temp, prec)
|
||||
|
||||
return value
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
|
|
@ -474,7 +474,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
|
|||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class of the sensor."""
|
||||
return DEVICE_CLASS_MAP.get(self.unit_of_measurement)
|
||||
return DEVICE_CLASS_MAP.get(self._unit_of_measurement)
|
||||
|
||||
@property
|
||||
def state_class(self):
|
||||
|
|
|
@ -6,7 +6,11 @@ from typing import cast
|
|||
|
||||
import pyvera as veraApi
|
||||
|
||||
from homeassistant.components.sensor import ENTITY_ID_FORMAT, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
ENTITY_ID_FORMAT,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
LIGHT_LUX,
|
||||
|
@ -60,6 +64,19 @@ class VeraSensor(VeraDevice[veraApi.VeraSensor], SensorEntity):
|
|||
"""Return the name of the sensor."""
|
||||
return self.current_value
|
||||
|
||||
@property
|
||||
def device_class(self) -> str | None:
|
||||
"""Return the class of this entity."""
|
||||
if self.vera_device.category == veraApi.CATEGORY_TEMPERATURE_SENSOR:
|
||||
return SensorDeviceClass.TEMPERATURE
|
||||
if self.vera_device.category == veraApi.CATEGORY_LIGHT_SENSOR:
|
||||
return SensorDeviceClass.ILLUMINANCE
|
||||
if self.vera_device.category == veraApi.CATEGORY_HUMIDITY_SENSOR:
|
||||
return SensorDeviceClass.HUMIDITY
|
||||
if self.vera_device.category == veraApi.CATEGORY_POWER_METER:
|
||||
return SensorDeviceClass.POWER
|
||||
return None
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self) -> str | None:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
|
|
|
@ -63,31 +63,28 @@ async def test_temperature_conversion(
|
|||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
|
||||
|
||||
|
||||
async def test_deprecated_temperature_conversion(
|
||||
hass, caplog, enable_custom_integrations
|
||||
@pytest.mark.parametrize("device_class", (None, SensorDeviceClass.PRESSURE))
|
||||
async def test_temperature_conversion_wrong_device_class(
|
||||
hass, device_class, enable_custom_integrations
|
||||
):
|
||||
"""Test warning on deprecated temperature conversion."""
|
||||
"""Test temperatures are not converted if the sensor has wrong device class."""
|
||||
platform = getattr(hass.components, "test.sensor")
|
||||
platform.init(empty=True)
|
||||
platform.ENTITIES["0"] = platform.MockSensor(
|
||||
name="Test", native_value="0.0", native_unit_of_measurement=TEMP_FAHRENHEIT
|
||||
name="Test",
|
||||
native_value="0.0",
|
||||
native_unit_of_measurement=TEMP_FAHRENHEIT,
|
||||
device_class=device_class,
|
||||
)
|
||||
|
||||
entity0 = platform.ENTITIES["0"]
|
||||
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Check temperature is not converted
|
||||
state = hass.states.get(entity0.entity_id)
|
||||
assert state.state == "-17.8"
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_CELSIUS
|
||||
assert (
|
||||
"Entity sensor.test (<class 'custom_components.test.sensor.MockSensor'>) "
|
||||
"with device_class None reports a temperature in °F which will be converted to "
|
||||
"°C. Temperature conversion for entities without correct device_class is "
|
||||
"deprecated and will be removed from Home Assistant Core 2022.3. Please update "
|
||||
"your configuration if device_class is manually configured, otherwise report it "
|
||||
"to the custom component author."
|
||||
) in caplog.text
|
||||
assert state.state == "0.0"
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == TEMP_FAHRENHEIT
|
||||
|
||||
|
||||
@pytest.mark.parametrize("state_class", ("measurement", "total_increasing"))
|
||||
|
|
Loading…
Reference in New Issue