2019-04-03 15:40:03 +00:00
|
|
|
"""Demo fan platform that has a fake fan."""
|
2019-03-24 03:22:35 +00:00
|
|
|
from homeassistant.components.fan import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SPEED_HIGH,
|
|
|
|
SPEED_LOW,
|
|
|
|
SPEED_MEDIUM,
|
|
|
|
SUPPORT_DIRECTION,
|
|
|
|
SUPPORT_OSCILLATE,
|
|
|
|
SUPPORT_SET_SPEED,
|
|
|
|
FanEntity,
|
|
|
|
)
|
2019-12-08 16:59:27 +00:00
|
|
|
from homeassistant.const import STATE_OFF
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-06-12 05:12:56 +00:00
|
|
|
FULL_SUPPORT = SUPPORT_SET_SPEED | SUPPORT_OSCILLATE | SUPPORT_DIRECTION
|
|
|
|
LIMITED_SUPPORT = SUPPORT_SET_SPEED
|
2016-08-27 20:53:12 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the demo fan platform."""
|
2019-11-13 15:37:31 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
|
|
|
DemoFan(hass, "Living Room Fan", FULL_SUPPORT),
|
|
|
|
DemoFan(hass, "Ceiling Fan", LIMITED_SUPPORT),
|
|
|
|
]
|
|
|
|
)
|
2016-08-27 20:53:12 +00:00
|
|
|
|
|
|
|
|
2019-11-13 15:37:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Demo config entry."""
|
|
|
|
await async_setup_platform(hass, {}, async_add_entities)
|
|
|
|
|
|
|
|
|
2016-08-27 20:53:12 +00:00
|
|
|
class DemoFan(FanEntity):
|
|
|
|
"""A demonstration fan component."""
|
|
|
|
|
2017-06-12 05:12:56 +00:00
|
|
|
def __init__(self, hass, name: str, supported_features: int) -> None:
|
2016-08-27 20:53:12 +00:00
|
|
|
"""Initialize the entity."""
|
|
|
|
self.hass = hass
|
2017-06-12 05:12:56 +00:00
|
|
|
self._supported_features = supported_features
|
|
|
|
self._speed = STATE_OFF
|
|
|
|
self.oscillating = None
|
2019-09-02 04:42:57 +00:00
|
|
|
self._direction = None
|
2016-08-27 20:53:12 +00:00
|
|
|
self._name = name
|
|
|
|
|
2017-06-12 05:12:56 +00:00
|
|
|
if supported_features & SUPPORT_OSCILLATE:
|
|
|
|
self.oscillating = False
|
|
|
|
if supported_features & SUPPORT_DIRECTION:
|
2019-09-02 04:42:57 +00:00
|
|
|
self._direction = "forward"
|
2017-06-12 05:12:56 +00:00
|
|
|
|
2016-08-27 20:53:12 +00:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
"""Get entity name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed for a demo fan."""
|
|
|
|
return False
|
|
|
|
|
2017-01-14 06:08:13 +00:00
|
|
|
@property
|
|
|
|
def speed(self) -> str:
|
|
|
|
"""Return the current speed."""
|
|
|
|
return self._speed
|
|
|
|
|
2016-08-27 20:53:12 +00:00
|
|
|
@property
|
|
|
|
def speed_list(self) -> list:
|
|
|
|
"""Get the list of available speeds."""
|
2017-01-21 06:21:28 +00:00
|
|
|
return [STATE_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
|
2016-08-27 20:53:12 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, speed: str = None, **kwargs) -> None:
|
2016-08-27 20:53:12 +00:00
|
|
|
"""Turn on the entity."""
|
2017-02-02 20:07:00 +00:00
|
|
|
if speed is None:
|
|
|
|
speed = SPEED_MEDIUM
|
2016-08-27 20:53:12 +00:00
|
|
|
self.set_speed(speed)
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs) -> None:
|
2016-08-27 20:53:12 +00:00
|
|
|
"""Turn off the entity."""
|
|
|
|
self.oscillate(False)
|
|
|
|
self.set_speed(STATE_OFF)
|
|
|
|
|
|
|
|
def set_speed(self, speed: str) -> None:
|
|
|
|
"""Set the speed of the fan."""
|
2017-01-14 06:08:13 +00:00
|
|
|
self._speed = speed
|
2017-02-02 20:07:00 +00:00
|
|
|
self.schedule_update_ha_state()
|
2017-01-14 06:08:13 +00:00
|
|
|
|
|
|
|
def set_direction(self, direction: str) -> None:
|
|
|
|
"""Set the direction of the fan."""
|
2019-09-02 04:42:57 +00:00
|
|
|
self._direction = direction
|
2017-02-02 20:07:00 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-08-27 20:53:12 +00:00
|
|
|
|
|
|
|
def oscillate(self, oscillating: bool) -> None:
|
|
|
|
"""Set oscillation."""
|
|
|
|
self.oscillating = oscillating
|
2017-02-02 20:07:00 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-08-27 20:53:12 +00:00
|
|
|
|
2017-01-14 06:08:13 +00:00
|
|
|
@property
|
|
|
|
def current_direction(self) -> str:
|
|
|
|
"""Fan direction."""
|
2019-09-02 04:42:57 +00:00
|
|
|
return self._direction
|
2017-01-14 06:08:13 +00:00
|
|
|
|
2016-08-27 20:53:12 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Flag supported features."""
|
2017-06-12 05:12:56 +00:00
|
|
|
return self._supported_features
|