Update insteon to use new fan entity model (#45767)

This commit is contained in:
J. Nick Koston 2021-01-31 01:13:55 -10:00 committed by GitHub
parent 275946b96d
commit f372bcf306
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,28 +1,24 @@
"""Support for INSTEON fans via PowerLinc Modem.""" """Support for INSTEON fans via PowerLinc Modem."""
import math
from pyinsteon.constants import FanSpeed from pyinsteon.constants import FanSpeed
from homeassistant.components.fan import ( from homeassistant.components.fan import (
DOMAIN as FAN_DOMAIN, DOMAIN as FAN_DOMAIN,
SPEED_HIGH,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_OFF,
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
FanEntity, FanEntity,
) )
from homeassistant.helpers.dispatcher import async_dispatcher_connect 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 .const import SIGNAL_ADD_ENTITIES
from .insteon_entity import InsteonEntity from .insteon_entity import InsteonEntity
from .utils import async_add_insteon_entities from .utils import async_add_insteon_entities
FAN_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] SPEED_RANGE = (1, FanSpeed.HIGH) # off is not included
SPEED_TO_VALUE = {
SPEED_OFF: FanSpeed.OFF,
SPEED_LOW: FanSpeed.LOW,
SPEED_MEDIUM: FanSpeed.MEDIUM,
SPEED_HIGH: FanSpeed.HIGH,
}
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -43,33 +39,17 @@ class InsteonFanEntity(InsteonEntity, FanEntity):
"""An INSTEON fan entity.""" """An INSTEON fan entity."""
@property @property
def speed(self) -> str: def percentage(self) -> str:
"""Return the current speed.""" """Return the current speed percentage."""
if self._insteon_device_group.value == FanSpeed.HIGH: if self._insteon_device_group.value is None:
return SPEED_HIGH return None
if self._insteon_device_group.value == FanSpeed.MEDIUM: return ranged_value_to_percentage(SPEED_RANGE, self._insteon_device_group.value)
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
@property @property
def supported_features(self) -> int: def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
return SUPPORT_SET_SPEED 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( async def async_turn_on(
self, self,
speed: str = None, speed: str = None,
@ -78,18 +58,18 @@ class InsteonFanEntity(InsteonEntity, FanEntity):
**kwargs, **kwargs,
) -> None: ) -> None:
"""Turn on the fan.""" """Turn on the fan."""
if speed is None: if percentage is None:
speed = SPEED_MEDIUM percentage = 50
await self.async_set_speed(speed) await self.async_set_percentage(percentage)
async def async_turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs) -> None:
"""Turn off the fan.""" """Turn off the fan."""
await self._insteon_device.async_fan_off() await self._insteon_device.async_fan_off()
async def async_set_speed(self, speed: str) -> None: async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan.""" """Set the speed percentage of the fan."""
fan_speed = SPEED_TO_VALUE[speed] if percentage == 0:
if fan_speed == FanSpeed.OFF:
await self._insteon_device.async_fan_off() await self._insteon_device.async_fan_off()
else: 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)