Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Light support for switch entities."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
11  ColorMode,
12  LightEntity,
13 )
14 from homeassistant.const import (
15  ATTR_ENTITY_ID,
16  CONF_ENTITY_ID,
17  CONF_NAME,
18  SERVICE_TURN_OFF,
19  SERVICE_TURN_ON,
20  STATE_ON,
21  STATE_UNAVAILABLE,
22 )
23 from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
24 from homeassistant.helpers import entity_registry as er
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.event import async_track_state_change_event
28 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
29 
30 from .const import DOMAIN as SWITCH_DOMAIN
31 
32 DEFAULT_NAME = "Light Switch"
33 
34 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
35  {
36  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
37  vol.Required(CONF_ENTITY_ID): cv.entity_domain(SWITCH_DOMAIN),
38  }
39 )
40 
41 
43  hass: HomeAssistant,
44  config: ConfigType,
45  async_add_entities: AddEntitiesCallback,
46  discovery_info: DiscoveryInfoType | None = None,
47 ) -> None:
48  """Initialize Light Switch platform."""
49  registry = er.async_get(hass)
50  wrapped_switch = registry.async_get(config[CONF_ENTITY_ID])
51  unique_id = wrapped_switch.unique_id if wrapped_switch else None
52 
54  [
56  config[CONF_NAME],
57  config[CONF_ENTITY_ID],
58  unique_id,
59  )
60  ]
61  )
62 
63 
65  """Represents a Switch as a Light."""
66 
67  _attr_color_mode = ColorMode.ONOFF
68  _attr_should_poll = False
69  _attr_supported_color_modes = {ColorMode.ONOFF}
70 
71  def __init__(self, name: str, switch_entity_id: str, unique_id: str | None) -> None:
72  """Initialize Light Switch."""
73  self._attr_name_attr_name = name
74  self._attr_unique_id_attr_unique_id = unique_id
75  self._switch_entity_id_switch_entity_id = switch_entity_id
76 
77  async def async_turn_on(self, **kwargs: Any) -> None:
78  """Forward the turn_on command to the switch in this light switch."""
79  await self.hasshass.services.async_call(
80  SWITCH_DOMAIN,
81  SERVICE_TURN_ON,
82  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
83  blocking=True,
84  context=self._context_context,
85  )
86 
87  async def async_turn_off(self, **kwargs: Any) -> None:
88  """Forward the turn_off command to the switch in this light switch."""
89  await self.hasshass.services.async_call(
90  SWITCH_DOMAIN,
91  SERVICE_TURN_OFF,
92  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
93  blocking=True,
94  context=self._context_context,
95  )
96 
97  async def async_added_to_hass(self) -> None:
98  """Register callbacks."""
99 
100  @callback
101  def async_state_changed_listener(
102  event: Event[EventStateChangedData] | None = None,
103  ) -> None:
104  """Handle child updates."""
105  if (
106  state := self.hasshass.states.get(self._switch_entity_id_switch_entity_id)
107  ) is None or state.state == STATE_UNAVAILABLE:
108  self._attr_available_attr_available = False
109  return
110  self._attr_available_attr_available = True
111  self._attr_is_on_attr_is_on = state.state == STATE_ON
112  self.async_write_ha_stateasync_write_ha_state()
113 
114  self.async_on_removeasync_on_remove(
116  self.hasshass, [self._switch_entity_id_switch_entity_id], async_state_changed_listener
117  )
118  )
119  # Call once on adding
120  async_state_changed_listener()
None async_turn_off(self, **Any kwargs)
Definition: light.py:87
None __init__(self, str name, str switch_entity_id, str|None unique_id)
Definition: light.py:71
None async_turn_on(self, **Any kwargs)
Definition: light.py:77
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:47
CALLBACK_TYPE async_track_state_change_event(HomeAssistant hass, str|Iterable[str] entity_ids, Callable[[Event[EventStateChangedData]], Any] action, HassJobType|None job_type=None)
Definition: event.py:314