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

HA 2024 climate compatibility changes #1231

Closed
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
41 changes: 21 additions & 20 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

from homeassistant.components.climate import ClimateEntity, PLATFORM_SCHEMA
from homeassistant.components.climate.const import (
HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL,
HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, HVAC_MODE_AUTO,
SUPPORT_TARGET_TEMPERATURE, SUPPORT_FAN_MODE,
SUPPORT_SWING_MODE, HVAC_MODES, ATTR_HVAC_MODE)
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)
Expand All @@ -36,8 +33,10 @@
CONF_POWER_SENSOR_RESTORE_STATE = 'power_sensor_restore_state'

SUPPORT_FLAGS = (
SUPPORT_TARGET_TEMPERATURE |
SUPPORT_FAN_MODE
ClimateEntityFeature.TARGET_TEMPERATURE |
ClimateEntityFeature.FAN_MODE |
ClimateEntityFeature.TURN_ON |
ClimateEntityFeature.TURN_OFF
)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
Expand Down Expand Up @@ -96,6 +95,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
)])

class SmartIRClimate(ClimateEntity, RestoreEntity):
_enable_turn_on_off_backwards_compatibility = False

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
Expand All @@ -119,13 +120,13 @@ def __init__(self, hass, config, device_data):

valid_hvac_modes = [x for x in device_data['operationModes'] if x in HVAC_MODES]

self._operation_modes = [HVAC_MODE_OFF] + valid_hvac_modes
self._operation_modes = [HVACMode.OFF] + valid_hvac_modes
self._fan_modes = device_data['fanModes']
self._swing_modes = device_data.get('swingModes')
self._commands = device_data['commands']

self._target_temperature = self._min_temperature
self._hvac_mode = HVAC_MODE_OFF
self._hvac_mode = HVACMode.OFF
self._current_fan_mode = self._fan_modes[0]
self._current_swing_mode = None
self._last_on_operation = None
Expand All @@ -140,7 +141,7 @@ def __init__(self, hass, config, device_data):
self._support_swing = False

if self._swing_modes:
self._support_flags = self._support_flags | SUPPORT_SWING_MODE
self._support_flags = self._support_flags | ClimateEntityFeature.SWING_MODE
self._current_swing_mode = self._swing_modes[0]
self._support_swing = True

Expand Down Expand Up @@ -204,9 +205,9 @@ def name(self):
@property
def state(self):
"""Return the current state."""
if self.hvac_mode != HVAC_MODE_OFF:
if self.hvac_mode != HVACMode.OFF:
return self.hvac_mode
return HVAC_MODE_OFF
return HVACMode.OFF

@property
def temperature_unit(self):
Expand Down Expand Up @@ -316,7 +317,7 @@ async def async_set_temperature(self, **kwargs):
await self.async_set_hvac_mode(hvac_mode)
return

if not self._hvac_mode.lower() == HVAC_MODE_OFF:
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()

self.async_write_ha_state()
Expand All @@ -325,7 +326,7 @@ async def async_set_hvac_mode(self, hvac_mode):
"""Set operation mode."""
self._hvac_mode = hvac_mode

if not hvac_mode == HVAC_MODE_OFF:
if not hvac_mode == HVACMode.OFF:
self._last_on_operation = hvac_mode

await self.send_command()
Expand All @@ -335,21 +336,21 @@ async def async_set_fan_mode(self, fan_mode):
"""Set fan mode."""
self._current_fan_mode = fan_mode

if not self._hvac_mode.lower() == HVAC_MODE_OFF:
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()

async def async_set_swing_mode(self, swing_mode):
"""Set swing mode."""
self._current_swing_mode = swing_mode

if not self._hvac_mode.lower() == HVAC_MODE_OFF:
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()

async def async_turn_off(self):
"""Turn off."""
await self.async_set_hvac_mode(HVAC_MODE_OFF)
await self.async_set_hvac_mode(HVACMode.OFF)

async def async_turn_on(self):
"""Turn on."""
Expand All @@ -367,7 +368,7 @@ async def send_command(self):
swing_mode = self._current_swing_mode
target_temperature = '{0:g}'.format(self._target_temperature)

if operation_mode.lower() == HVAC_MODE_OFF:
if operation_mode.lower() == HVACMode.OFF:
await self._controller.send(self._commands['off'])
return

Expand Down Expand Up @@ -409,7 +410,7 @@ async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
if old_state is not None and new_state.state == old_state.state:
return

if new_state.state == STATE_ON and self._hvac_mode == HVAC_MODE_OFF:
if new_state.state == STATE_ON and self._hvac_mode == HVACMode.OFF:
self._on_by_remote = True
if self._power_sensor_restore_state == True and self._last_on_operation is not None:
self._hvac_mode = self._last_on_operation
Expand All @@ -420,8 +421,8 @@ async def _async_power_sensor_changed(self, entity_id, old_state, new_state):

if new_state.state == STATE_OFF:
self._on_by_remote = False
if self._hvac_mode != HVAC_MODE_OFF:
self._hvac_mode = HVAC_MODE_OFF
if self._hvac_mode != HVACMode.OFF:
self._hvac_mode = HVACMode.OFF
self.async_write_ha_state()

@callback
Expand Down