2019-02-13 20:21:14 +00:00
|
|
|
"""Support for fans through the SmartThings cloud API."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Sequence
|
2021-01-28 09:09:16 +00:00
|
|
|
import math
|
2019-02-15 16:40:54 +00:00
|
|
|
|
2019-07-01 02:29:21 +00:00
|
|
|
from pysmartthings import Capability
|
|
|
|
|
2021-01-28 09:09:16 +00:00
|
|
|
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
|
|
|
|
from homeassistant.util.percentage import (
|
2021-02-19 07:05:09 +00:00
|
|
|
int_states_in_range,
|
2021-01-28 09:09:16 +00:00
|
|
|
percentage_to_ranged_value,
|
|
|
|
ranged_value_to_percentage,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
from . import SmartThingsEntity
|
|
|
|
from .const import DATA_BROKERS, DOMAIN
|
|
|
|
|
2021-01-28 09:09:16 +00:00
|
|
|
SPEED_RANGE = (1, 3) # off is not included
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Add fans for a config entry."""
|
|
|
|
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
SmartThingsFan(device)
|
|
|
|
for device in broker.devices.values()
|
|
|
|
if broker.any_assigned(device.device_id, "fan")
|
|
|
|
]
|
|
|
|
)
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
|
2021-03-18 13:31:38 +00:00
|
|
|
def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
|
2019-02-15 16:40:54 +00:00
|
|
|
"""Return all capabilities supported if minimum required are present."""
|
|
|
|
supported = [Capability.switch, Capability.fan_speed]
|
2019-02-02 22:04:29 +00:00
|
|
|
# Must have switch and fan_speed
|
2019-02-15 16:40:54 +00:00
|
|
|
if all(capability in capabilities for capability in supported):
|
|
|
|
return supported
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SmartThingsFan(SmartThingsEntity, FanEntity):
|
|
|
|
"""Define a SmartThings Fan."""
|
|
|
|
|
2021-01-28 09:09:16 +00:00
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set the speed percentage of the fan."""
|
|
|
|
if percentage is None:
|
|
|
|
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)
|
2019-02-02 22:04:29 +00:00
|
|
|
# State is set optimistically in the command above, therefore update
|
|
|
|
# the entity state ahead of receiving the confirming push updates
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-02 22:04:29 +00:00
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
|
|
|
speed: str = None,
|
|
|
|
percentage: int = None,
|
|
|
|
preset_mode: str = None,
|
|
|
|
**kwargs,
|
|
|
|
) -> None:
|
2019-02-02 22:04:29 +00:00
|
|
|
"""Turn the fan on."""
|
2021-01-28 09:09:16 +00:00
|
|
|
await self.async_set_percentage(percentage)
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
|
|
|
"""Turn the fan off."""
|
|
|
|
await self._device.switch_off(set_status=True)
|
|
|
|
# State is set optimistically in the command above, therefore update
|
|
|
|
# the entity state ahead of receiving the confirming push updates
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-02 22:04:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if fan is on."""
|
|
|
|
return self._device.status.switch
|
|
|
|
|
|
|
|
@property
|
2021-03-02 07:32:24 +00:00
|
|
|
def percentage(self) -> int:
|
2021-01-28 09:09:16 +00:00
|
|
|
"""Return the current speed percentage."""
|
|
|
|
return ranged_value_to_percentage(SPEED_RANGE, self._device.status.fan_speed)
|
2019-02-02 22:04:29 +00:00
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
2021-02-20 05:57:21 +00:00
|
|
|
def speed_count(self) -> int:
|
2021-02-19 07:05:09 +00:00
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
return int_states_in_range(SPEED_RANGE)
|
|
|
|
|
2019-02-02 22:04:29 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_SET_SPEED
|