2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron switches."""
|
2022-01-03 18:23:11 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-05 15:39:14 +00:00
|
|
|
from collections.abc import Mapping
|
2022-09-01 12:14:31 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2024-01-07 16:48:23 +00:00
|
|
|
from pylutron import Button, Led, Lutron, Output
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2024-01-05 15:39:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-01-03 18:23:11 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2024-01-05 21:04:10 +00:00
|
|
|
from . import DOMAIN, LutronData
|
2024-01-05 19:38:02 +00:00
|
|
|
from .entity import LutronDevice
|
2018-12-25 08:33:03 +00:00
|
|
|
|
|
|
|
|
2024-01-05 15:39:14 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 18:23:11 +00:00
|
|
|
hass: HomeAssistant,
|
2024-01-05 15:39:14 +00:00
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 18:23:11 +00:00
|
|
|
) -> None:
|
2024-01-05 15:39:14 +00:00
|
|
|
"""Set up the Lutron switch platform.
|
|
|
|
|
|
|
|
Adds switches from the Main Repeater associated with the config_entry as
|
|
|
|
switch entities.
|
|
|
|
"""
|
2024-01-05 21:04:10 +00:00
|
|
|
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
entities: list[SwitchEntity] = []
|
2020-04-20 13:44:55 +00:00
|
|
|
|
|
|
|
# Add Lutron Switches
|
2024-01-05 21:04:10 +00:00
|
|
|
for area_name, device in entry_data.switches:
|
|
|
|
entities.append(LutronSwitch(area_name, device, entry_data.client))
|
2018-12-25 08:33:03 +00:00
|
|
|
|
2020-04-20 13:44:55 +00:00
|
|
|
# Add the indicator LEDs for scenes (keypad buttons)
|
2024-01-05 21:04:10 +00:00
|
|
|
for area_name, keypad_name, scene, led in entry_data.scenes:
|
2020-04-20 13:44:55 +00:00
|
|
|
if led is not None:
|
2024-01-05 21:04:10 +00:00
|
|
|
entities.append(
|
|
|
|
LutronLed(area_name, keypad_name, scene, led, entry_data.client)
|
2020-04-20 13:44:55 +00:00
|
|
|
)
|
2024-01-05 15:39:14 +00:00
|
|
|
async_add_entities(entities, True)
|
2018-12-25 08:33:03 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class LutronSwitch(LutronDevice, SwitchEntity):
|
2018-12-25 08:33:03 +00:00
|
|
|
"""Representation of a Lutron Switch."""
|
|
|
|
|
2024-01-07 16:48:23 +00:00
|
|
|
_lutron_device: Output
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, area_name: str, lutron_device: Output, controller: Lutron
|
|
|
|
) -> None:
|
2019-07-31 03:48:43 +00:00
|
|
|
"""Initialize the switch."""
|
2019-08-17 21:50:15 +00:00
|
|
|
self._prev_state = None
|
2019-07-31 03:48:43 +00:00
|
|
|
super().__init__(area_name, lutron_device, controller)
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2018-12-25 08:33:03 +00:00
|
|
|
"""Turn the switch on."""
|
|
|
|
self._lutron_device.level = 100
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2018-12-25 08:33:03 +00:00
|
|
|
"""Turn the switch off."""
|
|
|
|
self._lutron_device.level = 0
|
|
|
|
|
|
|
|
@property
|
2024-01-05 15:39:14 +00:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
2018-12-25 08:33:03 +00:00
|
|
|
"""Return the state attributes."""
|
2020-10-05 10:51:48 +00:00
|
|
|
return {"lutron_integration_id": self._lutron_device.id}
|
2018-12-25 08:33:03 +00:00
|
|
|
|
|
|
|
@property
|
2024-01-05 15:39:14 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-12-25 08:33:03 +00:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._lutron_device.last_level() > 0
|
2019-07-31 03:48:43 +00:00
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def update(self) -> None:
|
2019-07-31 03:48:43 +00:00
|
|
|
"""Call when forcing a refresh of the device."""
|
2019-08-17 21:50:15 +00:00
|
|
|
if self._prev_state is None:
|
|
|
|
self._prev_state = self._lutron_device.level > 0
|
2020-04-20 13:44:55 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class LutronLed(LutronDevice, SwitchEntity):
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Representation of a Lutron Keypad LED."""
|
|
|
|
|
2024-01-07 16:48:23 +00:00
|
|
|
_lutron_device: Led
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
area_name: str,
|
|
|
|
keypad_name: str,
|
|
|
|
scene_device: Button,
|
|
|
|
led_device: Led,
|
|
|
|
controller: Lutron,
|
|
|
|
) -> None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Initialize the switch."""
|
|
|
|
self._keypad_name = keypad_name
|
|
|
|
self._scene_name = scene_device.name
|
|
|
|
super().__init__(area_name, led_device, controller)
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Turn the LED on."""
|
|
|
|
self._lutron_device.state = 1
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Turn the LED off."""
|
|
|
|
self._lutron_device.state = 0
|
|
|
|
|
|
|
|
@property
|
2024-01-05 15:39:14 +00:00
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Return the state attributes."""
|
2020-10-05 10:51:48 +00:00
|
|
|
return {
|
2020-04-20 13:44:55 +00:00
|
|
|
"keypad": self._keypad_name,
|
|
|
|
"scene": self._scene_name,
|
|
|
|
"led": self._lutron_device.name,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2024-01-05 15:39:14 +00:00
|
|
|
def is_on(self) -> bool:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._lutron_device.last_state
|
|
|
|
|
|
|
|
@property
|
2022-09-01 12:14:31 +00:00
|
|
|
def name(self) -> str:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Return the name of the LED."""
|
|
|
|
return f"{self._area_name} {self._keypad_name}: {self._scene_name} LED"
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
def update(self) -> None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Call when forcing a refresh of the device."""
|
|
|
|
# The following property getter actually triggers an update in Lutron
|
|
|
|
self._lutron_device.state # pylint: disable=pointless-statement
|