Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for PCA 301 smart switch."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import pypca
9 from serial import SerialException
10 
11 from homeassistant.components.switch import SwitchEntity
12 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 DEFAULT_NAME = "PCA 301"
20 
21 
23  hass: HomeAssistant,
24  config: ConfigType,
25  add_entities: AddEntitiesCallback,
26  discovery_info: DiscoveryInfoType | None = None,
27 ) -> None:
28  """Set up the PCA switch platform."""
29 
30  if discovery_info is None:
31  return
32 
33  serial_device = discovery_info["device"]
34 
35  try:
36  pca = pypca.PCA(serial_device)
37  pca.open()
38 
39  entities = [SmartPlugSwitch(pca, device) for device in pca.get_devices()]
40  add_entities(entities, True)
41 
42  except SerialException as exc:
43  _LOGGER.warning("Unable to open serial port: %s", exc)
44  return
45 
46  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, pca.close)
47 
48  pca.start_scan()
49 
50 
52  """Representation of a PCA Smart Plug switch."""
53 
54  def __init__(self, pca, device_id):
55  """Initialize the switch."""
56  self._device_id_device_id = device_id
57  self._name_name = "PCA 301"
58  self._state_state = None
59  self._available_available = True
60  self._pca_pca = pca
61 
62  @property
63  def name(self):
64  """Return the name of the Smart Plug, if any."""
65  return self._name_name
66 
67  @property
68  def available(self) -> bool:
69  """Return if switch is available."""
70  return self._available_available
71 
72  @property
73  def is_on(self):
74  """Return true if switch is on."""
75  return self._state_state
76 
77  def turn_on(self, **kwargs: Any) -> None:
78  """Turn the switch on."""
79  self._pca_pca.turn_on(self._device_id_device_id)
80 
81  def turn_off(self, **kwargs: Any) -> None:
82  """Turn the switch off."""
83  self._pca_pca.turn_off(self._device_id_device_id)
84 
85  def update(self) -> None:
86  """Update the PCA switch's state."""
87  try:
88  self._state_state = self._pca_pca.get_state(self._device_id_device_id)
89  self._available_available = True
90 
91  except OSError as ex:
92  if self._available_available:
93  _LOGGER.warning("Could not read state for %s: %s", self.namenamename, ex)
94  self._available_available = False
str|UndefinedType|None name(self)
Definition: entity.py:738
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:27