Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for EnOcean binary sensors."""
2 
3 from __future__ import annotations
4 
5 from enocean.utils import combine_hex
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_ID, 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 .entity import EnOceanEntity
21 
22 DEFAULT_NAME = "EnOcean binary sensor"
23 DEPENDENCIES = ["enocean"]
24 EVENT_BUTTON_PRESSED = "button_pressed"
25 
26 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
27  {
28  vol.Required(CONF_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
29  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
30  vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
31  }
32 )
33 
34 
36  hass: HomeAssistant,
37  config: ConfigType,
38  add_entities: AddEntitiesCallback,
39  discovery_info: DiscoveryInfoType | None = None,
40 ) -> None:
41  """Set up the Binary Sensor platform for EnOcean."""
42  dev_id: list[int] = config[CONF_ID]
43  dev_name: str = config[CONF_NAME]
44  device_class: BinarySensorDeviceClass | None = config.get(CONF_DEVICE_CLASS)
45 
46  add_entities([EnOceanBinarySensor(dev_id, dev_name, device_class)])
47 
48 
50  """Representation of EnOcean binary sensors such as wall switches.
51 
52  Supported EEPs (EnOcean Equipment Profiles):
53  - F6-02-01 (Light and Blind Control - Application Style 2)
54  - F6-02-02 (Light and Blind Control - Application Style 1)
55  """
56 
57  def __init__(
58  self,
59  dev_id: list[int],
60  dev_name: str,
61  device_class: BinarySensorDeviceClass | None,
62  ) -> None:
63  """Initialize the EnOcean binary sensor."""
64  super().__init__(dev_id)
65  self._attr_device_class_attr_device_class = device_class
66  self.whichwhich = -1
67  self.onoffonoff = -1
68  self._attr_unique_id_attr_unique_id = f"{combine_hex(dev_id)}-{device_class}"
69  self._attr_name_attr_name = dev_name
70 
71  def value_changed(self, packet):
72  """Fire an event with the data that have changed.
73 
74  This method is called when there is an incoming packet associated
75  with this platform.
76 
77  Example packet data:
78  - 2nd button pressed
79  ['0xf6', '0x10', '0x00', '0x2d', '0xcf', '0x45', '0x30']
80  - button released
81  ['0xf6', '0x00', '0x00', '0x2d', '0xcf', '0x45', '0x20']
82  """
83  # Energy Bow
84  pushed = None
85 
86  if packet.data[6] == 0x30:
87  pushed = 1
88  elif packet.data[6] == 0x20:
89  pushed = 0
90 
91  self.schedule_update_ha_stateschedule_update_ha_state()
92 
93  action = packet.data[1]
94  if action == 0x70:
95  self.whichwhich = 0
96  self.onoffonoff = 0
97  elif action == 0x50:
98  self.whichwhich = 0
99  self.onoffonoff = 1
100  elif action == 0x30:
101  self.whichwhich = 1
102  self.onoffonoff = 0
103  elif action == 0x10:
104  self.whichwhich = 1
105  self.onoffonoff = 1
106  elif action == 0x37:
107  self.whichwhich = 10
108  self.onoffonoff = 0
109  elif action == 0x15:
110  self.whichwhich = 10
111  self.onoffonoff = 1
112  self.hasshass.bus.fire(
113  EVENT_BUTTON_PRESSED,
114  {
115  "id": self.dev_iddev_id,
116  "pushed": pushed,
117  "which": self.whichwhich,
118  "onoff": self.onoffonoff,
119  },
120  )
None __init__(self, list[int] dev_id, str dev_name, BinarySensorDeviceClass|None device_class)
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)