Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Lutron Caseta switches."""
2 
3 from typing import Any
4 
5 from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from .entity import LutronCasetaUpdatableEntity
11 
12 
14  hass: HomeAssistant,
15  config_entry: ConfigEntry,
16  async_add_entities: AddEntitiesCallback,
17 ) -> None:
18  """Set up the Lutron Caseta switch platform.
19 
20  Adds switches from the Caseta bridge associated with the config_entry as
21  switch entities.
22  """
23  data = config_entry.runtime_data
24  bridge = data.bridge
25  switch_devices = bridge.get_devices_by_domain(SWITCH_DOMAIN)
27  LutronCasetaLight(switch_device, data) for switch_device in switch_devices
28  )
29 
30 
32  """Representation of a Lutron Caseta switch."""
33 
34  def __init__(self, device, data):
35  """Init a button entity."""
36 
37  super().__init__(device, data)
38  self._enabled_default_enabled_default = True
39 
40  if "parent_device" not in device:
41  return
42 
43  keypads = data.keypad_data.keypads
44  parent_keypad = keypads[device["parent_device"]]
45  parent_device_info = parent_keypad["device_info"]
46  # Append the child device name to the end of the parent keypad name to create the entity name
47  self._attr_name_attr_name_attr_name = f'{parent_device_info["name"]} {device["device_name"]}'
48  # Set the device_info to the same as the Parent Keypad
49  # The entities will be nested inside the keypad device
50  self._attr_device_info_attr_device_info_attr_device_info = parent_device_info
51 
52  async def async_turn_on(self, **kwargs: Any) -> None:
53  """Turn the switch on."""
54  await self._smartbridge_smartbridge.turn_on(self.device_iddevice_id)
55 
56  async def async_turn_off(self, **kwargs: Any) -> None:
57  """Turn the switch off."""
58  await self._smartbridge_smartbridge.turn_off(self.device_iddevice_id)
59 
60  @property
61  def is_on(self) -> bool:
62  """Return true if device is on."""
63  return self._device_device_device["current_state"] > 0
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:17