Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for IHC switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from ihcsdk.ihccontroller import IHCController
8 
9 from homeassistant.components.switch import SwitchEntity
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from .const import CONF_OFF_ID, CONF_ON_ID, DOMAIN, IHC_CONTROLLER
15 from .entity import IHCEntity
16 from .util import async_pulse, async_set_bool
17 
18 
20  hass: HomeAssistant,
21  config: ConfigType,
22  add_entities: AddEntitiesCallback,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> None:
25  """Set up the IHC switch platform."""
26  if discovery_info is None:
27  return
28  devices = []
29  for name, device in discovery_info.items():
30  ihc_id = device["ihc_id"]
31  product_cfg = device["product_cfg"]
32  product = device["product"]
33  # Find controller that corresponds with device id
34  controller_id = device["ctrl_id"]
35  ihc_controller: IHCController = hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
36  ihc_off_id = product_cfg.get(CONF_OFF_ID)
37  ihc_on_id = product_cfg.get(CONF_ON_ID)
38 
39  switch = IHCSwitch(
40  ihc_controller, controller_id, name, ihc_id, ihc_off_id, ihc_on_id, product
41  )
42  devices.append(switch)
43  add_entities(devices)
44 
45 
47  """Representation of an IHC switch."""
48 
49  def __init__(
50  self,
51  ihc_controller: IHCController,
52  controller_id: str,
53  name: str,
54  ihc_id: int,
55  ihc_off_id: int,
56  ihc_on_id: int,
57  product=None,
58  ) -> None:
59  """Initialize the IHC switch."""
60  super().__init__(ihc_controller, controller_id, name, ihc_id, product)
61  self._ihc_off_id_ihc_off_id = ihc_off_id
62  self._ihc_on_id_ihc_on_id = ihc_on_id
63 
64  async def async_turn_on(self, **kwargs: Any) -> None:
65  """Turn the switch on."""
66  if self._ihc_on_id_ihc_on_id:
67  await async_pulse(self.hasshass, self.ihc_controllerihc_controller, self._ihc_on_id_ihc_on_id)
68  else:
69  await async_set_bool(self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, True)
70 
71  async def async_turn_off(self, **kwargs: Any) -> None:
72  """Turn the device off."""
73  if self._ihc_off_id_ihc_off_id:
74  await async_pulse(self.hasshass, self.ihc_controllerihc_controller, self._ihc_off_id_ihc_off_id)
75  else:
76  await async_set_bool(self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, False)
77 
78  def on_ihc_change(self, ihc_id, value):
79  """Handle IHC resource change."""
80  self._attr_is_on_attr_is_on = value
81  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, IHCController ihc_controller, str controller_id, str name, int ihc_id, int ihc_off_id, int ihc_on_id, product=None)
Definition: switch.py:58
None async_turn_on(self, **Any kwargs)
Definition: switch.py:64
None async_turn_off(self, **Any kwargs)
Definition: switch.py:71
def on_ihc_change(self, ihc_id, value)
Definition: switch.py:78
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:24
None async_pulse(HomeAssistant hass, IHCController ihc_controller, int ihc_id)
Definition: util.py:12
asyncio.Future[bool] async_set_bool(HomeAssistant hass, IHCController ihc_controller, int ihc_id, bool value)
Definition: util.py:22