2020-07-11 00:23:35 +00:00
|
|
|
"""Support for Bond fans."""
|
2020-07-11 01:20:50 +00:00
|
|
|
from typing import Any, Callable, List, Optional
|
2020-07-11 00:23:35 +00:00
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
from bond import DeviceTypes
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
from homeassistant.components.fan import (
|
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SPEED_OFF,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
2020-07-11 01:20:50 +00:00
|
|
|
from .entity import BondEntity
|
2020-07-12 16:31:53 +00:00
|
|
|
from .utils import BondDevice, BondHub
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up Bond fan devices."""
|
2020-07-12 16:31:53 +00:00
|
|
|
hub: BondHub = hass.data[DOMAIN][entry.entry_id]
|
2020-07-11 00:23:35 +00:00
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
devices = await hass.async_add_executor_job(hub.get_bond_devices)
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
fans = [
|
2020-07-12 16:31:53 +00:00
|
|
|
BondFan(hub, device)
|
2020-07-11 00:23:35 +00:00
|
|
|
for device in devices
|
2020-07-12 01:04:07 +00:00
|
|
|
if device.type == DeviceTypes.CEILING_FAN
|
2020-07-11 00:23:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(fans, True)
|
|
|
|
|
|
|
|
|
2020-07-11 01:20:50 +00:00
|
|
|
class BondFan(BondEntity, FanEntity):
|
2020-07-11 00:23:35 +00:00
|
|
|
"""Representation of a Bond fan."""
|
|
|
|
|
2020-07-12 16:31:53 +00:00
|
|
|
def __init__(self, hub: BondHub, device: BondDevice):
|
2020-07-11 00:23:35 +00:00
|
|
|
"""Create HA entity representing Bond fan."""
|
2020-07-12 16:31:53 +00:00
|
|
|
super().__init__(hub, device)
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
self._power: Optional[bool] = None
|
|
|
|
self._speed: Optional[int] = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
|
|
|
features = 0
|
|
|
|
if self._device.supports_command("SetSpeed"):
|
|
|
|
features |= SUPPORT_SET_SPEED
|
|
|
|
return features
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed(self) -> Optional[str]:
|
|
|
|
"""Return the current speed."""
|
|
|
|
if self._power is None:
|
|
|
|
return None
|
|
|
|
if self._power == 0:
|
|
|
|
return SPEED_OFF
|
|
|
|
|
|
|
|
return self.speed_list[self._speed] if self._speed is not None else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
|
|
|
"""Get the list of available speeds."""
|
|
|
|
return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Fetch assumed state of the fan from the hub using API."""
|
2020-07-12 16:31:53 +00:00
|
|
|
state: dict = self._hub.bond.getDeviceState(self._device.device_id)
|
2020-07-11 00:23:35 +00:00
|
|
|
self._power = state.get("power")
|
|
|
|
self._speed = state.get("speed")
|
|
|
|
|
|
|
|
def set_speed(self, speed: str) -> None:
|
|
|
|
"""Set the desired speed for the fan."""
|
|
|
|
speed_index = self.speed_list.index(speed)
|
2020-07-12 16:31:53 +00:00
|
|
|
self._hub.bond.setSpeed(self._device.device_id, speed=speed_index)
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
def turn_on(self, speed: Optional[str] = None, **kwargs) -> None:
|
|
|
|
"""Turn on the fan."""
|
|
|
|
if speed is not None:
|
|
|
|
self.set_speed(speed)
|
2020-07-12 16:31:53 +00:00
|
|
|
self._hub.bond.turnOn(self._device.device_id)
|
2020-07-11 00:23:35 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the fan off."""
|
2020-07-12 16:31:53 +00:00
|
|
|
self._hub.bond.turnOff(self._device.device_id)
|