Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Ankuoo RecSwitch MS6126 devices."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyrecswitch import RSNetwork, RSNetworkError
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
13  SwitchEntity,
14 )
15 from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 DEFAULT_NAME = "RecSwitch {0}"
24 
25 DATA_RSN = "RSN"
26 
27 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
28  {
29  vol.Required(CONF_HOST): cv.string,
30  vol.Required(CONF_MAC): vol.All(cv.string, vol.Upper),
31  vol.Optional(CONF_NAME): cv.string,
32  }
33 )
34 
35 
37  hass: HomeAssistant,
38  config: ConfigType,
39  async_add_entities: AddEntitiesCallback,
40  discovery_info: DiscoveryInfoType | None = None,
41 ) -> None:
42  """Set up the device."""
43 
44  host = config[CONF_HOST]
45  mac_address = config[CONF_MAC]
46  device_name = config.get(CONF_NAME)
47 
48  if not hass.data.get(DATA_RSN):
49  hass.data[DATA_RSN] = RSNetwork()
50  job = hass.data[DATA_RSN].create_datagram_endpoint()
51  hass.async_create_task(job)
52 
53  device = hass.data[DATA_RSN].register_device(mac_address, host)
54  async_add_entities([RecSwitchSwitch(device, device_name, mac_address)])
55 
56 
58  """Representation of a recswitch device."""
59 
60  def __init__(self, device, device_name, mac_address):
61  """Initialize a recswitch device."""
62  self.gpio_stategpio_state = False
63  self.devicedevice = device
64  self.device_namedevice_name = device_name
65  self.mac_addressmac_address = mac_address
66  if not self.device_namedevice_name:
67  self.device_namedevice_name = DEFAULT_NAME.format(self.mac_addressmac_address)
68 
69  @property
70  def unique_id(self):
71  """Return the switch unique ID."""
72  return self.mac_addressmac_address
73 
74  @property
75  def name(self):
76  """Return the switch name."""
77  return self.device_namedevice_name
78 
79  @property
80  def is_on(self):
81  """Return true if switch is on."""
82  return self.gpio_stategpio_state
83 
84  async def async_turn_on(self, **kwargs: Any) -> None:
85  """Turn on the switch."""
86  await self.async_set_gpio_statusasync_set_gpio_status(True)
87 
88  async def async_turn_off(self, **kwargs: Any) -> None:
89  """Turn off the switch."""
90  await self.async_set_gpio_statusasync_set_gpio_status(False)
91 
92  async def async_set_gpio_status(self, status):
93  """Set the switch status."""
94 
95  try:
96  ret = await self.devicedevice.set_gpio_status(status)
97  self.gpio_stategpio_state = ret.state
98  except RSNetworkError as error:
99  _LOGGER.error("Setting status to %s: %r", self.namenamename, error)
100 
101  async def async_update(self) -> None:
102  """Update the current switch status."""
103 
104  try:
105  ret = await self.devicedevice.get_gpio_status()
106  self.gpio_stategpio_state = ret.state
107  except RSNetworkError as error:
108  _LOGGER.error("Reading status from %s: %r", self.namenamename, error)
def __init__(self, device, device_name, mac_address)
Definition: switch.py:60
str|UndefinedType|None name(self)
Definition: entity.py:738
def register_device(hass, api_key, name, device_id, device_ids, device_names)
Definition: __init__.py:47
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:41