2017-01-25 08:11:37 +00:00
|
|
|
"""
|
|
|
|
Component for interacting with a Lutron RadioRA 2 system.
|
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/lutron/
|
2017-01-25 08:11:37 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2017-06-08 22:21:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
2017-01-25 08:11:37 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2017-03-16 06:08:47 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2018-12-25 08:33:03 +00:00
|
|
|
REQUIREMENTS = ['pylutron==0.2.0']
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2017-01-28 15:02:19 +00:00
|
|
|
DOMAIN = 'lutron'
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
LUTRON_CONTROLLER = 'lutron_controller'
|
|
|
|
LUTRON_DEVICES = 'lutron_devices'
|
|
|
|
|
2017-06-08 22:21:06 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
def setup(hass, base_config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Lutron component."""
|
2017-01-25 08:11:37 +00:00
|
|
|
from pylutron import Lutron
|
|
|
|
|
|
|
|
hass.data[LUTRON_CONTROLLER] = None
|
2018-12-25 08:33:03 +00:00
|
|
|
hass.data[LUTRON_DEVICES] = {'light': [],
|
|
|
|
'cover': [],
|
|
|
|
'switch': [],
|
|
|
|
'scene': []}
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
config = base_config.get(DOMAIN)
|
|
|
|
hass.data[LUTRON_CONTROLLER] = Lutron(
|
2017-07-11 03:52:37 +00:00
|
|
|
config[CONF_HOST], config[CONF_USERNAME], config[CONF_PASSWORD])
|
2017-06-08 22:21:06 +00:00
|
|
|
|
2017-01-25 08:11:37 +00:00
|
|
|
hass.data[LUTRON_CONTROLLER].load_xml_db()
|
|
|
|
hass.data[LUTRON_CONTROLLER].connect()
|
2017-06-08 22:21:06 +00:00
|
|
|
_LOGGER.info("Connected to main repeater at %s", config[CONF_HOST])
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
# Sort our devices into types
|
|
|
|
for area in hass.data[LUTRON_CONTROLLER].areas:
|
|
|
|
for output in area.outputs:
|
2018-01-13 18:11:20 +00:00
|
|
|
if output.type == 'SYSTEM_SHADE':
|
|
|
|
hass.data[LUTRON_DEVICES]['cover'].append((area.name, output))
|
2018-12-25 08:33:03 +00:00
|
|
|
elif output.is_dimmable:
|
2018-01-13 18:11:20 +00:00
|
|
|
hass.data[LUTRON_DEVICES]['light'].append((area.name, output))
|
2018-12-25 08:33:03 +00:00
|
|
|
else:
|
|
|
|
hass.data[LUTRON_DEVICES]['switch'].append((area.name, output))
|
|
|
|
for keypad in area.keypads:
|
|
|
|
for button in keypad.buttons:
|
|
|
|
# This is the best way to determine if a button does anything
|
|
|
|
# useful until pylutron is updated to provide information on
|
|
|
|
# which buttons actually control scenes.
|
|
|
|
for led in keypad.leds:
|
|
|
|
if (led.number == button.number and
|
|
|
|
button.name != 'Unknown Button' and
|
|
|
|
button.button_type in ('SingleAction', 'Toggle')):
|
|
|
|
hass.data[LUTRON_DEVICES]['scene'].append(
|
|
|
|
(area.name, button, led))
|
|
|
|
|
|
|
|
for component in ('light', 'cover', 'switch', 'scene'):
|
2017-01-25 08:11:37 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, None, base_config)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class LutronDevice(Entity):
|
|
|
|
"""Representation of a Lutron device entity."""
|
|
|
|
|
2017-04-12 07:52:01 +00:00
|
|
|
def __init__(self, area_name, lutron_device, controller):
|
2017-01-25 08:11:37 +00:00
|
|
|
"""Initialize the device."""
|
|
|
|
self._lutron_device = lutron_device
|
|
|
|
self._controller = controller
|
|
|
|
self._area_name = area_name
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-04-12 07:52:01 +00:00
|
|
|
"""Register callbacks."""
|
2018-12-25 08:33:03 +00:00
|
|
|
self.hass.async_add_executor_job(
|
|
|
|
self._lutron_device.subscribe,
|
|
|
|
self._update_callback,
|
|
|
|
None
|
2017-04-12 07:52:01 +00:00
|
|
|
)
|
2017-01-25 08:11:37 +00:00
|
|
|
|
2018-12-25 08:33:03 +00:00
|
|
|
def _update_callback(self, _device, _context, _event, _params):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Run when invoked by pylutron when the device state changes."""
|
2017-01-25 08:11:37 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2017-04-12 07:52:01 +00:00
|
|
|
return "{} {}".format(self._area_name, self._lutron_device.name)
|
2017-01-25 08:11:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|