1111 ClimateEntityFeature , HVACMode , HVAC_MODES , ATTR_HVAC_MODE )
1212from homeassistant .const import (
1313 CONF_NAME , STATE_ON , STATE_OFF , STATE_UNKNOWN , STATE_UNAVAILABLE , ATTR_TEMPERATURE ,
14- PRECISION_TENTHS , PRECISION_HALVES , PRECISION_WHOLE )
14+ ATTR_UNIT_OF_MEASUREMENT , PRECISION_TENTHS , PRECISION_HALVES , PRECISION_TENTHS ,
15+ UnitOfTemperature )
1516from homeassistant .core import Event , EventStateChangedData , callback
1617from homeassistant .helpers .event import async_track_state_change , async_track_state_change_event
1718import homeassistant .helpers .config_validation as cv
1819from homeassistant .helpers .restore_state import RestoreEntity
20+ from homeassistant .util .unit_conversion import TemperatureConverter
1921from . import COMPONENT_ABS_DIR , Helper
2022from .controller import get_controller
2123
@@ -114,6 +116,12 @@ def __init__(self, hass, config, device_data):
114116 self ._supported_models = device_data ['supportedModels' ]
115117 self ._supported_controller = device_data ['supportedController' ]
116118 self ._commands_encoding = device_data ['commandsEncoding' ]
119+
120+ # Device target temperatures are in Celsius by default
121+ device_unit = UnitOfTemperature .CELSIUS
122+ if device_data .get ('temperatureUnit' ) in ('F' , '°F' ):
123+ device_unit = UnitOfTemperature .FAHRENHEIT
124+ self ._unit = device_unit
117125 self ._min_temperature = device_data ['minTemperature' ]
118126 self ._max_temperature = device_data ['maxTemperature' ]
119127 self ._precision = device_data ['precision' ]
@@ -134,8 +142,6 @@ def __init__(self, hass, config, device_data):
134142 self ._current_temperature = None
135143 self ._current_humidity = None
136144
137- self ._unit = hass .config .units .temperature_unit
138-
139145 #Supported features
140146 self ._support_flags = SUPPORT_FLAGS
141147 self ._support_swing = False
@@ -167,7 +173,13 @@ async def async_added_to_hass(self):
167173 self ._hvac_mode = last_state .state
168174 self ._current_fan_mode = last_state .attributes ['fan_mode' ]
169175 self ._current_swing_mode = last_state .attributes .get ('swing_mode' )
170- self ._target_temperature = last_state .attributes ['temperature' ]
176+ # Temperature unit used for state may not match device
177+ temperature = last_state .attributes ['temperature' ]
178+ unit = self .hass .config .units .temperature_unit
179+ self ._target_temperature = (
180+ TemperatureConverter .convert (temperature , unit , self ._unit )
181+ if unit != self ._unit
182+ else temperature )
171183
172184 if 'last_on_operation' in last_state .attributes :
173185 self ._last_on_operation = last_state .attributes ['last_on_operation' ]
@@ -308,10 +320,7 @@ async def async_set_temperature(self, **kwargs):
308320 _LOGGER .warning ('The temperature value is out of min/max range' )
309321 return
310322
311- if self ._precision == PRECISION_WHOLE :
312- self ._target_temperature = round (temperature )
313- else :
314- self ._target_temperature = round (temperature , 1 )
323+ self ._target_temperature = temperature
315324
316325 if hvac_mode :
317326 await self .async_set_hvac_mode (hvac_mode )
@@ -366,7 +375,15 @@ async def send_command(self):
366375 operation_mode = self ._hvac_mode
367376 fan_mode = self ._current_fan_mode
368377 swing_mode = self ._current_swing_mode
369- target_temperature = '{0:g}' .format (self ._target_temperature )
378+ temperature = self ._target_temperature
379+
380+ if self ._precision == PRECISION_TENTHS :
381+ temperature = round (temperature , 1 )
382+ elif self ._precision == PRECISION_HALVES :
383+ temperature = round (temperature * 2.0 ) / 2.0
384+ else :
385+ temperature = round (temperature )
386+ target_temperature = '{0:g}' .format (temperature )
370387
371388 if operation_mode .lower () == HVACMode .OFF :
372389 await self ._controller .send (self ._commands ['off' ])
@@ -440,7 +457,14 @@ def _async_update_temp(self, state):
440457 """Update thermostat with latest state from temperature sensor."""
441458 try :
442459 if state .state != STATE_UNKNOWN and state .state != STATE_UNAVAILABLE :
443- self ._current_temperature = float (state .state )
460+ unit = state .attributes .get (ATTR_UNIT_OF_MEASUREMENT )
461+ if not unit :
462+ unit = self .hass .config .units .temperature_unit
463+ self ._current_temperature = (
464+ TemperatureConverter .convert (float (state .state ), unit , self ._unit )
465+ if unit != self ._unit
466+ else float (state .state ))
467+
444468 except ValueError as ex :
445469 _LOGGER .error ("Unable to update from temperature sensor: %s" , ex )
446470
@@ -451,4 +475,4 @@ def _async_update_humidity(self, state):
451475 if state .state != STATE_UNKNOWN and state .state != STATE_UNAVAILABLE :
452476 self ._current_humidity = float (state .state )
453477 except ValueError as ex :
454- _LOGGER .error ("Unable to update from humidity sensor: %s" , ex )
478+ _LOGGER .error ("Unable to update from humidity sensor: %s" , ex )
0 commit comments