2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Lutron Caseta switches."""
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchEntity
|
2022-01-03 18:23:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-05-18 20:35:35 +00:00
|
|
|
from . import LutronCasetaDeviceUpdatableEntity
|
2022-06-15 06:32:38 +00:00
|
|
|
from .const import DOMAIN as CASETA_DOMAIN
|
|
|
|
from .models import LutronCasetaData
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:23:11 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-05-11 09:05:13 +00:00
|
|
|
"""Set up the Lutron Caseta switch platform.
|
|
|
|
|
|
|
|
Adds switches from the Caseta bridge associated with the config_entry as
|
|
|
|
switch entities.
|
|
|
|
"""
|
2022-06-15 06:32:38 +00:00
|
|
|
data: LutronCasetaData = hass.data[CASETA_DOMAIN][config_entry.entry_id]
|
|
|
|
bridge = data.bridge
|
2017-09-05 09:30:36 +00:00
|
|
|
switch_devices = bridge.get_devices_by_domain(DOMAIN)
|
2022-05-18 20:35:35 +00:00
|
|
|
async_add_entities(
|
2022-10-09 18:39:12 +00:00
|
|
|
LutronCasetaLight(switch_device, data) for switch_device in switch_devices
|
2022-05-18 20:35:35 +00:00
|
|
|
)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
|
2022-05-18 20:35:35 +00:00
|
|
|
class LutronCasetaLight(LutronCasetaDeviceUpdatableEntity, SwitchEntity):
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Representation of a Lutron Caseta switch."""
|
|
|
|
|
2022-10-12 20:29:28 +00:00
|
|
|
def __init__(self, device, data):
|
|
|
|
"""Init a button entity."""
|
|
|
|
|
|
|
|
super().__init__(device, data)
|
|
|
|
self._enabled_default = True
|
|
|
|
|
|
|
|
if "parent_device" not in device:
|
|
|
|
return
|
|
|
|
|
2022-10-23 19:57:04 +00:00
|
|
|
keypads = data.keypad_data.keypads
|
|
|
|
parent_keypad = keypads[device["parent_device"]]
|
|
|
|
parent_device_info = parent_keypad["device_info"]
|
2022-10-12 20:29:28 +00:00
|
|
|
# Append the child device name to the end of the parent keypad name to create the entity name
|
|
|
|
self._attr_name = f'{parent_device_info["name"]} {device["device_name"]}'
|
|
|
|
# Set the device_info to the same as the Parent Keypad
|
|
|
|
# The entities will be nested inside the keypad device
|
|
|
|
self._attr_device_info = parent_device_info
|
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Turn the switch on."""
|
2020-10-03 20:06:23 +00:00
|
|
|
await self._smartbridge.turn_on(self.device_id)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
2022-09-01 12:14:31 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Turn the switch off."""
|
2020-10-03 20:06:23 +00:00
|
|
|
await self._smartbridge.turn_off(self.device_id)
|
2017-03-23 00:18:14 +00:00
|
|
|
|
|
|
|
@property
|
2022-09-01 12:14:31 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-03-23 00:18:14 +00:00
|
|
|
"""Return true if device is on."""
|
2019-11-26 17:06:14 +00:00
|
|
|
return self._device["current_state"] > 0
|