Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for ADS binary sensors."""
2 
3 from __future__ import annotations
4 
5 import pyads
6 import voluptuous as vol
7 
9  DEVICE_CLASSES_SCHEMA,
10  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13 )
14 from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 from .const import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE
21 from .entity import AdsEntity
22 from .hub import AdsHub
23 
24 DEFAULT_NAME = "ADS binary sensor"
25 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
26  {
27  vol.Required(CONF_ADS_VAR): cv.string,
28  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
29  vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
30  }
31 )
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  add_entities: AddEntitiesCallback,
38  discovery_info: DiscoveryInfoType | None = None,
39 ) -> None:
40  """Set up the Binary Sensor platform for ADS."""
41  ads_hub = hass.data[DATA_ADS]
42 
43  ads_var: str = config[CONF_ADS_VAR]
44  name: str = config[CONF_NAME]
45  device_class: BinarySensorDeviceClass | None = config.get(CONF_DEVICE_CLASS)
46 
47  ads_sensor = AdsBinarySensor(ads_hub, name, ads_var, device_class)
48  add_entities([ads_sensor])
49 
50 
52  """Representation of ADS binary sensors."""
53 
54  def __init__(
55  self,
56  ads_hub: AdsHub,
57  name: str,
58  ads_var: str,
59  device_class: BinarySensorDeviceClass | None,
60  ) -> None:
61  """Initialize ADS binary sensor."""
62  super().__init__(ads_hub, name, ads_var)
63  self._attr_device_class_attr_device_class = device_class or BinarySensorDeviceClass.MOVING
64 
65  async def async_added_to_hass(self) -> None:
66  """Register device notification."""
67  await self.async_initialize_deviceasync_initialize_device(self._ads_var_ads_var, pyads.PLCTYPE_BOOL)
68 
69  @property
70  def is_on(self) -> bool:
71  """Return True if the entity is on."""
72  return self._state_dict[STATE_KEY_STATE]
None __init__(self, AdsHub ads_hub, str name, str ads_var, BinarySensorDeviceClass|None device_class)
None async_initialize_device(self, str ads_var, type plctype, str state_key=STATE_KEY_STATE, int|None factor=None)
Definition: entity.py:37
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)