diff --git a/homeassistant/components/climate/__init__.py b/homeassistant/components/climate/__init__.py index 5ae10fca303..d35b142a8c4 100644 --- a/homeassistant/components/climate/__init__.py +++ b/homeassistant/components/climate/__init__.py @@ -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) diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index 50173840657..26a5a41bf10 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -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) diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index b707f2f7199..87b05ded264 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -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 diff --git a/homeassistant/util/temperature.py b/homeassistant/util/temperature.py index d6e245de04f..c773e564011 100644 --- a/homeassistant/util/temperature.py +++ b/homeassistant/util/temperature.py @@ -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) diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index c99c2cf87bf..83edb056139 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -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."""