mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Entity and climate: do not convert temperature unnecessary (#4522)
* Climate: more consistent units * Prevent unnecessary conversion in entity component * int -> round * Disable Google tests because they connect to the internet * Remove default conversion rounding F->C * Add rounding of temp to weather comp * Fix equality * Maintain precision when converting temp in entity * Revert "Disable Google tests because they connect to the internet" This reverts commit b60485dc19bb97f4a502854d5ff2297330df0b40.
This commit is contained in:
parent
00019b9ff0
commit
2c7e895105
@ -562,16 +562,15 @@ class ClimateDevice(Entity):
|
|||||||
|
|
||||||
def _convert_for_display(self, temp):
|
def _convert_for_display(self, temp):
|
||||||
"""Convert temperature into preferred units for display purposes."""
|
"""Convert temperature into preferred units for display purposes."""
|
||||||
if temp is None or not isinstance(temp, Number):
|
if (temp is None or not isinstance(temp, Number) or
|
||||||
|
self.temperature_unit == self.unit_of_measurement):
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
value = convert_temperature(temp, self.temperature_unit,
|
value = convert_temperature(temp, self.temperature_unit,
|
||||||
self.unit_of_measurement)
|
self.unit_of_measurement)
|
||||||
|
|
||||||
if self.unit_of_measurement is TEMP_CELSIUS:
|
if self.unit_of_measurement == TEMP_CELSIUS:
|
||||||
decimal_count = 1
|
return round(value, 1)
|
||||||
else:
|
else:
|
||||||
# Users of fahrenheit generally expect integer units.
|
# Users of fahrenheit generally expect integer units.
|
||||||
decimal_count = 0
|
return round(value)
|
||||||
|
|
||||||
return round(value, decimal_count)
|
|
||||||
|
@ -5,7 +5,9 @@ For more details about this component, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/weather/
|
https://home-assistant.io/components/weather/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
from numbers import Number
|
||||||
|
|
||||||
|
from homeassistant.const import TEMP_CELSIUS
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.util.temperature import convert as convert_temperature
|
from homeassistant.util.temperature import convert as convert_temperature
|
||||||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
||||||
@ -84,10 +86,7 @@ class WeatherEntity(Entity):
|
|||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
data = {
|
data = {
|
||||||
ATTR_WEATHER_TEMPERATURE:
|
ATTR_WEATHER_TEMPERATURE: self._temp_for_display,
|
||||||
convert_temperature(
|
|
||||||
self.temperature, self.temperature_unit,
|
|
||||||
self.hass.config.units.temperature_unit),
|
|
||||||
ATTR_WEATHER_HUMIDITY: self.humidity,
|
ATTR_WEATHER_HUMIDITY: self.humidity,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,6 +123,20 @@ class WeatherEntity(Entity):
|
|||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def _temp_for_display(self):
|
||||||
"""Return the unit of measurement."""
|
"""Convert temperature into preferred units for display purposes."""
|
||||||
return None
|
temp = self.temperature
|
||||||
|
unit = self.temperature_unit
|
||||||
|
hass_unit = self.hass.config.units.temperature_unit
|
||||||
|
|
||||||
|
if (temp is None or not isinstance(temp, Number) or
|
||||||
|
unit == hass_unit):
|
||||||
|
return temp
|
||||||
|
|
||||||
|
value = convert_temperature(temp, unit, hass_unit)
|
||||||
|
|
||||||
|
if hass_unit == TEMP_CELSIUS:
|
||||||
|
return round(value, 1)
|
||||||
|
else:
|
||||||
|
# Users of fahrenheit generally expect integer units.
|
||||||
|
return round(value)
|
||||||
|
@ -259,9 +259,12 @@ class Entity(object):
|
|||||||
# Convert temperature if we detect one
|
# Convert temperature if we detect one
|
||||||
try:
|
try:
|
||||||
unit_of_measure = attr.get(ATTR_UNIT_OF_MEASUREMENT)
|
unit_of_measure = attr.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
if unit_of_measure in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
|
||||||
units = self.hass.config.units
|
units = self.hass.config.units
|
||||||
state = str(units.temperature(float(state), unit_of_measure))
|
if (unit_of_measure in (TEMP_CELSIUS, TEMP_FAHRENHEIT) and
|
||||||
|
unit_of_measure != units.temperature_unit):
|
||||||
|
prec = len(state) - state.index('.') - 1 if '.' in state else 0
|
||||||
|
temp = units.temperature(float(state), unit_of_measure)
|
||||||
|
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
|
||||||
|
@ -31,4 +31,4 @@ def convert(temperature: float, from_unit: str, to_unit: str) -> float:
|
|||||||
elif from_unit == TEMP_CELSIUS:
|
elif from_unit == TEMP_CELSIUS:
|
||||||
return celsius_to_fahrenheit(temperature)
|
return celsius_to_fahrenheit(temperature)
|
||||||
else:
|
else:
|
||||||
return round(fahrenheit_to_celsius(temperature), 1)
|
return fahrenheit_to_celsius(temperature)
|
||||||
|
@ -80,7 +80,8 @@ class TestUnitSystem(unittest.TestCase):
|
|||||||
METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit))
|
METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
26.7,
|
26.7,
|
||||||
METRIC_SYSTEM.temperature(80, IMPERIAL_SYSTEM.temperature_unit))
|
round(METRIC_SYSTEM.temperature(
|
||||||
|
80, IMPERIAL_SYSTEM.temperature_unit), 1))
|
||||||
|
|
||||||
def test_temperature_to_imperial(self):
|
def test_temperature_to_imperial(self):
|
||||||
"""Test temperature conversion to imperial system."""
|
"""Test temperature conversion to imperial system."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user