Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
ClimateEntityFeature, HVACMode, HVAC_MODES, ATTR_HVAC_MODE)
from homeassistant.const import (
CONF_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE, ATTR_TEMPERATURE,
PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE)
ATTR_UNIT_OF_MEASUREMENT, PRECISION_TENTHS, PRECISION_HALVES, UnitOfTemperature)
from homeassistant.core import Event, EventStateChangedData, callback
from homeassistant.helpers.event import async_track_state_change, async_track_state_change_event
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util.unit_conversion import TemperatureConverter
from . import COMPONENT_ABS_DIR, Helper
from .controller import get_controller

Expand Down Expand Up @@ -114,6 +115,12 @@ def __init__(self, hass, config, device_data):
self._supported_models = device_data['supportedModels']
self._supported_controller = device_data['supportedController']
self._commands_encoding = device_data['commandsEncoding']

# Device target temperatures are in Celsius by default
device_unit = UnitOfTemperature.CELSIUS
if device_data.get('temperatureUnit') in ('F', '°F'):
device_unit = UnitOfTemperature.FAHRENHEIT
self._unit = device_unit
self._min_temperature = device_data['minTemperature']
self._max_temperature = device_data['maxTemperature']
self._precision = device_data['precision']
Expand All @@ -134,8 +141,6 @@ def __init__(self, hass, config, device_data):
self._current_temperature = None
self._current_humidity = None

self._unit = hass.config.units.temperature_unit

#Supported features
self._support_flags = SUPPORT_FLAGS
self._support_swing = False
Expand Down Expand Up @@ -167,7 +172,13 @@ async def async_added_to_hass(self):
self._hvac_mode = last_state.state
self._current_fan_mode = last_state.attributes['fan_mode']
self._current_swing_mode = last_state.attributes.get('swing_mode')
self._target_temperature = last_state.attributes['temperature']
# Temperature unit used for state may not match device
temperature = last_state.attributes['temperature']
unit = self.hass.config.units.temperature_unit
self._target_temperature = (
TemperatureConverter.convert(temperature, unit, self._unit)
if unit != self._unit
else temperature)

if 'last_on_operation' in last_state.attributes:
self._last_on_operation = last_state.attributes['last_on_operation']
Expand Down Expand Up @@ -308,10 +319,7 @@ async def async_set_temperature(self, **kwargs):
_LOGGER.warning('The temperature value is out of min/max range')
return

if self._precision == PRECISION_WHOLE:
self._target_temperature = round(temperature)
else:
self._target_temperature = round(temperature, 1)
self._target_temperature = temperature

if hvac_mode:
await self.async_set_hvac_mode(hvac_mode)
Expand Down Expand Up @@ -366,7 +374,15 @@ async def send_command(self):
operation_mode = self._hvac_mode
fan_mode = self._current_fan_mode
swing_mode = self._current_swing_mode
target_temperature = '{0:g}'.format(self._target_temperature)
temperature = self._target_temperature

if self._precision == PRECISION_TENTHS:
temperature = round(temperature, 1)
elif self._precision == PRECISION_HALVES:
temperature = round(temperature * 2.0) / 2.0
else:
temperature = round(temperature)
target_temperature = '{0:g}'.format(temperature)

if operation_mode.lower() == HVACMode.OFF:
await self._controller.send(self._commands['off'])
Expand Down Expand Up @@ -440,7 +456,14 @@ def _async_update_temp(self, state):
"""Update thermostat with latest state from temperature sensor."""
try:
if state.state != STATE_UNKNOWN and state.state != STATE_UNAVAILABLE:
self._current_temperature = float(state.state)
unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
if not unit:
unit = self.hass.config.units.temperature_unit
self._current_temperature = (
TemperatureConverter.convert(float(state.state), unit, self._unit)
if unit != self._unit
else float(state.state))

except ValueError as ex:
_LOGGER.error("Unable to update from temperature sensor: %s", ex)

Expand All @@ -451,4 +474,4 @@ def _async_update_humidity(self, state):
if state.state != STATE_UNKNOWN and state.state != STATE_UNAVAILABLE:
self._current_humidity = float(state.state)
except ValueError as ex:
_LOGGER.error("Unable to update from humidity sensor: %s", ex)
_LOGGER.error("Unable to update from humidity sensor: %s", ex)