Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for IHC binary sensors."""
2 
3 from __future__ import annotations
4 
5 from ihcsdk.ihccontroller import IHCController
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.const import CONF_TYPE
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
15 from homeassistant.util.enum import try_parse_enum
16 
17 from .const import CONF_INVERTING, DOMAIN, IHC_CONTROLLER
18 from .entity import IHCEntity
19 
20 
22  hass: HomeAssistant,
23  config: ConfigType,
24  add_entities: AddEntitiesCallback,
25  discovery_info: DiscoveryInfoType | None = None,
26 ) -> None:
27  """Set up the IHC binary sensor platform."""
28  if discovery_info is None:
29  return
30  devices = []
31  for name, device in discovery_info.items():
32  ihc_id = device["ihc_id"]
33  product_cfg = device["product_cfg"]
34  product = device["product"]
35  # Find controller that corresponds with device id
36  controller_id = device["ctrl_id"]
37  ihc_controller: IHCController = hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
38  sensor = IHCBinarySensor(
39  ihc_controller,
40  controller_id,
41  name,
42  ihc_id,
43  product_cfg.get(CONF_TYPE),
44  product_cfg[CONF_INVERTING],
45  product,
46  )
47  devices.append(sensor)
48  add_entities(devices)
49 
50 
52  """IHC Binary Sensor.
53 
54  The associated IHC resource can be any in or output from a IHC product
55  or function block, but it must be a boolean ON/OFF resources.
56  """
57 
58  def __init__(
59  self,
60  ihc_controller: IHCController,
61  controller_id: str,
62  name: str,
63  ihc_id: int,
64  sensor_type: str,
65  inverting: bool,
66  product=None,
67  ) -> None:
68  """Initialize the IHC binary sensor."""
69  super().__init__(ihc_controller, controller_id, name, ihc_id, product)
70  self._attr_device_class_attr_device_class = try_parse_enum(BinarySensorDeviceClass, sensor_type)
71  self.invertinginverting = inverting
72 
73  def on_ihc_change(self, ihc_id, value):
74  """IHC resource has changed."""
75  if self.invertinginverting:
76  self._attr_is_on_attr_is_on = not value
77  else:
78  self._attr_is_on_attr_is_on = value
79  self.schedule_update_ha_stateschedule_update_ha_state()
None __init__(self, IHCController ihc_controller, str controller_id, str name, int ihc_id, str sensor_type, bool inverting, product=None)
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)