Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for switches through the SmartThings cloud API."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Sequence
6 from typing import Any
7 
8 from pysmartthings import Capability
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 .const import DATA_BROKERS, DOMAIN
16 from .entity import SmartThingsEntity
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Add switches for a config entry."""
25  broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
27  SmartThingsSwitch(device)
28  for device in broker.devices.values()
29  if broker.any_assigned(device.device_id, "switch")
30  )
31 
32 
33 def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
34  """Return all capabilities supported if minimum required are present."""
35  # Must be able to be turned on/off.
36  if Capability.switch in capabilities:
37  return [Capability.switch, Capability.energy_meter, Capability.power_meter]
38  return None
39 
40 
42  """Define a SmartThings switch."""
43 
44  async def async_turn_off(self, **kwargs: Any) -> None:
45  """Turn the switch off."""
46  await self._device_device.switch_off(set_status=True)
47  # State is set optimistically in the command above, therefore update
48  # the entity state ahead of receiving the confirming push updates
49  self.async_write_ha_stateasync_write_ha_state()
50 
51  async def async_turn_on(self, **kwargs: Any) -> None:
52  """Turn the switch on."""
53  await self._device_device.switch_on(set_status=True)
54  # State is set optimistically in the command above, therefore update
55  # the entity state ahead of receiving the confirming push updates
56  self.async_write_ha_stateasync_write_ha_state()
57 
58  @property
59  def is_on(self) -> bool:
60  """Return true if light is on."""
61  return self._device_device.status.switch
None async_turn_on(self, **Any kwargs)
Definition: entity.py:1701
Sequence[str]|None get_capabilities(Sequence[str] capabilities)
Definition: switch.py:33
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23