diff --git a/homeassistant/components/insteon/fan.py b/homeassistant/components/insteon/fan.py index f9d1c381f49..3327f9df5eb 100644 --- a/homeassistant/components/insteon/fan.py +++ b/homeassistant/components/insteon/fan.py @@ -1,28 +1,24 @@ """Support for INSTEON fans via PowerLinc Modem.""" +import math + from pyinsteon.constants import FanSpeed from homeassistant.components.fan import ( DOMAIN as FAN_DOMAIN, - SPEED_HIGH, - SPEED_LOW, - SPEED_MEDIUM, - SPEED_OFF, SUPPORT_SET_SPEED, FanEntity, ) from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.util.percentage import ( + percentage_to_ranged_value, + ranged_value_to_percentage, +) from .const import SIGNAL_ADD_ENTITIES from .insteon_entity import InsteonEntity from .utils import async_add_insteon_entities -FAN_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] -SPEED_TO_VALUE = { - SPEED_OFF: FanSpeed.OFF, - SPEED_LOW: FanSpeed.LOW, - SPEED_MEDIUM: FanSpeed.MEDIUM, - SPEED_HIGH: FanSpeed.HIGH, -} +SPEED_RANGE = (1, FanSpeed.HIGH) # off is not included async def async_setup_entry(hass, config_entry, async_add_entities): @@ -43,33 +39,17 @@ class InsteonFanEntity(InsteonEntity, FanEntity): """An INSTEON fan entity.""" @property - def speed(self) -> str: - """Return the current speed.""" - if self._insteon_device_group.value == FanSpeed.HIGH: - return SPEED_HIGH - if self._insteon_device_group.value == FanSpeed.MEDIUM: - return SPEED_MEDIUM - if self._insteon_device_group.value == FanSpeed.LOW: - return SPEED_LOW - return SPEED_OFF - - @property - def speed_list(self) -> list: - """Get the list of available speeds.""" - return FAN_SPEEDS + def percentage(self) -> str: + """Return the current speed percentage.""" + if self._insteon_device_group.value is None: + return None + return ranged_value_to_percentage(SPEED_RANGE, self._insteon_device_group.value) @property def supported_features(self) -> int: """Flag supported features.""" return SUPPORT_SET_SPEED - # - # The fan entity model has changed to use percentages and preset_modes - # instead of speeds. - # - # Please review - # https://developers.home-assistant.io/docs/core/entity/fan/ - # async def async_turn_on( self, speed: str = None, @@ -78,18 +58,18 @@ class InsteonFanEntity(InsteonEntity, FanEntity): **kwargs, ) -> None: """Turn on the fan.""" - if speed is None: - speed = SPEED_MEDIUM - await self.async_set_speed(speed) + if percentage is None: + percentage = 50 + await self.async_set_percentage(percentage) async def async_turn_off(self, **kwargs) -> None: """Turn off the fan.""" await self._insteon_device.async_fan_off() - async def async_set_speed(self, speed: str) -> None: - """Set the speed of the fan.""" - fan_speed = SPEED_TO_VALUE[speed] - if fan_speed == FanSpeed.OFF: + async def async_set_percentage(self, percentage: int) -> None: + """Set the speed percentage of the fan.""" + if percentage == 0: await self._insteon_device.async_fan_off() else: - await self._insteon_device.async_fan_on(on_level=fan_speed) + on_level = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage)) + await self._insteon_device.async_fan_on(on_level=on_level)