From 471c155baa8d087f8a1fc8a3eacf06a95d60a59d Mon Sep 17 00:00:00 2001 From: Vassilis Panos <4130346+vassilis-panos@users.noreply.github.com> Date: Sat, 5 Feb 2022 12:23:20 +0200 Subject: [PATCH] Compliance with the breaking change of the new fan model --- custom_components/smartir/__init__.py | 2 +- custom_components/smartir/fan.py | 50 +++++++++++++++---------- custom_components/smartir/manifest.json | 8 ++-- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/custom_components/smartir/__init__.py b/custom_components/smartir/__init__.py index 110955fd..15c923d7 100644 --- a/custom_components/smartir/__init__.py +++ b/custom_components/smartir/__init__.py @@ -19,7 +19,7 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = 'smartir' -VERSION = '1.17.4' +VERSION = '1.17.5' MANIFEST_URL = ( "https://raw.githubusercontent.com/" "smartHomeHub/SmartIR/{}/" diff --git a/custom_components/smartir/fan.py b/custom_components/smartir/fan.py index 1bccaba4..2c7b99fc 100644 --- a/custom_components/smartir/fan.py +++ b/custom_components/smartir/fan.py @@ -16,6 +16,10 @@ from homeassistant.helpers.event import async_track_state_change import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.util.percentage import ( + ordered_list_item_to_percentage, + percentage_to_ordered_list_item +) from . import COMPONENT_ABS_DIR, Helper from .controller import get_controller @@ -93,7 +97,7 @@ def __init__(self, hass, config, device_data): self._supported_models = device_data['supportedModels'] self._supported_controller = device_data['supportedController'] self._commands_encoding = device_data['commandsEncoding'] - self._speed_list = [SPEED_OFF] + device_data['speed'] + self._speed_list = device_data['speed'] self._commands = device_data['commands'] self._speed = SPEED_OFF @@ -161,19 +165,22 @@ def name(self): def state(self): """Return the current state.""" if (self._on_by_remote or \ - self.speed != SPEED_OFF): + self._speed != SPEED_OFF): return STATE_ON return SPEED_OFF @property - def speed_list(self): - """Get the list of available speeds.""" - return self._speed_list + def percentage(self): + """Return speed percentage of the fan.""" + if (self._speed == SPEED_OFF): + return 0 + + return ordered_list_item_to_percentage(self._speed_list, self._speed) @property - def speed(self): - """Return the current speed.""" - return self._speed + def speed_count(self): + """Return the number of speeds the fan supports.""" + return len(self._speed_list) @property def oscillating(self): @@ -207,12 +214,16 @@ def extra_state_attributes(self): 'commands_encoding': self._commands_encoding, } - async def async_set_speed(self, speed: str): - """Set the speed of the fan.""" - self._speed = speed + async def async_set_percentage(self, percentage: int): + """Set the desired speed for the fan.""" + if (percentage == 0): + self._speed = SPEED_OFF + else: + self._speed = percentage_to_ordered_list_item( + self._speed_list, percentage) - if not speed == SPEED_OFF: - self._last_on_speed = speed + if not self._speed == SPEED_OFF: + self._last_on_speed = self._speed await self.send_command() await self.async_update_ha_state() @@ -233,16 +244,17 @@ async def async_set_direction(self, direction: str): await self.async_update_ha_state() - async def async_turn_on(self, speed: str = None, **kwargs): + async def async_turn_on(self, percentage: int = None, **kwargs): """Turn on the fan.""" - if speed is None: - speed = self._last_on_speed or self._speed_list[1] + if percentage is None: + percentage = ordered_list_item_to_percentage( + self._speed_list, self._last_on_speed or self._speed_list[0]) - await self.async_set_speed(speed) + await self.async_set_percentage(percentage) async def async_turn_off(self): """Turn off the fan.""" - await self.async_set_speed(SPEED_OFF) + await self.async_set_percentage(0) async def send_command(self): async with self._temp_lock: @@ -280,4 +292,4 @@ async def _async_power_sensor_changed(self, entity_id, old_state, new_state): self._on_by_remote = False if self._speed != SPEED_OFF: self._speed = SPEED_OFF - await self.async_update_ha_state() + await self.async_update_ha_state() \ No newline at end of file diff --git a/custom_components/smartir/manifest.json b/custom_components/smartir/manifest.json index 5536795d..aa6c7aa4 100644 --- a/custom_components/smartir/manifest.json +++ b/custom_components/smartir/manifest.json @@ -5,11 +5,11 @@ "dependencies": [], "codeowners": ["@smartHomeHub"], "requirements": ["aiofiles==0.6.0"], - "homeassistant": "0.115.0", - "version": "1.17.4", + "homeassistant": "2022.2.0", + "version": "1.17.5", "updater": { - "version": "1.17.4", - "releaseNotes": "-- Fixed typo #764", + "version": "1.17.5", + "releaseNotes": "-- Compliance with the breaking change of the new fan model", "files": [ "__init__.py", "climate.py",