Skip to content
16 changes: 11 additions & 5 deletions homeassistant/components/lutron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# Attribute on events that indicates what action was taken with the button.
ATTR_ACTION = "action"
ATTR_FULL_ID = "full_id"

CONFIG_SCHEMA = vol.Schema(
{
Expand All @@ -37,7 +38,6 @@

def setup(hass, base_config):
"""Set up the Lutron component."""

hass.data[LUTRON_BUTTONS] = []
hass.data[LUTRON_CONTROLLER] = None
hass.data[LUTRON_DEVICES] = {
Expand Down Expand Up @@ -84,7 +84,9 @@ def setup(hass, base_config):
(area.name, keypad.name, button, led)
)

hass.data[LUTRON_BUTTONS].append(LutronButton(hass, keypad, button))
hass.data[LUTRON_BUTTONS].append(
LutronButton(hass, area.name, keypad, button)
)
if area.occupancy_group is not None:
hass.data[LUTRON_DEVICES]["binary_sensor"].append(
(area.name, area.occupancy_group)
Expand Down Expand Up @@ -133,21 +135,25 @@ class LutronButton:
represented as an entity; it simply fires events.
"""

def __init__(self, hass, keypad, button):
def __init__(self, hass, area_name, keypad, button):
"""Register callback for activity on the button."""
name = f"{keypad.name}: {button.name}"
self._hass = hass
self._has_release_event = (
button.button_type is not None and "RaiseLower" in button.button_type
)
self._id = slugify(name)
self._keypad = keypad
self._area_name = area_name
self._button_name = button.name
self._button = button
self._event = "lutron_event"
self._full_id = slugify(f"{area_name} {keypad.name}: {button.name}")

button.subscribe(self.button_callback, None)

def button_callback(self, button, context, event, params):
"""Fire an event about a button being pressed or released."""

# Events per button type:
# RaiseLower -> pressed/released
# SingleAction -> single
Expand All @@ -161,5 +167,5 @@ def button_callback(self, button, context, event, params):
action = "single"

if action:
data = {ATTR_ID: self._id, ATTR_ACTION: action}
data = {ATTR_ID: self._id, ATTR_ACTION: action, ATTR_FULL_ID: self._full_id}
self._hass.bus.fire(self._event, data)
57 changes: 57 additions & 0 deletions homeassistant/components/lutron/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Lutron switches."""
devs = []

# Add Lutron Switches
for (area_name, device) in hass.data[LUTRON_DEVICES]["switch"]:
dev = LutronSwitch(area_name, device, hass.data[LUTRON_CONTROLLER])
devs.append(dev)

# Add the indicator LEDs for scenes (keypad buttons)
for scene_data in hass.data[LUTRON_DEVICES]["scene"]:
(area_name, keypad_name, scene, led) = scene_data
if led is not None:
led = LutronLed(
area_name, keypad_name, scene, led, hass.data[LUTRON_CONTROLLER]
)
devs.append(led)

add_entities(devs, True)


Expand Down Expand Up @@ -50,3 +61,49 @@ def update(self):
"""Call when forcing a refresh of the device."""
if self._prev_state is None:
self._prev_state = self._lutron_device.level > 0


class LutronLed(LutronDevice, SwitchDevice):
"""Representation of a Lutron Keypad LED."""

def __init__(self, area_name, keypad_name, scene_device, led_device, controller):
"""Initialize the switch."""
self._keypad_name = keypad_name
self._scene_name = scene_device.name
super().__init__(area_name, led_device, controller)

def turn_on(self, **kwargs):
"""Turn the LED on."""
self._lutron_device.state = 1

def turn_off(self, **kwargs):
"""Turn the LED off."""
self._lutron_device.state = 0

@property
def device_state_attributes(self):
"""Return the state attributes."""
attr = {
"keypad": self._keypad_name,
"scene": self._scene_name,
"led": self._lutron_device.name,
}
return attr

@property
def is_on(self):
"""Return true if device is on."""
return self._lutron_device.last_state

@property
def name(self):
"""Return the name of the LED."""
return f"{self._area_name} {self._keypad_name}: {self._scene_name} LED"

def update(self):
"""Call when forcing a refresh of the device."""
if self._lutron_device.last_state is not None:
return

# The following property getter actually triggers an update in Lutron
self._lutron_device.state # pylint: disable=pointless-statement