Skip to content

Commit

Permalink
Fix RC-3 logic with on and regular codes
Browse files Browse the repository at this point in the history
Add optional field disable_sending_commands_when_device_off in XXXX.json
  • Loading branch information
klimofey committed Jun 25, 2024
1 parent a7fb75e commit aa87af2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
1 change: 1 addition & 0 deletions codes/climate/1948.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"RC-3"
],
"supportedController": "UFOR11",
"disable_sending_commands_when_device_off": true,
"commandsEncoding": "Raw",
"temperatureUnit": "C",
"minTemperature": 16.0,
Expand Down
98 changes: 61 additions & 37 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)])


Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
):
Expand Down
4 changes: 2 additions & 2 deletions info.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit aa87af2

Please sign in to comment.