Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Component for interfacing RFK101 proximity card readers."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from rfk101py.rfk101py import rfk101py
8 import voluptuous as vol
9 
10 from homeassistant.const import (
11  CONF_HOST,
12  CONF_NAME,
13  CONF_PORT,
14  EVENT_HOMEASSISTANT_STOP,
15 )
16 from homeassistant.core import Event, HomeAssistant
18 from homeassistant.helpers.typing import ConfigType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 DOMAIN = "idteck_prox"
23 
24 EVENT_IDTECK_PROX_KEYCARD = "idteck_prox_keycard"
25 
26 CONFIG_SCHEMA = vol.Schema(
27  {
28  DOMAIN: vol.All(
29  cv.ensure_list,
30  [
31  vol.Schema(
32  {
33  vol.Required(CONF_HOST): cv.string,
34  vol.Required(CONF_PORT): cv.port,
35  vol.Required(CONF_NAME): cv.string,
36  }
37  )
38  ],
39  )
40  },
41  extra=vol.ALLOW_EXTRA,
42 )
43 
44 
45 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
46  """Set up the IDTECK proximity card component."""
47  conf = config[DOMAIN]
48  for unit in conf:
49  host = unit[CONF_HOST]
50  port = unit[CONF_PORT]
51  name = unit[CONF_NAME]
52 
53  try:
54  reader = IdteckReader(hass, host, port, name)
55  reader.connect()
56  hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, reader.stop)
57  except OSError as error:
58  _LOGGER.error("Error creating %s. %s", name, error)
59  return False
60 
61  return True
62 
63 
65  """Representation of an IDTECK proximity card reader."""
66 
67  def __init__(self, hass, host, port, name):
68  """Initialize the reader."""
69  self.hasshass = hass
70  self._host_host = host
71  self._port_port = port
72  self._name_name = name
73  self._connection_connection = None
74 
75  def connect(self):
76  """Connect to the reader."""
77 
78  self._connection_connection = rfk101py(self._host_host, self._port_port, self._callback_callback)
79 
80  def _callback(self, card):
81  """Send a keycard event message into Home Assistant whenever a card is read."""
82  self.hasshass.bus.fire(
83  EVENT_IDTECK_PROX_KEYCARD, {"card": card, "name": self._name_name}
84  )
85 
86  def stop(self, _: Event) -> None:
87  """Close resources."""
88  if self._connection_connection:
89  self._connection_connection.close()
90  self._connection_connection = None
def __init__(self, hass, host, port, name)
Definition: __init__.py:67
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:45