mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Deprecate temperature conversion in base entity class (#68978)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
39cc91dec6
commit
69ee4cd978
@ -295,6 +295,9 @@ class Entity(ABC):
|
|||||||
# If we reported this entity is updated while disabled
|
# If we reported this entity is updated while disabled
|
||||||
_disabled_reported = False
|
_disabled_reported = False
|
||||||
|
|
||||||
|
# If we reported this entity is relying on deprecated temperature conversion
|
||||||
|
_temperature_reported = False
|
||||||
|
|
||||||
# Protect for multiple updates
|
# Protect for multiple updates
|
||||||
_update_staged = False
|
_update_staged = False
|
||||||
|
|
||||||
@ -642,23 +645,57 @@ class Entity(ABC):
|
|||||||
if DATA_CUSTOMIZE in self.hass.data:
|
if DATA_CUSTOMIZE in self.hass.data:
|
||||||
attr.update(self.hass.data[DATA_CUSTOMIZE].get(self.entity_id))
|
attr.update(self.hass.data[DATA_CUSTOMIZE].get(self.entity_id))
|
||||||
|
|
||||||
# Convert temperature if we detect one
|
def _convert_temperature(state: str, attr: dict) -> str:
|
||||||
try:
|
# Convert temperature if we detect one
|
||||||
|
# pylint: disable-next=import-outside-toplevel
|
||||||
|
from homeassistant.components.sensor import SensorEntity
|
||||||
|
|
||||||
unit_of_measure = attr.get(ATTR_UNIT_OF_MEASUREMENT)
|
unit_of_measure = attr.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
units = self.hass.config.units
|
units = self.hass.config.units
|
||||||
domain = split_entity_id(self.entity_id)[0]
|
if unit_of_measure == units.temperature_unit or unit_of_measure not in (
|
||||||
if (
|
TEMP_CELSIUS,
|
||||||
unit_of_measure in (TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
TEMP_FAHRENHEIT,
|
||||||
and unit_of_measure != units.temperature_unit
|
|
||||||
and domain != "sensor"
|
|
||||||
):
|
):
|
||||||
|
return state
|
||||||
|
|
||||||
|
domain = split_entity_id(self.entity_id)[0]
|
||||||
|
if domain != "sensor":
|
||||||
|
if not self._temperature_reported:
|
||||||
|
self._temperature_reported = True
|
||||||
|
report_issue = self._suggest_report_issue()
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Entity %s (%s) relies on automatic temperature conversion, this will "
|
||||||
|
"be unsupported in Home Assistant Core 2022.7. Please %s",
|
||||||
|
self.entity_id,
|
||||||
|
type(self),
|
||||||
|
report_issue,
|
||||||
|
)
|
||||||
|
elif not isinstance(self, SensorEntity):
|
||||||
|
if not self._temperature_reported:
|
||||||
|
self._temperature_reported = True
|
||||||
|
report_issue = self._suggest_report_issue()
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Temperature sensor %s (%s) does not inherit SensorEntity, "
|
||||||
|
"this will be unsupported in Home Assistant Core 2022.7."
|
||||||
|
"Please %s",
|
||||||
|
self.entity_id,
|
||||||
|
type(self),
|
||||||
|
report_issue,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return state
|
||||||
|
|
||||||
|
try:
|
||||||
prec = len(state) - state.index(".") - 1 if "." in state else 0
|
prec = len(state) - state.index(".") - 1 if "." in state else 0
|
||||||
temp = units.temperature(float(state), unit_of_measure)
|
temp = units.temperature(float(state), unit_of_measure)
|
||||||
state = str(round(temp) if prec == 0 else round(temp, prec))
|
state = str(round(temp) if prec == 0 else round(temp, prec))
|
||||||
attr[ATTR_UNIT_OF_MEASUREMENT] = units.temperature_unit
|
attr[ATTR_UNIT_OF_MEASUREMENT] = units.temperature_unit
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Could not convert state to float
|
# Could not convert state to float
|
||||||
pass
|
pass
|
||||||
|
return state
|
||||||
|
|
||||||
|
state = _convert_temperature(state, attr)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self._context_set is not None
|
self._context_set is not None
|
||||||
|
@ -13,6 +13,7 @@ from homeassistant.const import (
|
|||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
|
TEMP_FAHRENHEIT,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, HomeAssistantError
|
from homeassistant.core import Context, HomeAssistantError
|
||||||
from homeassistant.helpers import entity, entity_registry
|
from homeassistant.helpers import entity, entity_registry
|
||||||
@ -814,6 +815,64 @@ async def test_float_conversion(hass):
|
|||||||
assert state.state == "3.6"
|
assert state.state == "3.6"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_temperature_conversion(hass, caplog):
|
||||||
|
"""Test conversion of temperatures."""
|
||||||
|
# Non sensor entity reporting a temperature
|
||||||
|
with patch.object(
|
||||||
|
entity.Entity, "state", PropertyMock(return_value=100)
|
||||||
|
), patch.object(
|
||||||
|
entity.Entity, "unit_of_measurement", PropertyMock(return_value=TEMP_FAHRENHEIT)
|
||||||
|
):
|
||||||
|
ent = entity.Entity()
|
||||||
|
ent.hass = hass
|
||||||
|
ent.entity_id = "hello.world"
|
||||||
|
ent.async_write_ha_state()
|
||||||
|
|
||||||
|
state = hass.states.get("hello.world")
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == "38"
|
||||||
|
assert (
|
||||||
|
"Entity hello.world (<class 'homeassistant.helpers.entity.Entity'>) relies on automatic "
|
||||||
|
"temperature conversion, this will be unsupported in Home Assistant Core 2022.7. "
|
||||||
|
"Please create a bug report" in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sensor entity, not extending SensorEntity, reporting a temperature
|
||||||
|
with patch.object(
|
||||||
|
entity.Entity, "state", PropertyMock(return_value=100)
|
||||||
|
), patch.object(
|
||||||
|
entity.Entity, "unit_of_measurement", PropertyMock(return_value=TEMP_FAHRENHEIT)
|
||||||
|
):
|
||||||
|
ent = entity.Entity()
|
||||||
|
ent.hass = hass
|
||||||
|
ent.entity_id = "sensor.temp"
|
||||||
|
ent.async_write_ha_state()
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.temp")
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == "38"
|
||||||
|
assert (
|
||||||
|
"Temperature sensor sensor.temp (<class 'homeassistant.helpers.entity.Entity'>) "
|
||||||
|
"does not inherit SensorEntity, this will be unsupported in Home Assistant Core "
|
||||||
|
"2022.7.Please create a bug report" in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
# Sensor entity, not extending SensorEntity, not reporting a number
|
||||||
|
with patch.object(
|
||||||
|
entity.Entity, "state", PropertyMock(return_value="really warm")
|
||||||
|
), patch.object(
|
||||||
|
entity.Entity, "unit_of_measurement", PropertyMock(return_value=TEMP_FAHRENHEIT)
|
||||||
|
):
|
||||||
|
ent = entity.Entity()
|
||||||
|
ent.hass = hass
|
||||||
|
ent.entity_id = "sensor.temp"
|
||||||
|
ent.async_write_ha_state()
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.temp")
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == "really warm"
|
||||||
|
|
||||||
|
|
||||||
async def test_attribution_attribute(hass):
|
async def test_attribution_attribute(hass):
|
||||||
"""Test attribution attribute."""
|
"""Test attribution attribute."""
|
||||||
mock_entity = entity.Entity()
|
mock_entity = entity.Entity()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user