Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for binary sensor using GC100."""
2 
3 from __future__ import annotations
4 
5 import voluptuous as vol
6 
8  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
9  BinarySensorEntity,
10 )
11 from homeassistant.const import DEVICE_DEFAULT_NAME
12 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16 
17 from . import CONF_PORTS, DATA_GC100
18 
19 _SENSORS_SCHEMA = vol.Schema({cv.string: cv.string})
20 
21 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
22  {vol.Required(CONF_PORTS): vol.All(cv.ensure_list, [_SENSORS_SCHEMA])}
23 )
24 
25 
27  hass: HomeAssistant,
28  config: ConfigType,
29  add_entities: AddEntitiesCallback,
30  discovery_info: DiscoveryInfoType | None = None,
31 ) -> None:
32  """Set up the GC100 devices."""
33  binary_sensors = []
34  ports = config[CONF_PORTS]
35  for port in ports:
36  for port_addr, port_name in port.items():
37  binary_sensors.append(
38  GC100BinarySensor(port_name, port_addr, hass.data[DATA_GC100])
39  )
40  add_entities(binary_sensors, True)
41 
42 
44  """Representation of a binary sensor from GC100."""
45 
46  def __init__(self, name, port_addr, gc100):
47  """Initialize the GC100 binary sensor."""
48  self._name_name = name or DEVICE_DEFAULT_NAME
49  self._port_addr_port_addr = port_addr
50  self._gc100_gc100 = gc100
51  self._state_state = None
52 
53  # Subscribe to be notified about state changes (PUSH)
54  self._gc100_gc100.subscribe(self._port_addr_port_addr, self.set_stateset_state)
55 
56  @property
57  def name(self):
58  """Return the name of the sensor."""
59  return self._name_name
60 
61  @property
62  def is_on(self):
63  """Return the state of the entity."""
64  return self._state_state
65 
66  def update(self) -> None:
67  """Update the sensor state."""
68  self._gc100_gc100.read_sensor(self._port_addr_port_addr, self.set_stateset_state)
69 
70  def set_state(self, state):
71  """Set the current state."""
72  self._state_state = state == 1
73  self.schedule_update_ha_stateschedule_update_ha_state()
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)
Callable[[], None] subscribe(HomeAssistant hass, str topic, MessageCallbackType msg_callback, int qos=DEFAULT_QOS, str encoding="utf-8")
Definition: client.py:247