Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Abode Security System switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from jaraco.abode.devices.switch import Switch
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import AbodeSystem
16 from .const import DOMAIN
17 from .entity import AbodeAutomation, AbodeDevice
18 
19 DEVICE_TYPES = ["switch", "valve"]
20 
21 
23  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
24 ) -> None:
25  """Set up Abode switch devices."""
26  data: AbodeSystem = hass.data[DOMAIN]
27 
28  entities: list[SwitchEntity] = [
29  AbodeSwitch(data, device)
30  for device_type in DEVICE_TYPES
31  for device in data.abode.get_devices(generic_type=device_type)
32  ]
33 
34  entities.extend(
35  AbodeAutomationSwitch(data, automation)
36  for automation in data.abode.get_automations()
37  )
38 
39  async_add_entities(entities)
40 
41 
43  """Representation of an Abode switch."""
44 
45  _device: Switch
46  _attr_name = None
47 
48  def turn_on(self, **kwargs: Any) -> None:
49  """Turn on the device."""
50  self._device_device.switch_on()
51 
52  def turn_off(self, **kwargs: Any) -> None:
53  """Turn off the device."""
54  self._device_device.switch_off()
55 
56  @property
57  def is_on(self) -> bool:
58  """Return true if device is on."""
59  return cast(bool, self._device_device.is_on)
60 
61 
63  """A switch implementation for Abode automations."""
64 
65  _attr_translation_key = "automation"
66 
67  async def async_added_to_hass(self) -> None:
68  """Set up trigger automation service."""
69  await super().async_added_to_hass()
70 
71  signal = f"abode_trigger_automation_{self.entity_id}"
72  self.async_on_removeasync_on_remove(async_dispatcher_connect(self.hasshass, signal, self.triggertrigger))
73 
74  def turn_on(self, **kwargs: Any) -> None:
75  """Enable the automation."""
76  if self._automation_automation.enable(True):
77  self.schedule_update_ha_stateschedule_update_ha_state()
78 
79  def turn_off(self, **kwargs: Any) -> None:
80  """Disable the automation."""
81  if self._automation_automation.enable(False):
82  self.schedule_update_ha_stateschedule_update_ha_state()
83 
84  def trigger(self) -> None:
85  """Trigger the automation."""
86  self._automation_automation.trigger()
87 
88  @property
89  def is_on(self) -> bool:
90  """Return True if the automation is enabled."""
91  return bool(self._automation_automation.enabled)
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:24
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103