diff --git a/codes/climate/1948.json b/codes/climate/1948.json index 28957fde..40348a01 100644 --- a/codes/climate/1948.json +++ b/codes/climate/1948.json @@ -6,6 +6,7 @@ "RC-3" ], "supportedController": "UFOR11", + "disable_sending_commands_when_device_off": true, "commandsEncoding": "Raw", "temperatureUnit": "C", "minTemperature": 16.0, diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index c35b0bea..f766b0a0 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -48,6 +48,13 @@ CONF_POWER_SENSOR_DELAY = "power_sensor_delay" CONF_POWER_SENSOR_RESTORE_STATE = "power_sensor_restore_state" +CONF_DISABLE_SENDING_COMMANDS_WHEN_DEVICE_OFF = ( + "disable_sending_commands_when_device_off" +) + +CONF_CODE_ON_NAME = "on" +CONF_CODE_REGULAR_NAME = "regular" + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Optional(CONF_UNIQUE_ID): cv.string, @@ -92,6 +99,11 @@ async def async_setup_platform( _LOGGER.error("SmartIR climate device data init failed!") return + # Make disable_sending_off_when_device_off optional + device_data[CONF_DISABLE_SENDING_COMMANDS_WHEN_DEVICE_OFF] = device_data.get( + CONF_DISABLE_SENDING_COMMANDS_WHEN_DEVICE_OFF, False + ) + async_add_entities([SmartIRClimate(hass, config, device_data)]) @@ -116,6 +128,7 @@ def __init__(self, hass, config, device_data): self._power_sensor = config.get(CONF_POWER_SENSOR) self._power_sensor_delay = config.get(CONF_POWER_SENSOR_DELAY) self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE) + self._temperature_unit = hass.config.units.temperature_unit self._state = STATE_UNKNOWN @@ -139,6 +152,9 @@ def __init__(self, hass, config, device_data): self._supported_controller = device_data["supportedController"] self._commands_encoding = device_data["commandsEncoding"] self._commands = device_data["commands"] + self._disable_sending_commands_when_device_off = device_data[ + CONF_DISABLE_SENDING_COMMANDS_WHEN_DEVICE_OFF + ] # device temperature units self._data_temperature_unit = UnitOfTemperature.CELSIUS @@ -489,47 +505,36 @@ async def async_turn_on(self): await self.async_set_hvac_mode(self._hvac_mode) async def _send_command(self, state, hvac_mode, fan_mode, swing_mode, temperature): + _LOGGER.error( + "_send_command \nself._state: %s \nstate: %s \nhvac_mode: %s \nfan_mode: %s \nswing_mode: %s \ntemperature: %s", + self._state, + state, + hvac_mode, + fan_mode, + swing_mode, + temperature, + ) async with self._temp_lock: - if isinstance(temperature, str): - target_temperature = str( - display_temp( - self.hass, - temperature, - self._data_temperature_unit, - self._precision, - ) - ) - elif isinstance(temperature, dict): - if state == STATE_OFF: - target_temperature = temperature.get("on") - if not target_temperature: - _LOGGER.error( - "Missing 'on' field in temperature object for off state." - ) - return - else: - target_temperature = temperature.get("regular") - if not target_temperature: - _LOGGER.error( - "Missing 'regular' field in temperature object for on state." - ) - return - target_temperature = str( - display_temp( - self.hass, - target_temperature, - self._data_temperature_unit, - self._precision, - ) + target_temperature = str( + display_temp( + self.hass, + temperature, + self._data_temperature_unit, + self._precision, ) - else: - _LOGGER.error("Temperature must be either a string or a dictionary.") - return + ) if self._power_sensor and self._state != state: self._async_power_sensor_check_schedule(state) + if ( + state == STATE_OFF + and self._state == STATE_OFF + and self._disable_sending_commands_when_device_off + ): + return + try: if state == STATE_OFF: if ( @@ -620,9 +625,28 @@ async def _send_command(self, state, hvac_mode, fan_mode, swing_mode, temperatur and target_temperature in self._commands[hvac_mode][fan_mode].keys() ): - await self._controller.send( - self._commands[hvac_mode][fan_mode][target_temperature] - ) + # some devices have a special code for turning on the device with the last settings + if isinstance( + self._commands[hvac_mode][fan_mode][target_temperature], + dict, + ): + # if now state is off, use the "on" code, otherwise use the "regular" code + command_name = ( + CONF_CODE_ON_NAME + if self._state == STATE_OFF + else CONF_CODE_REGULAR_NAME + ) + await self._controller.send( + self._commands[hvac_mode][fan_mode][ + target_temperature + ][command_name] + ) + else: + await self._controller.send( + self._commands[hvac_mode][fan_mode][ + target_temperature + ] + ) elif hvac_mode == HVACMode.FAN_ONLY and isinstance( self._commands[hvac_mode][fan_mode], str ): diff --git a/info.md b/info.md index e2fe8fb0..c7a702f3 100644 --- a/info.md +++ b/info.md @@ -1,8 +1,8 @@ # ⏻ Smart IR [![HACS Badge](https://img.shields.io/badge/HACS-Custom-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration) -[![License](https://img.shields.io/github/license/klimofey/smartir?style=for-the-badge)](https://github.com/klimofey/smartir/blob/master/LICENSE) -[![Latest Release](https://img.shields.io/github/v/release/klimofey/smartir?style=for-the-badge)](https://github.com/klimofey/smartir/releases) +[![License](https://img.shields.io/github/license/klimofey/smartir?style=for-the-badge)](https://github.com/klimofey/SmartIR/blob/master/LICENSE) +[![Latest Release](https://img.shields.io/github/v/release/klimofey/smartir?style=for-the-badge)](https://github.com/klimofey/SmartIR/releases) [![Code style](https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge)](https://github.com/psf/black) ## Overview