diff --git a/.coveragerc b/.coveragerc index 985e73f0f6f849..5f361929f5dff2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -552,6 +552,7 @@ omit = homeassistant/components/folder_watcher.py homeassistant/components/foursquare.py homeassistant/components/goalfeed.py + homeassistant/components/idteck_prox.py homeassistant/components/ifttt.py homeassistant/components/image_processing/dlib_face_detect.py homeassistant/components/image_processing/dlib_face_identify.py diff --git a/homeassistant/components/idteck_prox.py b/homeassistant/components/idteck_prox.py new file mode 100644 index 00000000000000..90c07c487b6b95 --- /dev/null +++ b/homeassistant/components/idteck_prox.py @@ -0,0 +1,75 @@ +"""Component for interfacing RFK101 proximity card readers. + +For more details about this component, please refer to the documentation at +https://home-assistant.io/components/idteck_prox/ +""" +import logging + +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.const import ( + CONF_HOST, CONF_PORT, CONF_NAME, EVENT_HOMEASSISTANT_STOP) + +REQUIREMENTS = ['rfk101py==0.0.1'] + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = "idteck_prox" + +EVENT_IDTECK_PROX_KEYCARD = 'idteck_prox_keycard' + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.All(cv.ensure_list, [vol.Schema({ + vol.Required(CONF_HOST): cv.string, + vol.Required(CONF_PORT): cv.port, + vol.Required(CONF_NAME): cv.string, + })]) +}, extra=vol.ALLOW_EXTRA) + + +def setup(hass, config): + """Set up the IDTECK proximity card component.""" + conf = config[DOMAIN] + for unit in conf: + host = unit[CONF_HOST] + port = unit[CONF_PORT] + name = unit[CONF_NAME] + + try: + reader = IdteckReader(hass, host, port, name) + reader.connect() + hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, reader.stop) + except OSError as error: + _LOGGER.error('Error creating "%s". %s', name, error) + return False + + return True + + +class IdteckReader(): + """Representation of an IDTECK proximity card reader.""" + + def __init__(self, hass, host, port, name): + """Initialize the reader.""" + self.hass = hass + self._host = host + self._port = port + self._name = name + self._connection = None + + def connect(self): + """Connect to the reader.""" + from rfk101py.rfk101py import rfk101py + self._connection = rfk101py(self._host, self._port, self._callback) + + def _callback(self, card): + """Send a keycard event message into HASS whenever a card is read.""" + self.hass.bus.fire( + EVENT_IDTECK_PROX_KEYCARD, {'card': card, 'name': self._name}) + + def stop(self): + """Close resources.""" + if self._connection: + self._connection.close() + self._connection = None diff --git a/requirements_all.txt b/requirements_all.txt index 87b97cc2746116..ff64a2104f6b89 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1416,6 +1416,9 @@ regenmaschine==1.1.0 # homeassistant.components.python_script restrictedpython==4.0b7 +# homeassistant.components.idteck_prox +rfk101py==0.0.1 + # homeassistant.components.rflink rflink==0.0.37