Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for the Dynalite channels and presets as switches."""
2 
3 from typing import Any
4 
5 from homeassistant.components.switch import SwitchEntity
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.const import STATE_ON
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from .entity import DynaliteBase, async_setup_entry_base
12 
13 
15  hass: HomeAssistant,
16  config_entry: ConfigEntry,
17  async_add_entities: AddEntitiesCallback,
18 ) -> None:
19  """Record the async_add_entities function to add them later when received from Dynalite."""
21  hass, config_entry, async_add_entities, "switch", DynaliteSwitch
22  )
23 
24 
26  """Representation of a Dynalite Channel as a Home Assistant Switch."""
27 
28  @property
29  def is_on(self) -> bool:
30  """Return true if switch is on."""
31  return self._device_device.is_on
32 
33  async def async_turn_on(self, **kwargs: Any) -> None:
34  """Turn the switch on."""
35  await self._device_device.async_turn_on()
36 
37  async def async_turn_off(self, **kwargs: Any) -> None:
38  """Turn the switch off."""
39  await self._device_device.async_turn_off()
40 
41  def initialize_state(self, state):
42  """Initialize the state from cache."""
43  target_level = 1 if state.state == STATE_ON else 0
44  self._device_device.init_level(target_level)
None async_setup_entry_base(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities, str platform, Callable entity_from_device)
Definition: entity.py:26
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:18