Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for imperial units #1103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
21 changes: 15 additions & 6 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
SUPPORT_SWING_MODE, 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)
PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE, TEMP_FAHRENHEIT)
from homeassistant.core import callback
from homeassistant.helpers.event import async_track_state_change
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -54,7 +54,6 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the IR Climate platform."""
_LOGGER.debug("Setting up the startir platform")
device_code = config.get(CONF_DEVICE_CODE)
device_files_subdir = os.path.join('codes', 'climate')
device_files_absdir = os.path.join(COMPONENT_ABS_DIR, device_files_subdir)
Expand Down Expand Up @@ -84,9 +83,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

with open(device_json_path) as j:
try:
_LOGGER.debug(f"loading json file {device_json_path}")
device_data = json.load(j)
_LOGGER.debug(f"{device_json_path} file loaded")
except Exception:
_LOGGER.error("The device Json file is invalid")
return
Expand All @@ -97,7 +94,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

class SmartIRClimate(ClimateEntity, RestoreEntity):
def __init__(self, hass, config, device_data):
_LOGGER.debug(f"SmartIRClimate init started for device {config.get(CONF_NAME)} supported models {device_data['supportedModels']}")
self.hass = hass
self._unique_id = config.get(CONF_UNIQUE_ID)
self._name = config.get(CONF_NAME)
Expand Down Expand Up @@ -135,6 +131,10 @@ def __init__(self, hass, config, device_data):

self._unit = hass.config.units.temperature_unit

if self._unit == TEMP_FAHRENHEIT:
self._min_temperature = self._celsius_to_fahrenheit(self._min_temperature)
self._max_temperature = self._celsius_to_fahrenheit(self._max_temperature)

#Supported features
self._support_flags = SUPPORT_FLAGS
self._support_swing = False
Expand All @@ -158,7 +158,6 @@ def __init__(self, hass, config, device_data):
async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()
_LOGGER.debug(f"async_added_to_hass {self} {self.name} {self.supported_features}")

last_state = await self.async_get_last_state()

Expand Down Expand Up @@ -295,6 +294,12 @@ def extra_state_attributes(self):
'commands_encoding': self._commands_encoding
}

def _celsius_to_fahrenheit(self, temperature):
return round(temperature * 9 / 5) + 32

def _fahrenheit_to_celsius(self, temperature):
return round((temperature - 32) * 5 / 9)

async def async_set_temperature(self, **kwargs):
"""Set new target temperatures."""
hvac_mode = kwargs.get(ATTR_HVAC_MODE)
Expand Down Expand Up @@ -366,6 +371,9 @@ async def send_command(self):
fan_mode = self._current_fan_mode
swing_mode = self._current_swing_mode
target_temperature = '{0:g}'.format(self._target_temperature)

if self._unit == TEMP_FAHRENHEIT:
target_temperature = '{0:g}'.format(self._fahrenheit_to_celsius(self._target_temperature))

if operation_mode.lower() == HVAC_MODE_OFF:
await self._controller.send(self._commands['off'])
Expand Down Expand Up @@ -441,3 +449,4 @@ def _async_update_humidity(self, state):
self._current_humidity = float(state.state)
except ValueError as ex:
_LOGGER.error("Unable to update from humidity sensor: %s", ex)