Update smartthings to use new fan entity model (#45592)

This commit is contained in:
J. Nick Koston 2021-01-28 03:09:16 -06:00 committed by GitHub
parent 0441960ffd
commit 3896e81db7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,22 +1,19 @@
"""Support for fans through the SmartThings cloud API.""" """Support for fans through the SmartThings cloud API."""
import math
from typing import Optional, Sequence from typing import Optional, Sequence
from pysmartthings import Capability from pysmartthings import Capability
from homeassistant.components.fan import ( from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
SPEED_HIGH, from homeassistant.util.percentage import (
SPEED_LOW, percentage_to_ranged_value,
SPEED_MEDIUM, ranged_value_to_percentage,
SPEED_OFF,
SUPPORT_SET_SPEED,
FanEntity,
) )
from . import SmartThingsEntity from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN from .const import DATA_BROKERS, DOMAIN
VALUE_TO_SPEED = {0: SPEED_OFF, 1: SPEED_LOW, 2: SPEED_MEDIUM, 3: SPEED_HIGH} SPEED_RANGE = (1, 3) # off is not included
SPEED_TO_VALUE = {v: k for k, v in VALUE_TO_SPEED.items()}
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
@ -42,21 +39,19 @@ def get_capabilities(capabilities: Sequence[str]) -> Optional[Sequence[str]]:
class SmartThingsFan(SmartThingsEntity, FanEntity): class SmartThingsFan(SmartThingsEntity, FanEntity):
"""Define a SmartThings Fan.""" """Define a SmartThings Fan."""
async def async_set_speed(self, speed: str): async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan.""" """Set the speed percentage of the fan."""
value = SPEED_TO_VALUE[speed] if percentage is None:
await self._device.set_fan_speed(value, set_status=True) await self._device.switch_on(set_status=True)
elif percentage == 0:
await self._device.switch_off(set_status=True)
else:
value = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
await self._device.set_fan_speed(value, set_status=True)
# State is set optimistically in the command above, therefore update # State is set optimistically in the command above, therefore update
# the entity state ahead of receiving the confirming push updates # the entity state ahead of receiving the confirming push updates
self.async_write_ha_state() self.async_write_ha_state()
#
# 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,
@ -65,14 +60,7 @@ class SmartThingsFan(SmartThingsEntity, FanEntity):
**kwargs, **kwargs,
) -> None: ) -> None:
"""Turn the fan on.""" """Turn the fan on."""
if speed is not None: await self.async_set_percentage(percentage)
value = SPEED_TO_VALUE[speed]
await self._device.set_fan_speed(value, set_status=True)
else:
await self._device.switch_on(set_status=True)
# State is set optimistically in the commands above, therefore update
# the entity state ahead of receiving the confirming push updates
self.async_write_ha_state()
async def async_turn_off(self, **kwargs) -> None: async def async_turn_off(self, **kwargs) -> None:
"""Turn the fan off.""" """Turn the fan off."""
@ -87,14 +75,9 @@ class SmartThingsFan(SmartThingsEntity, FanEntity):
return self._device.status.switch return self._device.status.switch
@property @property
def speed(self) -> str: def percentage(self) -> str:
"""Return the current speed.""" """Return the current speed percentage."""
return VALUE_TO_SPEED[self._device.status.fan_speed] return ranged_value_to_percentage(SPEED_RANGE, self._device.status.fan_speed)
@property
def speed_list(self) -> list:
"""Get the list of available speeds."""
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
@property @property
def supported_features(self) -> int: def supported_features(self) -> int: