Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Lutron switches."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from pylutron import Button, Keypad, Led, Lutron, Output
9 
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import DOMAIN, LutronData
16 from .entity import LutronDevice, LutronKeypad
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the Lutron switch platform.
25 
26  Adds switches from the Main Repeater associated with the config_entry as
27  switch entities.
28  """
29  entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
30  entities: list[SwitchEntity] = []
31 
32  # Add Lutron Switches
33  for area_name, device in entry_data.switches:
34  entities.append(LutronSwitch(area_name, device, entry_data.client))
35 
36  # Add the indicator LEDs for scenes (keypad buttons)
37  for area_name, keypad, scene, led in entry_data.scenes:
38  if led is not None:
39  entities.append(LutronLed(area_name, keypad, scene, led, entry_data.client))
40  async_add_entities(entities, True)
41 
42 
44  """Representation of a Lutron Switch."""
45 
46  _lutron_device: Output
47  _attr_name = None
48 
49  def turn_on(self, **kwargs: Any) -> None:
50  """Turn the switch on."""
51  self._lutron_device_lutron_device.level = 100
52 
53  def turn_off(self, **kwargs: Any) -> None:
54  """Turn the switch off."""
55  self._lutron_device_lutron_device.level = 0
56 
57  @property
58  def extra_state_attributes(self) -> Mapping[str, Any] | None:
59  """Return the state attributes."""
60  return {"lutron_integration_id": self._lutron_device_lutron_device.id}
61 
62  def _request_state(self) -> None:
63  """Request the state from the device."""
64  _ = self._lutron_device_lutron_device.level
65 
66  def _update_attrs(self) -> None:
67  """Update the state attributes."""
68  self._attr_is_on_attr_is_on = self._lutron_device_lutron_device.last_level() > 0
69 
70 
72  """Representation of a Lutron Keypad LED."""
73 
74  _lutron_device: Led
75 
76  def __init__(
77  self,
78  area_name: str,
79  keypad: Keypad,
80  scene_device: Button,
81  led_device: Led,
82  controller: Lutron,
83  ) -> None:
84  """Initialize the switch."""
85  super().__init__(area_name, led_device, controller, keypad)
86  self._keypad_name_keypad_name = keypad.name
87  self._attr_name_attr_name = scene_device.name
88 
89  def turn_on(self, **kwargs: Any) -> None:
90  """Turn the LED on."""
91  self._lutron_device_lutron_device.state = 1
92 
93  def turn_off(self, **kwargs: Any) -> None:
94  """Turn the LED off."""
95  self._lutron_device_lutron_device.state = 0
96 
97  @property
98  def extra_state_attributes(self) -> Mapping[str, Any] | None:
99  """Return the state attributes."""
100  return {
101  "keypad": self._keypad_name_keypad_name,
102  "scene": self._attr_name_attr_name,
103  "led": self._lutron_device_lutron_device.name,
104  }
105 
106  def _request_state(self) -> None:
107  """Request the state from the device."""
108  _ = self._lutron_device_lutron_device.state
109 
110  def _update_attrs(self) -> None:
111  """Update the state attributes."""
112  self._attr_is_on_attr_is_on = self._lutron_device_lutron_device.last_state
Mapping[str, Any]|None extra_state_attributes(self)
Definition: switch.py:98
None __init__(self, str area_name, Keypad keypad, Button scene_device, Led led_device, Lutron controller)
Definition: switch.py:83
Mapping[str, Any]|None extra_state_attributes(self)
Definition: switch.py:58
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23