From 85917df03d8244887d8bfb0fc01b0353112fa127 Mon Sep 17 00:00:00 2001 From: devbyaccident Date: Wed, 2 Aug 2023 14:40:17 -0400 Subject: [PATCH 1/4] Adding support for imperial units Co-authored-by: Kevin Brown-Silva --- custom_components/smartir/climate.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index 0c21ea2a..af732b99 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -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() @@ -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) @@ -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']) @@ -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) + From c9a611cb0b37a96daddacfebd0fc23b55e2edc1c Mon Sep 17 00:00:00 2001 From: Ryan DeShone Date: Wed, 30 Oct 2024 15:44:36 -0400 Subject: [PATCH 2/4] Update deprecated temp const --- custom_components/smartir/climate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index af732b99..1b1a03ad 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -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, TEMP_FAHRENHEIT) + PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE, UnitOfTemperature) from homeassistant.core import callback from homeassistant.helpers.event import async_track_state_change import homeassistant.helpers.config_validation as cv @@ -131,7 +131,7 @@ def __init__(self, hass, config, device_data): self._unit = hass.config.units.temperature_unit - if self._unit == TEMP_FAHRENHEIT: + if self._unit == UnitOfTemperature.FAHRENHEIT: self._min_temperature = self._celsius_to_fahrenheit(self._min_temperature) self._max_temperature = self._celsius_to_fahrenheit(self._max_temperature) @@ -372,7 +372,7 @@ async def send_command(self): swing_mode = self._current_swing_mode target_temperature = '{0:g}'.format(self._target_temperature) - if self._unit == TEMP_FAHRENHEIT: + if self._unit == UnitOfTemperature.FAHRENHEIT: target_temperature = '{0:g}'.format(self._fahrenheit_to_celsius(self._target_temperature)) if operation_mode.lower() == HVAC_MODE_OFF: From a85b497d138c20ca638eb3637e461f620494329a Mon Sep 17 00:00:00 2001 From: Ryan DeShone Date: Wed, 30 Oct 2024 15:51:41 -0400 Subject: [PATCH 3/4] Add missing logger lines --- custom_components/smartir/climate.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index 1b1a03ad..871eaf82 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -54,6 +54,7 @@ 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) @@ -83,7 +84,9 @@ 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 @@ -94,6 +97,7 @@ 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) @@ -158,6 +162,7 @@ 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() From 24f92d3da471ea5186a3d5ef1c41230b7a5091ad Mon Sep 17 00:00:00 2001 From: Ryan DeShone Date: Wed, 30 Oct 2024 15:56:20 -0400 Subject: [PATCH 4/4] Drop extra newline at eof --- custom_components/smartir/climate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index 871eaf82..3f6b43c7 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -454,4 +454,3 @@ 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) -