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 b60485dc19.
pull/4540/head
Paulus Schoutsen 2016-11-22 17:38:04 -08:00 committed by GitHub
parent 00019b9ff0
commit 2c7e895105
5 changed files with 34 additions and 18 deletions

View File

@ -562,16 +562,15 @@ class ClimateDevice(Entity):
def _convert_for_display(self, temp):
"""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
value = convert_temperature(temp, self.temperature_unit,
self.unit_of_measurement)
if self.unit_of_measurement is TEMP_CELSIUS:
decimal_count = 1
if self.unit_of_measurement == TEMP_CELSIUS:
return round(value, 1)
else:
# Users of fahrenheit generally expect integer units.
decimal_count = 0
return round(value, decimal_count)
return round(value)

View File

@ -5,7 +5,9 @@ For more details about this component, please refer to the documentation at
https://home-assistant.io/components/weather/
"""
import logging
from numbers import Number
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.util.temperature import convert as convert_temperature
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
@ -84,10 +86,7 @@ class WeatherEntity(Entity):
def state_attributes(self):
"""Return the state attributes."""
data = {
ATTR_WEATHER_TEMPERATURE:
convert_temperature(
self.temperature, self.temperature_unit,
self.hass.config.units.temperature_unit),
ATTR_WEATHER_TEMPERATURE: self._temp_for_display,
ATTR_WEATHER_HUMIDITY: self.humidity,
}
@ -124,6 +123,20 @@ class WeatherEntity(Entity):
raise NotImplementedError()
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return None
def _temp_for_display(self):
"""Convert temperature into preferred units for display purposes."""
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)

View File

@ -259,9 +259,12 @@ class Entity(object):
# Convert temperature if we detect one
try:
unit_of_measure = attr.get(ATTR_UNIT_OF_MEASUREMENT)
if unit_of_measure in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
units = self.hass.config.units
state = str(units.temperature(float(state), unit_of_measure))
units = self.hass.config.units
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
except ValueError:
# Could not convert state to float

View File

@ -31,4 +31,4 @@ def convert(temperature: float, from_unit: str, to_unit: str) -> float:
elif from_unit == TEMP_CELSIUS:
return celsius_to_fahrenheit(temperature)
else:
return round(fahrenheit_to_celsius(temperature), 1)
return fahrenheit_to_celsius(temperature)

View File

@ -80,7 +80,8 @@ class TestUnitSystem(unittest.TestCase):
METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit))
self.assertEqual(
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):
"""Test temperature conversion to imperial system."""