Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """The ATEN PE switch component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from atenpdu import AtenPE, AtenPEError
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
13  SwitchDeviceClass,
14  SwitchEntity,
15 )
16 from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import PlatformNotReady
20 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 CONF_AUTH_KEY = "auth_key"
27 CONF_COMMUNITY = "community"
28 CONF_PRIV_KEY = "priv_key"
29 DEFAULT_COMMUNITY = "private"
30 DEFAULT_PORT = "161"
31 DEFAULT_USERNAME = "administrator"
32 
33 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
34  {
35  vol.Required(CONF_HOST): cv.string,
36  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
37  vol.Optional(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): cv.string,
38  vol.Optional(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
39  vol.Optional(CONF_AUTH_KEY): cv.string,
40  vol.Optional(CONF_PRIV_KEY): cv.string,
41  }
42 )
43 
44 
46  hass: HomeAssistant,
47  config: ConfigType,
48  async_add_entities: AddEntitiesCallback,
49  discovery_info: DiscoveryInfoType | None = None,
50 ) -> None:
51  """Set up the ATEN PE switch."""
52  node = config[CONF_HOST]
53  serv = config[CONF_PORT]
54 
55  dev = AtenPE(
56  node=node,
57  serv=serv,
58  community=config[CONF_COMMUNITY],
59  username=config[CONF_USERNAME],
60  authkey=config.get(CONF_AUTH_KEY),
61  privkey=config.get(CONF_PRIV_KEY),
62  )
63 
64  try:
65  await hass.async_add_executor_job(dev.initialize)
66  mac = await dev.deviceMAC()
67  outlets = dev.outlets()
68  name = await dev.deviceName()
69  model = await dev.modelName()
70  sw_version = await dev.deviceFWversion()
71  except AtenPEError as exc:
72  _LOGGER.error("Failed to initialize %s:%s: %s", node, serv, str(exc))
73  raise PlatformNotReady from exc
74 
75  info = DeviceInfo(
76  connections={(CONNECTION_NETWORK_MAC, mac)},
77  manufacturer="ATEN",
78  model=model,
79  name=name,
80  sw_version=sw_version,
81  )
82 
84  (AtenSwitch(dev, info, mac, outlet.id, outlet.name) for outlet in outlets), True
85  )
86 
87 
89  """Represents an ATEN PE switch."""
90 
91  _attr_device_class = SwitchDeviceClass.OUTLET
92 
93  def __init__(
94  self, device: AtenPE, info: DeviceInfo, mac: str, outlet: str, name: str
95  ) -> None:
96  """Initialize an ATEN PE switch."""
97  self._device_device = device
98  self._outlet_outlet = outlet
99  self._attr_device_info_attr_device_info = info
100  self._attr_unique_id_attr_unique_id = f"{mac}-{outlet}"
101  self._attr_name_attr_name = name or f"Outlet {outlet}"
102 
103  async def async_turn_on(self, **kwargs: Any) -> None:
104  """Turn the switch on."""
105  await self._device_device.setOutletStatus(self._outlet_outlet, "on")
106  self._attr_is_on_attr_is_on = True
107 
108  async def async_turn_off(self, **kwargs: Any) -> None:
109  """Turn the switch off."""
110  await self._device_device.setOutletStatus(self._outlet_outlet, "off")
111  self._attr_is_on_attr_is_on = False
112 
113  async def async_update(self) -> None:
114  """Process update from entity."""
115  status = await self._device_device.displayOutletStatus(self._outlet_outlet)
116  if status == "on":
117  self._attr_is_on_attr_is_on = True
118  elif status == "off":
119  self._attr_is_on_attr_is_on = False
None __init__(self, AtenPE device, DeviceInfo info, str mac, str outlet, str name)
Definition: switch.py:95
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:50