Skip to content

Commit

Permalink
Add fan support to lutron_caseta (#29033)
Browse files Browse the repository at this point in the history
* Adding lutron_caseta fan controller. Updated to pylutron 0.5.1

* Accidental import remain

* updating for black formatting

* Fix blank spaces

* Moving third party import

* update to 0.5.1 to pass checks

* fix deletion of new line

* Update fan.py

* Update with dev branch, update from comments, added mediumhigh
  • Loading branch information
djj211 authored and MartinHjelmare committed Nov 28, 2019
1 parent f1a4e21 commit 665613e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/lutron_caseta/__init__.py
Expand Up @@ -32,7 +32,7 @@
extra=vol.ALLOW_EXTRA,
)

LUTRON_CASETA_COMPONENTS = ["light", "switch", "cover", "scene"]
LUTRON_CASETA_COMPONENTS = ["light", "switch", "cover", "scene", "fan"]


async def async_setup(hass, base_config):
Expand Down
96 changes: 96 additions & 0 deletions homeassistant/components/lutron_caseta/fan.py
@@ -0,0 +1,96 @@
"""Support for Lutron Caseta fans."""
import logging

from pylutron_caseta import FAN_OFF, FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_HIGH

from homeassistant.components.fan import (
SUPPORT_SET_SPEED,
FanEntity,
DOMAIN,
SPEED_HIGH,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_OFF,
)

from . import LUTRON_CASETA_SMARTBRIDGE, LutronCasetaDevice

_LOGGER = logging.getLogger(__name__)

VALUE_TO_SPEED = {
None: SPEED_OFF,
FAN_OFF: SPEED_OFF,
FAN_LOW: SPEED_LOW,
FAN_MEDIUM: SPEED_MEDIUM,
FAN_MEDIUM_HIGH: SPEED_MEDIUM,
FAN_HIGH: SPEED_HIGH,
}

SPEED_TO_VALUE = {
SPEED_OFF: FAN_OFF,
SPEED_LOW: FAN_LOW,
SPEED_MEDIUM: FAN_MEDIUM,
SPEED_HIGH: FAN_HIGH,
}

FAN_SPEEDS = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up Lutron fan."""
entities = []
bridge = hass.data[LUTRON_CASETA_SMARTBRIDGE]
fan_devices = bridge.get_devices_by_domain(DOMAIN)

for fan_device in fan_devices:
entity = LutronCasetaFan(fan_device, bridge)
entities.append(entity)

async_add_entities(entities, True)


class LutronCasetaFan(LutronCasetaDevice, FanEntity):
"""Representation of a Lutron Caseta fan. Including Fan Speed."""

@property
def speed(self) -> str:
"""Return the current speed."""
return VALUE_TO_SPEED[self._device["fan_speed"]]

@property
def speed_list(self) -> list:
"""Get the list of available speeds."""
return FAN_SPEEDS

@property
def supported_features(self) -> int:
"""Flag supported features. Speed Only."""
return SUPPORT_SET_SPEED

async def async_turn_on(self, speed: str = None, **kwargs):
"""Turn the fan on."""
if speed is None:
speed = SPEED_MEDIUM
await self.async_set_speed(speed)

async def async_turn_off(self, **kwargs):
"""Turn the fan off."""
await self.async_set_speed(SPEED_OFF)

async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
self._smartbridge.set_fan(self.device_id, SPEED_TO_VALUE[speed])

@property
def is_on(self):
"""Return true if device is on."""
return VALUE_TO_SPEED[self._device["fan_speed"]] in [
SPEED_LOW,
SPEED_MEDIUM,
SPEED_HIGH,
]

async def async_update(self):
"""Update when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug("State of this lutron fan device is %s", self._device)

0 comments on commit 665613e

Please sign in to comment.