Skip to content
8 changes: 6 additions & 2 deletions homeassistant/components/lutron_caseta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
CONF_KEYFILE = 'keyfile'
CONF_CERTFILE = 'certfile'
CONF_CA_CERTS = 'ca_certs'
CONF_CERT_REQUIRED = 'cert_required'

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_KEYFILE): cv.string,
vol.Required(CONF_CERTFILE): cv.string,
vol.Required(CONF_CA_CERTS): cv.string,
vol.Optional(CONF_CERT_REQUIRED, default=False): cv.boolean,
})
}, extra=vol.ALLOW_EXTRA)

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


Expand All @@ -40,9 +42,11 @@ async def async_setup(hass, base_config):
keyfile = hass.config.path(config[CONF_KEYFILE])
certfile = hass.config.path(config[CONF_CERTFILE])
ca_certs = hass.config.path(config[CONF_CA_CERTS])
cert_required = config[CONF_CERT_REQUIRED]

bridge = Smartbridge.create_tls(
hostname=config[CONF_HOST], keyfile=keyfile, certfile=certfile,
ca_certs=ca_certs)
ca_certs=ca_certs, cert_required=cert_required)
hass.data[LUTRON_CASETA_SMARTBRIDGE] = bridge
await bridge.connect()
if not hass.data[LUTRON_CASETA_SMARTBRIDGE].is_connected():
Expand Down
82 changes: 82 additions & 0 deletions homeassistant/components/lutron_caseta/fan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Support for Lutron Caseta fans."""
import logging

from homeassistant.components.fan import (
SUPPORT_SET_SPEED, FanEntity, DOMAIN)

from . import LUTRON_CASETA_SMARTBRIDGE, LutronCasetaDevice

_LOGGER = logging.getLogger(__name__)

LUTRON_SPEED_OFF = 'Off'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only speeds in the base fan component are allowed to be returned in the speed list or accepted as set speed argument.

We can build two dicts to map between home assistant speeds and device speeds. Make a best effort. It might not be a perfect map.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare I added the two dictionaries to map between speeds and devices (modeled off some other fan integrations). The base fan component does not have a "MediumHigh" field to map to. Not sure if you prefer that I take the approach that I did, or if you prefer that I add "mediumhigh" to the base component?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't add new speeds without approval in an issue in our architecture repo.

Please remove all speeds not currently in the base fan component.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare Understood. I will open an issue. I removed the speed not in the base fan component.

LUTRON_SPEED_LOW = 'Low'
LUTRON_SPEED_MEDIUM = 'Medium'
LUTRON_SPEED_MEDIUMHIGH = "MediumHigh"
LUTRON_SPEED_HIGH = 'High'


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

for fan_device in fan_devices:
dev = LutronCasetaFan(fan_device, bridge)
devs.append(dev)

async_add_entities(devs, True)
return True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is checking this return value. We can remove the statement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare return value removed.



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

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

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

Note: The default Hass Speeds were all lower case
and missing MediumHigh. Lutron Case and fan
speeds specified instead.
"""
return [LUTRON_SPEED_OFF, LUTRON_SPEED_LOW, LUTRON_SPEED_MEDIUM,
LUTRON_SPEED_MEDIUMHIGH, LUTRON_SPEED_HIGH]

@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 = LUTRON_SPEED_MEDIUMHIGH
await self.async_set_speed(speed)

async def async_turn_off(self, **kwargs):
"""Turn the fan off."""
await self.async_set_speed(LUTRON_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)

@property
def is_on(self):
"""Return true if device is on."""
return self._state["fan_speed"] in [LUTRON_SPEED_LOW,
LUTRON_SPEED_MEDIUM,
LUTRON_SPEED_MEDIUMHIGH,
LUTRON_SPEED_HIGH]

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