2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron switches."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
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-22 14:52:59 +00:00
|
|
|
from pylutron import Button, Keypad, Led, Lutron, Output
|
2024-01-07 16:48:23 +00:00
|
|
|
|
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-22 14:52:59 +00:00
|
|
|
from .entity import LutronDevice, LutronKeypad
|
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-22 14:52:59 +00:00
|
|
|
for area_name, keypad, scene, led in entry_data.scenes:
|
2020-04-20 13:44:55 +00:00
|
|
|
if led is not None:
|
2024-01-22 14:52:59 +00:00
|
|
|
entities.append(LutronLed(area_name, keypad, scene, led, entry_data.client))
|
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
|
2024-02-24 19:18:06 +00:00
|
|
|
_attr_name = None
|
2024-01-07 16:48:23 +00:00
|
|
|
|
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
|
|
|
|
2024-01-25 11:54:31 +00:00
|
|
|
def _request_state(self) -> None:
|
|
|
|
"""Request the state from the device."""
|
2024-03-17 09:58:14 +00:00
|
|
|
_ = self._lutron_device.level
|
2019-07-31 03:48:43 +00:00
|
|
|
|
2024-01-25 11:54:31 +00:00
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update the state attributes."""
|
|
|
|
self._attr_is_on = self._lutron_device.last_level() > 0
|
2020-04-20 13:44:55 +00:00
|
|
|
|
|
|
|
|
2024-01-22 14:52:59 +00:00
|
|
|
class LutronLed(LutronKeypad, 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,
|
2024-01-22 14:52:59 +00:00
|
|
|
keypad: Keypad,
|
2024-01-07 16:48:23 +00:00
|
|
|
scene_device: Button,
|
|
|
|
led_device: Led,
|
|
|
|
controller: Lutron,
|
|
|
|
) -> None:
|
2020-04-20 13:44:55 +00:00
|
|
|
"""Initialize the switch."""
|
2024-01-22 14:52:59 +00:00
|
|
|
super().__init__(area_name, led_device, controller, keypad)
|
|
|
|
self._keypad_name = keypad.name
|
|
|
|
self._attr_name = scene_device.name
|
2020-04-20 13:44:55 +00:00
|
|
|
|
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,
|
2024-01-22 14:52:59 +00:00
|
|
|
"scene": self._attr_name,
|
2020-04-20 13:44:55 +00:00
|
|
|
"led": self._lutron_device.name,
|
|
|
|
}
|
|
|
|
|
2024-01-25 11:54:31 +00:00
|
|
|
def _request_state(self) -> None:
|
|
|
|
"""Request the state from the device."""
|
2024-03-17 09:58:14 +00:00
|
|
|
_ = self._lutron_device.state
|
2024-01-25 11:54:31 +00:00
|
|
|
|
|
|
|
def _update_attrs(self) -> None:
|
|
|
|
"""Update the state attributes."""
|
|
|
|
self._attr_is_on = self._lutron_device.last_state
|